diff --git a/uil/aplus-february-2015/7/MMM.MD b/uil/aplus-february-2015/7/MMM.MD new file mode 100644 index 0000000..7901cde --- /dev/null +++ b/uil/aplus-february-2015/7/MMM.MD @@ -0,0 +1,19 @@ +# MMM + +Run my solution on **[repl.it](https://repl.it/@Xevion/A-Computer-Science-February-2015-MMM)!** + +Despite this being one of the easier problems, it took much longer than it should have to solve. + +This is mostly due to the `.median(int[] values)` method being programmed incorrectly, as if you're not familiar, or need a quick refresher: Median acts differently depending on whether or not there are a even or odd amount of numbers in the list. + +When odd, it reports the center, which is easy enough, but when it's even, the average of the two centermost numbers is taken, which ended up being harder for me to program as I initially did not notice. + +Simply put, the two values that were being averaged was shifted by 1 index, meaning the wrong values were selected. + +Since I programmed the center to be calculated before a if statement concerning how the median is calculated is seen, I shifted the center over, which made the even-length Median(s) correct, but now the odd-length Median(s) incorrect. + +This ends up making the `MMM` statistic incorrect too. + +Remember to call `Arrays.sort` on the median values list, and use floating point arguments to correctly round the numbers (and effectively print quickly without a bunch of excessive string concatenations)! + +This was a excessively long explanation for the problem. Sorry. \ No newline at end of file diff --git a/uil/aplus-february-2015/7/MMM.java b/uil/aplus-february-2015/7/MMM.java new file mode 100644 index 0000000..6e2ee68 --- /dev/null +++ b/uil/aplus-february-2015/7/MMM.java @@ -0,0 +1,72 @@ +import static java.lang.System.out; +import java.util.Scanner; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Collections; +import java.io.File; +import java.io.IOException; + +class Main { + public static void main(String[] args) throws IOException { + Scanner s = new Scanner(new File("mmm.dat")); + while(s.hasNextLine()) { + String line = s.nextLine(); + Scanner r = new Scanner(line); + int i = 0; + while(r.hasNextDouble()) { + i++; + r.nextDouble(); + } + double[] arr = new double[i]; + r = new Scanner(line); + i = 0; + while(r.hasNextDouble()) + arr[i++] = r.nextDouble(); + out.println(String.format("%.2f %.2f %.2f %.2f", mean(arr), median(arr), mode(arr), mmm(arr))); + } + } + + public static double mean(double[] values) { + double sum = 0; + for(double value : values) + sum += value; + return sum / values.length; + } + + // 1 2 3 4 5 6 + // ^ ^ mid = (7/2) - 1 + // med = ([mid]+[mid+1]) / 2 + public static double median(double[] values) { + Arrays.sort(values); + int center = values.length / 2 - 1; + if(values.length % 2 == 0) + return (values[center] + values[center + 1]) / 2.0; + else + return values[center + 1]; + } + + public static double mode(double[] values) { + if(values.length == 0) + return -1.0; + HashMap map = new HashMap(); + int max = 1; + double temp = values[0]; + for(double value : values) { + if(map.get(value) == null) { + map.put(value, 1); + } else { + int cur = map.get(value); + map.put(value, ++cur); + if(max < cur) { + max = cur; + temp = value; + } + } + } + return temp; + } + + public static double mmm(double[] values) { + return (mean(values) + median(values) + mode(values)) / 3.0; + } +} \ No newline at end of file