Skip to content

Instantly share code, notes, and snippets.

@kimki1124
Created August 6, 2017 13:24
Show Gist options
  • Save kimki1124/aac50fe44ee752278e69ba8a2946d244 to your computer and use it in GitHub Desktop.
Save kimki1124/aac50fe44ee752278e69ba8a2946d244 to your computer and use it in GitHub Desktop.
public static double bestPracticeMean(String town, String strng) {
return parseTemp(town, strng).stream()
.collect(averagingDouble(n -> n));
}
public static double bestPracticeVariance(String town, String strng) {
double mean = mean(town, strng);
if (mean == -1.0) return -1.0;
return parseTemp(town, strng).stream()
.collect(averagingDouble(n -> (n - mean) * (n - mean)));
}
private static List<Double> parseTemp(String town, String strng) {
List<Double> temps = new ArrayList<>();
for (String line : strng.split("\\n")) {
String[] data = line.split(":");
if (town.equals(data[0])) {
for (String weather : data[1].split(",")) {
String[] temp = weather.split("\\s");
temps.add(Double.parseDouble(temp[1]));
}
break;
}
}
if (temps.isEmpty()) temps.add(-1.0);
return temps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment