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
13 changes: 8 additions & 5 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): Sidhartha Chaganti
How many JUnits were you able to get to pass? 10/10 tests

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

I added a method to CodingCompCSVUtil.java which is called averageNumberOfReportedIncidentsPerYearByCategory which returns an ArrayList of DisasterDescription entries which are unique by category, each of which contain the average number of reportedIncidents per year. To do this, I added a field to DisasterDescription called avgIncidents which is a float field that contains the average number of incidents. Then, I used a HashSet in the method to collect the unique categories in the records List of List of Strings that was provided in the parameters. Afterwards, I iterated through each unique category, found the total number of reported incidents for each category, and divided by the total number of entries for each category. Then, I created a DisasterDescription object which contains the unique category and the respective average number of reported incidents per year and added it to the finalList ArrayList. Then, I returned the ArrayList finalList for further processing as the user sees fit.

I think that averageNumberOfReportedIncidents is a useful method which can be used to identify average number of incidents per year for each natural disaster or to identify the average number of incidents per year for each country for a given disaster. I think that there is a lot of use for this method using the different types of data provided.


Feedback for the coding competition? Things you would like to see in future events?
Feedback for the coding competition? Things you would like to see in future events?
I enjoyed working on this project, especially having to discover what each method did on my own by reading the code in the test file and by looking at the return types. Overall, I enjoyed it and understand why there wasn't documentation for each method in the source files (it would be too easy)
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down
202 changes: 183 additions & 19 deletions src/main/java/codingcompetition2019/CodingCompCSVUtil.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,183 @@
package codingcompetition2019;

