This repository was archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCodingCompCSVUtil.java
More file actions
100 lines (87 loc) · 4.15 KB
/
CodingCompCSVUtil.java
File metadata and controls
100 lines (87 loc) · 4.15 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
package codingcompetition2019;
import java.io.IOException;
import java.nio.file.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CodingCompCSVUtil {
private Stream<List<String>> readCSVStream(String fileName) throws IOException {
Path file = Paths.get(fileName);
return Files.lines(file).map(line -> {
String[] lineContents = line.split(",");
return Arrays.asList(lineContents);
});
}
public List<List<String>> readCSVFileByCountry(String fileName, String countryName) throws IOException {
return readCSVStream(fileName).filter(
line -> countryName.equals(line.get(0))
).collect(Collectors.toList());
}
public List<List<String>> readCSVFileWithHeaders(String fileName) throws IOException {
return readCSVStream(fileName).collect(Collectors.toList());
}
public List<List<String>> readCSVFileWithoutHeaders(String fileName) throws IOException {
return readCSVStream(fileName).skip(1).collect(Collectors.toList());
}
private DisasterDescription getMostImpactfulDisaster(Stream<DisasterDescription> ddStream) {
return ddStream.max(
Comparator.comparingInt(
DisasterDescription::getReportedIncidentsNum
)
).orElseThrow(() -> new IllegalArgumentException("records must not be empty"));
}
public DisasterDescription getMostImpactfulYear(List<List<String>> records) {
Stream<DisasterDescription> dds = records.stream()
.map(DisasterDescription::new);
return getMostImpactfulDisaster(dds);
}
public DisasterDescription getMostImpactfulYearByCategory(String category, List<List<String>> records) {
Stream<DisasterDescription> ddsByCategory = records.stream()
.map(DisasterDescription::new)
.filter(dd -> dd.getCategory().equals(category));
return getMostImpactfulDisaster(ddsByCategory);
}
public DisasterDescription getMostImpactfulDisasterByYear(String year, List<List<String>> records) {
Stream<DisasterDescription> ddsByYear = records.stream()
.map(DisasterDescription::new)
.filter(dd -> !dd.getCategory().equals("All natural disasters"))
.filter(dd -> dd.getYear().equals(year));
return getMostImpactfulDisaster(ddsByYear);
}
public DisasterDescription getTotalReportedIncidentsByCategory(String category, List<List<String>> records) {
int totalReportedIncidents = records.stream()
.map(DisasterDescription::new)
.filter(dd -> dd.getCategory().equals(category))
.mapToInt(DisasterDescription::getReportedIncidentsNum)
.sum();
return new DisasterDescription(category, "", "", totalReportedIncidents);
}
/**
* This method will return the count if the number of incident falls within the provided range.
* To simplify the problem, we assume:
* + A value of -1 is provided if the max range is NOT applicable.
* + A min value can be provided, without providing a max value (which then has to be -1 like indicated above).
* + If a max value is provided, then a max value is also needed.
*/
public int countImpactfulYearsWithReportedIncidentsWithinRange(List<List<String>> records, int min, int max) {
return (int) records.stream()
.map(DisasterDescription::new)
.filter(disaster -> {
int incidents = disaster.getReportedIncidentsNum();
return (min <= incidents) && (max == -1 || incidents <= max);
}).count();
}
private int countReportedIncidents(List<List<String>> records) {
return records.stream()
.map(DisasterDescription::new)
.mapToInt(DisasterDescription::getReportedIncidentsNum)
.sum();
}
public boolean firstRecordsHaveMoreReportedIndicents(List<List<String>> records1, List<List<String>> records2) {
int incidents1 = countReportedIncidents(records1);
int incidents2 = countReportedIncidents(records2);
return incidents1 > incidents2;
}
}