|
@@ -1,5 +1,7 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+
|
3
|
5
|
public class ArrayDrills {
|
4
|
6
|
|
5
|
7
|
|
|
@@ -10,7 +12,8 @@ public class ArrayDrills {
|
10
|
12
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
13
|
*/
|
12
|
14
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
15
|
+ boolean answer = (input[0] == value || value.equals(input[input.length-1])) ? true : false;
|
|
16
|
+ return answer;
|
14
|
17
|
}
|
15
|
18
|
|
16
|
19
|
/**
|
|
@@ -19,7 +22,8 @@ public class ArrayDrills {
|
19
|
22
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
23
|
*/
|
21
|
24
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
25
|
+ boolean answer = (input.length >= 1 && input[0].equals(input[input.length-1])) ? true : false;
|
|
26
|
+ return answer;
|
23
|
27
|
}
|
24
|
28
|
|
25
|
29
|
|
|
@@ -30,7 +34,8 @@ public class ArrayDrills {
|
30
|
34
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
35
|
*/
|
32
|
36
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
37
|
+ boolean answer = (input1[input1.length-1].equals(input2[input2.length-1])) ? true : false;
|
|
38
|
+ return answer;
|
34
|
39
|
}
|
35
|
40
|
|
36
|
41
|
/**
|
|
@@ -39,7 +44,12 @@ public class ArrayDrills {
|
39
|
44
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
45
|
*/
|
41
|
46
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
47
|
+ Integer [] output = new Integer[input.length];
|
|
48
|
+ output[output.length-1] = input[0];
|
|
49
|
+ for(int x =0; x < input.length-1; x++){
|
|
50
|
+ output[x] = input[x+1];
|
|
51
|
+ }
|
|
52
|
+ return output;
|
43
|
53
|
}
|
44
|
54
|
|
45
|
55
|
|
|
@@ -50,7 +60,12 @@ public class ArrayDrills {
|
50
|
60
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
61
|
*/
|
52
|
62
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
63
|
+ Arrays.sort(input);
|
|
64
|
+ Integer [] output = new Integer[input.length];
|
|
65
|
+ for(int x=0; x < output.length; x++){
|
|
66
|
+ output[x] = input[input.length-1];
|
|
67
|
+ }
|
|
68
|
+ return output;
|
54
|
69
|
}
|
55
|
70
|
|
56
|
71
|
|
|
@@ -61,7 +76,20 @@ public class ArrayDrills {
|
61
|
76
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
77
|
*/
|
63
|
78
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
79
|
+ Integer [] output = new Integer[2];
|
|
80
|
+ if (input1.length%2==0){
|
|
81
|
+ output[0] = ((input1[(input1.length/2) -1]) + (input1[(input1.length/2)]));
|
|
82
|
+ }
|
|
83
|
+ else
|
|
84
|
+ output[0] = input1[input1.length/2];
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+ if (input2.length%2==0){
|
|
88
|
+ output[1] = ((input2[(input2.length/2)-1]) + (input2[(input1.length/2) +1 ]));
|
|
89
|
+ }
|
|
90
|
+ else
|
|
91
|
+ output[1] = input2[input1.length/2];
|
|
92
|
+ return output;
|
65
|
93
|
}
|
66
|
94
|
|
67
|
95
|
|
|
@@ -71,7 +99,8 @@ public class ArrayDrills {
|
71
|
99
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
100
|
*/
|
73
|
101
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
102
|
+ Integer [] c = ((a[0] + a[1]) > (b[0] + b[1])) ? a : b;
|
|
103
|
+ return c;
|
75
|
104
|
}
|
76
|
105
|
|
77
|
106
|
/**
|
|
@@ -81,6 +110,11 @@ public class ArrayDrills {
|
81
|
110
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
111
|
*/
|
83
|
112
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
113
|
+ Integer [] output = new Integer[3];
|
|
114
|
+ output[0] = nums[(nums.length/2) -1];
|
|
115
|
+ output[1] = nums[(nums.length/2)];
|
|
116
|
+ output[2] = nums[(nums.length/2)+1];
|
|
117
|
+
|
|
118
|
+ return output;
|
85
|
119
|
}
|
86
|
120
|
}
|