|
| 1 | +import java.util.Random; |
| 2 | + |
1 | 3 | public class Tester { |
2 | 4 | final static String GOOD = "\t\t\t OK!"; |
3 | 5 | final static String BAD = "\t\t\t *** Bummer you're wrong! ***"; |
@@ -30,12 +32,72 @@ public static void main(String[] args) { |
30 | 32 |
|
31 | 33 | /* Question 2 */ |
32 | 34 | System.out.println("Question 2: "); |
33 | | - boolean biggestEvenSum = (ex14.what(arr) == 12) && (ex14.what(arr2) == 8) && (ex14.what(arr3) == 8) && |
34 | | - (ex14.what(arr4) == 7) && (ex14.what(arr7) == 20); |
35 | | - if(biggestEvenSum) |
| 35 | + int[] temp = checkWTF(); |
| 36 | + if(temp == null) |
36 | 37 | System.out.println(GOOD); |
37 | | - else |
| 38 | + else { |
38 | 39 | System.out.println(BAD); |
| 40 | + System.out.println("\t\t\t The Array: " + printArr(temp)); |
| 41 | + System.out.println("\t\t\t Expected: " + what(temp)); |
| 42 | + System.out.println("\t\t\t Your stupid method: " + ex14.what(temp)); |
| 43 | + } |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + public static String printArr(int[] arr){ |
| 48 | + String ar = ""; |
| 49 | + for (int i = 0; i < arr.length; i++) { |
| 50 | + ar += arr[i] + ", "; |
| 51 | + } |
| 52 | + return ar; |
| 53 | + } |
| 54 | + |
| 55 | + public static int[][] generateArrays(){ |
| 56 | + int[][] mat = new int[10][]; |
| 57 | + Random rnd = new Random(); |
39 | 58 |
|
| 59 | + for (int i = 0; i < mat.length; i++) { |
| 60 | + mat[i] = new int[rnd.nextInt(30)]; |
| 61 | + for (int j = 0; j < mat[i].length; j++) { |
| 62 | + mat[i][j] = rnd.nextInt(200); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return mat; |
| 67 | + } |
| 68 | + |
| 69 | + public static int[] checkWTF(){ |
| 70 | + Ex14 ex14 = new Ex14(); |
| 71 | + int[][] mat = generateArrays(); |
| 72 | + for (int i = 0; i < mat.length; i++) { |
| 73 | + if(ex14.what(mat[i]) != what(mat[i])) |
| 74 | + return mat[i]; |
| 75 | + } |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + private static int f (int[]a, int low, int high) |
| 80 | + { |
| 81 | + int res = 0; |
| 82 | + for (int i=low; i<=high; i++) |
| 83 | + res += a[i]; |
| 84 | + return res; |
| 85 | + } |
| 86 | + public static int what (int []a) |
| 87 | + { |
| 88 | + int temp = 0; |
| 89 | + for (int i=0; i<a.length; i++) |
| 90 | + { |
| 91 | + for (int j=i; j<a.length; j++) |
| 92 | + { |
| 93 | + int c = f(a, i, j); |
| 94 | + if (c%2 == 0) |
| 95 | + { |
| 96 | + if (j-i+1 > temp) |
| 97 | + temp = j-i+1; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return temp; |
40 | 102 | } |
41 | 103 | } |
0 commit comments