yauhenip 6 лет назад
Родитель
Сommit
9b177e7c1e

+ 58
- 8
src/main/java/io/zipcoder/ArrayDrills.java Просмотреть файл

@@ -10,7 +10,11 @@ public class ArrayDrills {
10 10
      *           firstLast(6, [1,2,3]); // Should return false
11 11
      */
12 12
     public Boolean firstLast(Integer value, Integer[] input){
13
-        return null;
13
+        boolean isTrue = false;
14
+        if (input[0] == value || input[input.length-1] == value) {
15
+            isTrue = true;
16
+        }
17
+        return isTrue;
14 18
     }
15 19
 
16 20
     /**
@@ -19,7 +23,11 @@ public class ArrayDrills {
19 23
      *           sameFirstLast([1,2,1]); // Should return true
20 24
      */
21 25
     public Boolean sameFirstLast(Integer[] input){
22
-        return null;
26
+        boolean isTrue = false;
27
+        if (input.length >= 1 && input[0] == input[input.length-1]) {
28
+            isTrue = true;
29
+        }
30
+        return isTrue;
23 31
     }
24 32
 
25 33
 
@@ -30,7 +38,11 @@ public class ArrayDrills {
30 38
      *           commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
31 39
      */
32 40
     public Boolean commonEnd(Integer[] input1, Integer[] input2){
33
-        return null;
41
+        boolean isTrue = false;
42
+        if (input1[0] == input2[0] || input1[input1.length-1] == input2[input2.length-1]) {
43
+            isTrue = true;
44
+        }
45
+        return isTrue;
34 46
     }
35 47
 
36 48
     /**
@@ -39,7 +51,12 @@ public class ArrayDrills {
39 51
      *           rotateLeft([5, 11, 9]); // Should return [11,9,5]
40 52
      */
41 53
     public Integer[] rotateLeft(Integer[] input){
42
-        return null;
54
+        Integer[] rotatedLeftArray = new Integer[input.length];
55
+        for (int i=1; i<input.length; i++) {
56
+            rotatedLeftArray[i-1] = input[i];
57
+        }
58
+        rotatedLeftArray[input.length-1] = input[0];
59
+        return rotatedLeftArray;
43 60
     }
44 61
 
45 62
 
@@ -50,7 +67,17 @@ public class ArrayDrills {
50 67
      *           maxValue([5, 11, 9]); // Should return [11,11,11]
51 68
      */
52 69
     public Integer[] maxValue(Integer[] input){
53
-        return null;
70
+        Integer[] changedArray = new Integer[input.length];
71
+        int largestElement = input[0];
72
+        for (int i=1; i<input.length; i++) {
73
+            if (input[i] > largestElement) {
74
+                largestElement = input[i];
75
+            }
76
+        }
77
+        for (int i=0; i<input.length; i++) {
78
+            changedArray[i] = largestElement;
79
+        }
80
+        return changedArray;
54 81
     }
55 82
 
56 83
 
@@ -61,7 +88,20 @@ public class ArrayDrills {
61 88
      *           middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
62 89
      */
63 90
     public Integer[] middleWay(Integer[] input1, Integer[] input2){
64
-        return null;
91
+        Integer[] middleWayArray = new Integer[2];
92
+        middleWayArray[0] = checkArrayLength(input1);
93
+        middleWayArray[1] = checkArrayLength(input2);
94
+        return middleWayArray;
95
+    }
96
+
97
+    public Integer checkArrayLength (Integer[] input) {
98
+        Integer returnedValue;
99
+        if (input.length%2 == 1) {
100
+            returnedValue = input[input.length/2];
101
+        } else {
102
+            returnedValue = input[input.length/2] + input[input.length/2-1];
103
+        }
104
+        return returnedValue;
65 105
     }
66 106
 
67 107
 
@@ -71,7 +111,13 @@ public class ArrayDrills {
71 111
      * Return the array which has the largest sum. In event of a tie, return a.
72 112
      */
73 113
     public Integer[] biggerTwo(Integer[] a, Integer[] b){
74
-        return null;
114
+        Integer[] returnedArray;
115
+        if (a[0] + a[1] >= b[0] + b[1]) {
116
+            returnedArray = a;
117
+        } else {
118
+            returnedArray = b;
119
+        }
120
+        return returnedArray;
75 121
     }
76 122
 
77 123
     /**
@@ -81,6 +127,10 @@ public class ArrayDrills {
81 127
      *           midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
82 128
      */
83 129
     public Integer[] midThree(Integer[] nums){
84
-        return null;
130
+        Integer[] midThreeArray = new Integer[3];
131
+        midThreeArray[0] = nums[nums.length/2-1];
132
+        midThreeArray[1] = nums[nums.length/2];
133
+        midThreeArray[2] = nums[nums.length/2+1];
134
+        return midThreeArray;
85 135
     }
86 136
 }

+ 1
- 1
src/test/java/io/zipcoder/ArrayDrillsTest.java Просмотреть файл

@@ -179,7 +179,7 @@ public class ArrayDrillsTest {
179 179
     public void biggerTwo3(){
180 180
         Integer[] inputArray1 = {-1 ,20};
181 181
         Integer[] inputArray2 = {2, 15};
182
-        Integer[] expected = {1,20};
182
+        Integer[] expected = {-1,20};
183 183
         Integer[] actual = arrayDrills.biggerTwo(inputArray1, inputArray2);
184 184
         Assert.assertArrayEquals(expected, actual);
185 185
     }