|
@@ -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,11 @@ 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
|
+
|
|
16
|
+ if (input[0] == value || input[input.length - 1] == value) {
|
|
17
|
+ return true;
|
|
18
|
+ }
|
|
19
|
+ return false;
|
14
|
20
|
}
|
15
|
21
|
|
16
|
22
|
/**
|
|
@@ -19,7 +25,10 @@ public class ArrayDrills {
|
19
|
25
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
26
|
*/
|
21
|
27
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
28
|
+ if (input [0] == input[input.length - 1]){
|
|
29
|
+ return true;
|
|
30
|
+ }
|
|
31
|
+ else return false;
|
23
|
32
|
}
|
24
|
33
|
|
25
|
34
|
|
|
@@ -30,7 +39,10 @@ public class ArrayDrills {
|
30
|
39
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
40
|
*/
|
32
|
41
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
42
|
+ if (input1[0] == input2[0] || input1[input1.length -1] == input2[input2.length - 1]) {
|
|
43
|
+ return true;
|
|
44
|
+ }
|
|
45
|
+ else return false;
|
34
|
46
|
}
|
35
|
47
|
|
36
|
48
|
/**
|
|
@@ -39,7 +51,13 @@ 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 nextValue = input[0];
|
|
55
|
+ for (int i = input.length - 1; i >= 0; i--) {
|
|
56
|
+ Integer temp = input[i];
|
|
57
|
+ input[i] = nextValue;
|
|
58
|
+ nextValue = temp;
|
|
59
|
+ }
|
|
60
|
+ return input;
|
43
|
61
|
}
|
44
|
62
|
|
45
|
63
|
|
|
@@ -50,7 +68,16 @@ public class ArrayDrills {
|
50
|
68
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
69
|
*/
|
52
|
70
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
71
|
+ Integer maxValue = null;
|
|
72
|
+ for (Integer x : input) {
|
|
73
|
+ if (maxValue == null || x > maxValue) {
|
|
74
|
+ maxValue = x;
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+ for (int i = 0; i < input.length; i++){
|
|
78
|
+ input[i] = maxValue;
|
|
79
|
+ }
|
|
80
|
+ return input;
|
54
|
81
|
}
|
55
|
82
|
|
56
|
83
|
|
|
@@ -61,9 +88,17 @@ 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[] returnArr = new Integer[2];
|
|
92
|
+ returnArr[0] = getMiddleValue(input1);
|
|
93
|
+ returnArr[1] = getMiddleValue(input2);
|
|
94
|
+ return returnArr;
|
|
95
|
+ }
|
|
96
|
+ private Integer getMiddleValue(Integer[] input) {
|
|
97
|
+ if (input.length % 2 == 0) {
|
|
98
|
+ return input[input.length/2] + input[(input.length/2) - 1];
|
|
99
|
+ }
|
|
100
|
+ else return input[input.length/2];
|
65
|
101
|
}
|
66
|
|
-
|
67
|
102
|
|
68
|
103
|
/**
|
69
|
104
|
* Start with 2 int arrays, a and b, each length 2.
|
|
@@ -71,7 +106,10 @@ public class ArrayDrills {
|
71
|
106
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
107
|
*/
|
73
|
108
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
109
|
+ if (a[0] + a[1] >= b[0] + b[1]) {
|
|
110
|
+ return a;
|
|
111
|
+ }
|
|
112
|
+ else return b;
|
75
|
113
|
}
|
76
|
114
|
|
77
|
115
|
/**
|
|
@@ -81,6 +119,10 @@ public class ArrayDrills {
|
81
|
119
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
120
|
*/
|
83
|
121
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
122
|
+ Integer[] midThree = new Integer[3];
|
|
123
|
+ midThree[0] = nums[nums.length/2 - 1];
|
|
124
|
+ midThree[1] = nums[nums.length/2];
|
|
125
|
+ midThree[2] = nums[nums.length/2 + 1];
|
|
126
|
+ return midThree;
|
85
|
127
|
}
|
86
|
128
|
}
|