import java.io.IOException;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
* A class holding methods to analyze csv files of disasters
* @author Sidhartha Chaganti, [email protected]
* @version 1.0.0
*/
public class CodingCompCSVUtil {

/**
* Uses a HashSet to find the unique categories in a given records List of List of Strings and iterates through them to find the average number of reported incidents per year by category
* @param records The List of List of Strings containing the disaster entries
* @return a List of DisasterDescription instances, each containing the average number of disaster incidents per year for their category
*/
public List<DisasterDescription> averageNumberOfReportedIncidentsPerYearByCategory(List<List<String>> records) {
List<DisasterDescription> finalList = new ArrayList<DisasterDescription>();
Set<String> categorySet = new HashSet<String>();
for (List<String> entry : records) {
categorySet.add(entry.get(0));
}
for (String category : categorySet) {
int totalForCategory = 0;
int numEntries = 0;
for (List<String> entry : records) {
if (entry.get(0).equals(category)) {
totalForCategory += Integer.parseInt(entry.get(3));
}
numEntries++;
}
double avgIncidents = (double) (totalForCategory) / numEntries;
DisasterDescription temp = new DisasterDescription(category, avgIncidents);
finalList.add(temp);
}
return finalList;
}

/**
* Reads a specified CSV file and returns only the entries pertaining to a specified country
* @param fileName The csv file to be read
* @param countryName The name of the country with the disasters we are searching for
* @return A List of List of Strings containing the entries of the disasters from a particular country
*/
public List<List<String>> readCSVFileByCountry(String fileName, String countryName) throws IOException {
// TODO implement this method
return null;
List<List<String>> finalList = new ArrayList<List<String>>();
BufferedReader csvReader = new BufferedReader(new FileReader(fileName));
String row = csvReader.readLine();
while ( (row = csvReader.readLine()) != null ) {
String[] data = row.split(",");
if (data[0].equals(countryName)) {
List<String> temp = new ArrayList<String>();
for (String d : data) {
temp.add(d);
}
finalList.add(temp);
}
}

csvReader.close();
return finalList;
}

/**
* Reads a CSV file and returns all entries
* @param fileName The csv file to be read
* @return A List of List of Strings of all entries from the csv file
*/
public List<List<String>> readCSVFileWithHeaders(String fileName) throws IOException {
// TODO implement this method
return null;
List<List<String>> finalList = new ArrayList<List<String>>();
BufferedReader csvReader = new BufferedReader(new FileReader(fileName));
String row;
while ( (row = csvReader.readLine()) != null ) {
String[] data = row.split(",");
List<String> temp = new ArrayList<String>();
for (String d : data) {
temp.add(d);
}
finalList.add(temp);
}

csvReader.close();
return finalList;
}

/**
* Reads a CSV file and returns all entries except for the header entry
* @param fileName The csv file to be read
* @return A List of List of Strings of all entries from the csv file except the header entry
*/
public List<List<String>> readCSVFileWithoutHeaders(String fileName) throws IOException {
// TODO implement this method
return null;
List<List<String>> finalList = new ArrayList<List<String>>();
BufferedReader csvReader = new BufferedReader(new FileReader(fileName));
String row = csvReader.readLine();
while ( (row = csvReader.readLine()) != null ) {
String[] data = row.split(",");
List<String> temp = new ArrayList<String>();
for (String d : data) {
temp.add(d);
}
finalList.add(temp);
}

csvReader.close();
return finalList;
}

/**
* Finds and returns the DisasterDescriptionInstance of the record entry with the most impactful year (the year with the most incidents)
* @param records The List of List of Strings containing the disaster entries
* @return a DisasterDescription instance of the year with the most disaster incidents
*/
public DisasterDescription getMostImpactfulYear(List<List<String>> records) {
// TODO implement this method
return null;
// let first entry be the most impactful
List<String> first = records.get(0);
DisasterDescription mostImpactfulDisasterDescription = new DisasterDescription(first.get(0), first.get(2), Integer.parseInt(first.get(3)));
for (List<String> entry : records) {
int numIncidents = Integer.parseInt(entry.get(3));
if (numIncidents > mostImpactfulDisasterDescription.getReportedIncidentsNum()) {
// we need to make a new object
mostImpactfulDisasterDescription = new DisasterDescription(entry.get(0), entry.get(2), Integer.parseInt(entry.get(3)));
}
}
return mostImpactfulDisasterDescription;
}

/**
* Finds and returns the DisasterDescriptionInstance of the record entry with the most impactful year (the year with the most incidents) and the specified category
* @param category The category of disaster to filter records by
* @param records The List of List of Strings containing the disaster entries
* @return a DisasterDescription instance of the year with the most disaster incidents and the specified category
*/
public DisasterDescription getMostImpactfulYearByCategory(String category, List<List<String>> records) {
// TODO implement this method
return null;
// need to find the first instance of the category we want
DisasterDescription mostImpactfulDisasterDescription = new DisasterDescription(category, null, -1);
for (List<String> entry : records) {
int numIncidents = Integer.parseInt(entry.get(3));
if (entry.get(0).equals(category) && numIncidents > mostImpactfulDisasterDescription.getReportedIncidentsNum()) {
// we need to make a new object
mostImpactfulDisasterDescription = new DisasterDescription(entry.get(0), entry.get(2), Integer.parseInt(entry.get(3)));
}
}
return mostImpactfulDisasterDescription;
}

/**
* Returns the DisasterDescription instance of the most impactful type of disaster based on a specified year
* @param year The year to filter records by
* @param records The List of List of Strings containing the entries of disasters
* @return A DisasterDescription instance containing the category of the most impactful disaster during a specified year
*/
public DisasterDescription getMostImpactfulDisasterByYear(String year, List<List<String>> records) {
// TODO implement this method
return null;
DisasterDescription mostImpactfulDisasterDescription = new DisasterDescription(null, year, -1);
for (List<String> entry : records) {
int numIncidents = Integer.parseInt(entry.get(3));
if (entry.get(2).equals(year) && !(entry.get(0).equals("All natural disasters")) && numIncidents > mostImpactfulDisasterDescription.getReportedIncidentsNum()) {
// we need to make a new object
mostImpactfulDisasterDescription = new DisasterDescription(entry.get(0), entry.get(2), Integer.parseInt(entry.get(3)));
}
}
return mostImpactfulDisasterDescription;
}

/**
* Returns a DisasterDescription instance of the total reported incidents of a disaster category
* @param category The category to filter records by to find the total number of reported incidents
* @param records The List of List of Strings containing the disaster entries
* @return a DisasterDescription containing the total number of incidents from a specified category
*/
public DisasterDescription getTotalReportedIncidentsByCategory(String category, List<List<String>> records) {
// TODO implement this method
return null;
int totalForCategory = 0;
for (List<String> entry : records) {
if (entry.get(0).equals(category)) {
totalForCategory += Integer.parseInt(entry.get(3));
}
}
return new DisasterDescription(category, null, totalForCategory);
}

/**
Expand All @@ -45,14 +186,37 @@ public DisasterDescription getTotalReportedIncidentsByCategory(String category,
* + 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.
* @param records The List of List of Strings containing the disaster entries
* @param min The minimum number of incidents to consider in the total (inclusive)
* @param max The maximum number of incidents to consider in the total (inclusive) (Can be -1, which indicates no max and retrieve all values above minimum)
* @return The count of impactful years within the specified range
*/
public int countImpactfulYearsWithReportedIncidentsWithinRange(List<List<String>> records, int min, int max) {
// TODO implement this method
return -1;
int total = 0;
boolean hasMax = (max == -1) ? false : true;

for (List<String> entry : records) {
int entryNum = Integer.parseInt(entry.get(3));
if (hasMax) {
if (entryNum >= min && entryNum <= max) {
total += 1;
}
} else {
if (entryNum >= min) {
total += 1;
}
}
}
return total;
}

/**
* Returns whether the first records List contains more reported incidents than the second records List
* @param records1 The first List of List of Strings containing disaster entries
* @param records2 The second List of List of Strings continaining disaster entries
* @return A boolean of whether records1 contains more reported incidents than records2
*/
public boolean firstRecordsHaveMoreReportedIndicents(List<List<String>> records1, List<List<String>> records2) {
// TODO implement this method
return false;
return countImpactfulYearsWithReportedIncidentsWithinRange(records1, 1, -1) > countImpactfulYearsWithReportedIncidentsWithinRange(records2, 1, -1);
}
}
64 changes: 63 additions & 1 deletion src/main/java/codingcompetition2019/DisasterDescription.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
package codingcompetition2019;

