-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.java
More file actions
114 lines (99 loc) · 4.83 KB
/
Copy pathApplication.java
File metadata and controls
114 lines (99 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Application {
public static void main(String[] args) throws Exception {
List<RecordDTO> records = new ArrayList<>();
Map<Long, String> results = new HashMap<>();
//Input the CSV file
try (BufferedReader br = new BufferedReader(new FileReader("order_brush_order.csv"))) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String line;
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
//Convert from String to Object
RecordDTO record = new RecordDTO();
record.setOrderId(Long.parseLong(values[0]));
record.setShopId(Long.parseLong(values[1]));
record.setUserId(Long.parseLong(values[2]));
record.setEventTime(df.parse(values[3]));
records.add(record);
}
//Sort by event_time ASC
Collections.sort(records);
//Grouped by shopid
Map<Long, List<RecordDTO>> recordsGrouped = records.stream().collect(Collectors.groupingBy(RecordDTO::getShopId));
//For each shop...
for (Map.Entry<Long, List<RecordDTO>> perShop : recordsGrouped.entrySet()) {
//Grouped by hour
Map<Integer, List<RecordDTO>> objectsPerHour = perShop.getValue().stream().collect(Collectors.groupingBy(RecordDTO::getHour));
List<Long> suspectedUsers = new ArrayList<>();
//For each hour...
for (Map.Entry<Integer, List<RecordDTO>> perHour : objectsPerHour.entrySet()) {
//Distinct the buyer
List<Long> userids = perHour.getValue().stream().map(RecordDTO::getUserId).distinct().collect(Collectors.toList());
Map<Long, Integer> concurrences = new LinkedHashMap<>();
//If concurrence rate < 3 then skip this loop
if (perHour.getValue().size() / userids.size() < 3) {
continue;
}
//If concurrence rate >= 3, get the frequency of that userid in that hour
for (Long userid : userids) {
concurrences.put(userid, Collections.frequency(perHour.getValue(), userid));
}
//Sort the concurrences in descending order and get the suspected userid
concurrences.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()));
Integer max = concurrences.entrySet().iterator().next().getValue();
for (Map.Entry<Long, Integer> entry : concurrences.entrySet()) {
if (entry.getValue().equals(max)) {
suspectedUsers.add(entry.getKey());
}
}
}
//Convert the suspected buyers to String separated by '&'
StringBuffer userIdsBuffer = new StringBuffer();
if (suspectedUsers.size() > 0) {
for (Long user : suspectedUsers) {
userIdsBuffer.append(user);
userIdsBuffer.append("&");
}
} else {
userIdsBuffer.append(0);
}
String userIdResults = userIdsBuffer.toString();
if (userIdResults.substring(userIdResults.length() - 1).equals("&")){
userIdResults = userIdResults.substring(0, userIdResults.length() - 1);
}
//Put the shopid along with the userid into the final Map
results.put(perShop.getKey(), userIdResults);
}
//Write to the output CSV
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("submissions.csv"), "UTF-8"));
bw.write("shopid,userid");
bw.newLine();
for (Map.Entry<Long, String> entry : results.entrySet()) {
StringBuffer oneLine = new StringBuffer();
oneLine.append(entry.getKey());
oneLine.append(",");
oneLine.append(entry.getValue());
bw.write(oneLine.toString());
bw.newLine();
}
bw.flush();
bw.close();
}
}
}