Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions feedback.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Your team (name of each individual participating):
How many JUnits were you able to get to pass?
Your team (name of each individual participating): Harshal Sheth, Ankur Sundara
How many JUnits were you able to get to pass? 10/10

Document and describe any enhancements included to help the judges properly grade your submission.
Step 1:
Step 2:


Feedback for the coding competition? Things you would like to see in future events?
Step 0: We added a web-based frontend for visualizing and interacting with the provided data.
Step 1: To use it, simply open the `webui/Main.java` file, and click the green arrow
beside the Main class. Click the option for 'Run Main.main()'.
Step 2: Once our server has started, navigate to http://localhost:4567 in a browser, and
follow the instructions on using our tool.

Feedback for the coding competition? Things you would like to see in future events?
The competition was extremely fun!
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
<groupId>codingcompetition2019</groupId>
<artifactId>codingcompetition2019</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>codingcompetition2019</name>
<url>http://maven.apache.org</url>
Expand All @@ -16,11 +28,26 @@
</properties>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.28</version>
</dependency>
</dependencies>
</project>
114 changes: 78 additions & 36 deletions src/main/java/codingcompetition2019/CodingCompCSVUtil.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
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 {
public List<List<String>> readCSVFileByCountry(String fileName, String countryName) throws IOException {
// TODO implement this method
return null;
}

public List<List<String>> readCSVFileWithHeaders(String fileName) throws IOException {
// TODO implement this method
return null;
}

public List<List<String>> readCSVFileWithoutHeaders(String fileName) throws IOException {
// TODO implement this method
return null;
}

public DisasterDescription getMostImpactfulYear(List<List<String>> records) {
// TODO implement this method
return null;
}
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 DisasterDescription getMostImpactfulYearByCategory(String category, List<List<String>> records) {
// TODO implement this method
return null;
}
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 DisasterDescription getMostImpactfulDisasterByYear(String year, List<List<String>> records) {
// TODO implement this method
return null;
}
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);
}

public DisasterDescription getTotalReportedIncidentsByCategory(String category, List<List<String>> records) {
// TODO implement this method
return null;
}

/**
* This method will return the count if the number of incident falls within the provided range.
* To simplify the problem, we assume:
Expand All @@ -47,12 +77,24 @@ public DisasterDescription getTotalReportedIncidentsByCategory(String category,
* + If a max value is provided, then a max value is also needed.
*/
public int countImpactfulYearsWithReportedIncidentsWithinRange(List<List<String>> records, int min, int max) {
// TODO implement this method
return -1;
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) {
// TODO implement this method
return false;
int incidents1 = countReportedIncidents(records1);
int incidents2 = countReportedIncidents(records2);
return incidents1 > incidents2;
}
}
34 changes: 33 additions & 1 deletion src/main/java/codingcompetition2019/DisasterDescription.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package codingcompetition2019;

import java.util.List;

public class DisasterDescription {
// TODO finish this class
private String category;
private String countryCode;
private String year;
private int numIncidents;

public DisasterDescription(String category, String countryCode, String year, int numIncidents) {
this.category = category;
this.countryCode = countryCode;
this.year = year;
this.numIncidents = numIncidents;
}

public DisasterDescription(List<String> csvRow) {
this(csvRow.get(0), csvRow.get(1), csvRow.get(2), Integer.parseInt(csvRow.get(3)));
}

public String getCategory() {
return this.category;
}

public String getCountryCode() {
return this.countryCode;
}

public String getYear() {
return this.year;
}

public int getReportedIncidentsNum() {
return this.numIncidents;
}
}
51 changes: 51 additions & 0 deletions src/main/java/codingcompetition2019/webui/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package codingcompetition2019.webui;

import codingcompetition2019.CodingCompCSVUtil;
import codingcompetition2019.DisasterDescription;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static spark.Spark.*;
public class Main {
private static final String naturalDisasterByTypeFile = "src/main/resources/natural-disasters-by-type.csv";
private static final String significantEarthquakeFile = "src/main/resources/significant-earthquakes.csv";
private static final String significantVolanicEruptionsFile = "src/main/resources/significant-volcanic-eruptions.csv";

public static String loadJson(String fileName) throws IOException {
Gson gson = new Gson();
CodingCompCSVUtil util = new CodingCompCSVUtil();
List<List<String>> records = util.readCSVFileWithoutHeaders(fileName);
List<DisasterDescription> dds = records.stream().map(DisasterDescription::new).collect(Collectors.toList());
return gson.toJson(dds);
}

public static void main(String[] args) throws IOException {
String typeJson = loadJson(naturalDisasterByTypeFile);
String earthquakeJson = loadJson(significantEarthquakeFile);
String volcanoJson = loadJson(significantVolanicEruptionsFile);


System.out.println("Running on: http://localhost:4567");

staticFiles.location("html");
//staticFiles.externalLocation("/home/hsheth/data/projects/2019-StateFarm-CodingCompetitionProblem-Private/src/main/resources/html");
get("/api/disasters_by_type", (req, res) -> {
res.type("application/json");
return typeJson;
});

get("/api/significant_earthquakes", (req, res) -> {
res.type("application/json");
return earthquakeJson;
});

get("/api/significant_volcanic_eruptions", (req, res) -> {
res.type("application/json");
return volcanoJson;
});
}

}
Loading