/**
* A class representing the DisasterDescription object
* @author Sidhartha Chaganti, [email protected]
* @version 1.0.0
*/
public class DisasterDescription {
// TODO finish this class
private String year;
private String category;
private int reportedIncidentsNum;
private double avgIncidents;

/**
* Constructs a DisasterDescription object with the fields for the basic requirements
* @param category The category of the disaster(s)
* @param year The year the disaster(s) took place
* @param reportedIncidentsNum The number of disasters of this type occured in this year
*/
public DisasterDescription(String category, String year, int reportedIncidentsNum) {
this.year = year;
this.category = category;
this.reportedIncidentsNum = reportedIncidentsNum;
}

/**
* Constructs a DisasterDescription object with the fields for the bonus feature of average number of incidents per year by category
* @param category The category of the disaster(s)
* @param avgIncidents The average number of incidents per year for this category
*/
public DisasterDescription(String category, double avgIncidents) {
this.category = category;
this.avgIncidents = avgIncidents;
}

/**
* Returns the year of this instance
* @return The year of this instance
*/
public String getYear() {
return year;
}

/**
* Returns the category of this instance
* @return The category of this instance
*/
public String getCategory() {
return category;
}

/**
* Returns the number of reported incidents
* @return The number of reported incidents
*/
public int getReportedIncidentsNum() {
return reportedIncidentsNum;
}

/**
* Returns the average number of incidents per year for this category
* @return The average number of incidents
*/
public double getAvgIncidents() {
return avgIncidents;
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading