Created
April 18, 2024 11:29
-
-
Save sidd-kulk/a7e284c4e8ac808fb33244b7017bbaa6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class BRC { | |
public static void main(String[] args) throws FileNotFoundException { | |
long millis = System.currentTimeMillis(); | |
System.out.println(calculateMinMeanMaxPerStation(readCSV())); | |
System.out.println("Time taken = " + (System.currentTimeMillis() - millis)); | |
} | |
static Map<String, TemperatureReslt> calculateMinMeanMaxPerStation(List<CsvRecord> records) { | |
final Map<String, TemperatureReslt> result = new TreeMap<>(); | |
for (CsvRecord record : records) { | |
if (!result.containsKey(record.city())) { | |
result.put(record.city(), new TemperatureReslt(record.temperature(), record.temperature(), record.temperature())); | |
continue; | |
} | |
TemperatureReslt tr = result.get(record.city()); | |
double min = Math.min(tr.min(), record.temperature()); | |
double mean = (tr.mean() + record.temperature()) / 2.0; | |
double max = Math.max(tr.max(), record.temperature()); | |
result.put(record.city(), new TemperatureReslt(min, mean, max)); | |
} | |
return result; | |
} | |
static List<CsvRecord> readCSV() throws FileNotFoundException { | |
List<CsvRecord> records = new ArrayList<>(1999999999); | |
try (BufferedReader br = new BufferedReader(new FileReader("temperature.csv"))) { | |
String line; | |
while ((line = br.readLine()) != null) { | |
String[] values = line.split(";"); | |
if (values.length != 2) continue; | |
records.add(new CsvRecord(values[0], Double.parseDouble(values[1]))); | |
} | |
} catch (Exception e) { | |
System.err.println(e); | |
} | |
return records; | |
} | |
} | |
record CsvRecord(String city, double temperature) { | |
} | |
record TemperatureReslt(double min, double mean, double max) { | |
@Override | |
public String toString() { | |
return "<" + min + "," + mean + "," + max + ">"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import java.util.*; | |
class BRCTest { | |
private static final String[] stations = new String[]{"Hamburg", "Bulawayo", "Palembang", "St", "Cracow", "Bridgetown", "Istanbul", "Roseau", "Hamburg", "Bulawayo", "Palembang", "St", "Cracow", "Bridgetown", "Istanbul", "Roseau"}; | |
private static final double[] temperatures = new double[]{12.0, 8.9, 38.8, 15.2, 12.6, 56.9, 6.2, 34.4, 10.0, 7.9, 6.8, 2.2, 2.6, 6.9, 22.2, 4.4}; | |
private static final TemperatureResult[] results = new TemperatureResult[]{new TemperatureResult(6.9,31.9,56.9),new TemperatureResult(7.9,8.4,8.9),new TemperatureResult(2.6,7.6,12.6),new TemperatureResult(10,11,12),new TemperatureResult(6.2,14.2,22.2),new TemperatureResult(6.8,22.8,38.8),new TemperatureResult(4.4,19.4,34.4),new TemperatureResult(2.2,8.7,15.2)}; | |
@Test | |
void calculateMinMeanMaxPerStation() { | |
TreeMap<String, TemperatureResult> result = BRC.calculateMinMeanMaxPerStation(getCsvRecord()); | |
var uniqueSortedStations = Arrays.stream(stations).distinct().sorted().toArray(); | |
Assertions.assertEquals(uniqueSortedStations.length, result.size()); // Assert the Size as early check | |
int counter = 0; | |
for (Map.Entry<String, TemperatureResult> entry : result.entrySet()) { | |
String key = entry.getKey(); | |
TemperatureResult value = entry.getValue(); | |
Assertions.assertEquals(key, uniqueSortedStations[counter]); // Assert the Order of Elements | |
Assertions.assertEquals(results[counter], value); // Assert the min, mean and max values | |
counter++; // TODO: Better way than this? | |
} | |
} | |
private List<CsvRecord> getCsvRecord() { | |
final List<CsvRecord> records = new ArrayList<>(stations.length); | |
for (int i = 0; i < stations.length; i++) { | |
records.add(new CsvRecord(stations[i], temperatures[i])); | |
} | |
return records; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment