|
@@ -1,5 +1,8 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.List;
|
|
5
|
+
|
3
|
6
|
public class ArrayDrills {
|
4
|
7
|
|
5
|
8
|
|
|
@@ -7,19 +10,29 @@ public class ArrayDrills {
|
7
|
10
|
* Given an array of ints, return true if value appears as either the first or last element in the array.
|
8
|
11
|
* The array will be length 1 or more.
|
9
|
12
|
* example : firstLast(6, [1,2,6); // Should return true
|
10
|
|
- * firstLast(6, [1,2,3]); // Should return false
|
|
13
|
+ * firstLast(6, [1,2,3]); // Should return false
|
11
|
14
|
*/
|
12
|
|
- public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
15
|
+ public Boolean firstLast(Integer value, Integer[] input) {
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+ if (value.equals(input[input.length - 1]) || value.equals(input[0])) return true;
|
|
19
|
+
|
|
20
|
+ else return false;
|
|
21
|
+
|
|
22
|
+
|
14
|
23
|
}
|
15
|
24
|
|
|
25
|
+
|
16
|
26
|
/**
|
17
|
27
|
* Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.
|
18
|
28
|
* example : sameFirstLast([1,2,3]); // Should return false
|
19
|
|
- * sameFirstLast([1,2,1]); // Should return true
|
|
29
|
+ * sameFirstLast([1,2,1]); // Should return true
|
20
|
30
|
*/
|
21
|
|
- public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
31
|
+ public Boolean sameFirstLast(Integer[] input) {
|
|
32
|
+ if (input.length > 1 && input[0].equals(input[input.length - 1])) return true;
|
|
33
|
+ else return false;
|
|
34
|
+
|
|
35
|
+
|
23
|
36
|
}
|
24
|
37
|
|
25
|
38
|
|
|
@@ -27,19 +40,29 @@ public class ArrayDrills {
|
27
|
40
|
* Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element.
|
28
|
41
|
* Both arrays will be length 1 or more.
|
29
|
42
|
* example : commonEnd([1, 2, 3], [7, 3]); // Should return true
|
30
|
|
- * commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
|
43
|
+ * commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
44
|
*/
|
32
|
|
- public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
45
|
+ public Boolean commonEnd(Integer[] input1, Integer[] input2) {
|
|
46
|
+ if (input1[0].equals(input2[0]) || input1[input1.length - 1].equals(input2[input2.length - 1])) return true;
|
|
47
|
+ else return false;
|
|
48
|
+
|
34
|
49
|
}
|
35
|
50
|
|
36
|
51
|
/**
|
37
|
52
|
* Given an array of ints, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
|
38
|
53
|
* example : rotateLeft([1, 2, 3]); // Should return [2,3,1]
|
39
|
|
- * rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
|
54
|
+ * rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
55
|
*/
|
41
|
|
- public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
56
|
+ public Integer[] rotateLeft(Integer[] input) {
|
|
57
|
+ Integer temp = input[0];
|
|
58
|
+
|
|
59
|
+ Integer[] newArr = new Integer[input.length];
|
|
60
|
+ for (int x = 0; x < input.length - 1; x++) {
|
|
61
|
+ newArr[x] = input[x + 1];
|
|
62
|
+ }
|
|
63
|
+ newArr[input.length - 1] = temp;
|
|
64
|
+
|
|
65
|
+ return newArr;
|
43
|
66
|
}
|
44
|
67
|
|
45
|
68
|
|
|
@@ -47,10 +70,26 @@ public class ArrayDrills {
|
47
|
70
|
* Given an array of ints, figure out which is element in the array is largest,
|
48
|
71
|
* and set all the other elements to be that value. Return the changed array.
|
49
|
72
|
* example : maxValue([1, 2, 3]); // Should return [3,3,3]
|
50
|
|
- * maxValue([5, 11, 9]); // Should return [11,11,11]
|
|
73
|
+ * maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
74
|
*/
|
52
|
|
- public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
75
|
+ public Integer[] maxValue(Integer[] input) {
|
|
76
|
+ Integer temp = input[0];
|
|
77
|
+
|
|
78
|
+ Integer[] newArr = new Integer[input.length];
|
|
79
|
+ for (Integer x = 0; x < input.length; x++) {
|
|
80
|
+
|
|
81
|
+ if (input[x] > temp) {
|
|
82
|
+ temp = input[x];
|
|
83
|
+
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ }
|
|
87
|
+ for (Integer z = 0; z < newArr.length; z++) {
|
|
88
|
+
|
|
89
|
+ newArr[z] = temp;
|
|
90
|
+ }
|
|
91
|
+ return newArr;
|
|
92
|
+
|
54
|
93
|
}
|
55
|
94
|
|
56
|
95
|
|
|
@@ -58,10 +97,28 @@ public class ArrayDrills {
|
58
|
97
|
* Given 2 int arrays, a and b, return a new array length 2 containing their middle elements, if length is odd.
|
59
|
98
|
* If the array length is even the sum of the middle 2 elements in the array.
|
60
|
99
|
* example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
|
61
|
|
- * middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
|
100
|
+ * middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
101
|
*/
|
63
|
|
- public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
102
|
+ public Integer[] middleWay(Integer[] input1, Integer[] input2) {
|
|
103
|
+ Integer[] newArr = new Integer[2];
|
|
104
|
+ newArr[0] = mid(input1);
|
|
105
|
+ newArr[1] = mid(input2);
|
|
106
|
+
|
|
107
|
+ return newArr;
|
|
108
|
+
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ public Integer mid(Integer[] input) {
|
|
112
|
+ int value = 0;
|
|
113
|
+ if (input.length % 2 == 0) {
|
|
114
|
+ value = input[input.length / 2] + input[(input.length / 2) - 1];
|
|
115
|
+
|
|
116
|
+ } else {
|
|
117
|
+ value = input[input.length / 2];
|
|
118
|
+
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ return value;
|
65
|
122
|
}
|
66
|
123
|
|
67
|
124
|
|
|
@@ -70,17 +127,45 @@ public class ArrayDrills {
|
70
|
127
|
* Consider the sum of the values in each array.
|
71
|
128
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
129
|
*/
|
73
|
|
- public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
130
|
+ public Integer[] biggerTwo(Integer[] a, Integer[] b) {
|
|
131
|
+
|
|
132
|
+ int count1 = 0;
|
|
133
|
+ for (int x = 0; x < a.length; x++) {
|
|
134
|
+ count1 += a[x];
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ int count2 = 0;
|
|
138
|
+ for (int z = 0; z < b.length; z++) {
|
|
139
|
+ count2 += b[z];
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ if (count1 <= count2) {
|
|
143
|
+ return b;
|
|
144
|
+ } else {
|
|
145
|
+ return a;
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ //return null;
|
75
|
149
|
}
|
76
|
150
|
|
|
151
|
+
|
77
|
152
|
/**
|
78
|
153
|
* Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array.
|
79
|
154
|
* The array length will be at least 3.
|
80
|
155
|
* example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
|
81
|
|
- * midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
|
156
|
+ * midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
157
|
*/
|
83
|
|
- public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
158
|
+ public Integer[] midThree(Integer[] nums) {
|
|
159
|
+ Integer[] newArr = new Integer[3];
|
|
160
|
+ Integer x = nums.length /2;
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+ newArr[0] = nums[x - 1];
|
|
164
|
+ newArr[1] = nums[x];
|
|
165
|
+ newArr[2] = nums[x + 1];
|
|
166
|
+
|
|
167
|
+ return newArr;
|
|
168
|
+ }
|
|
169
|
+
|
85
|
170
|
}
|
86
|
|
-}
|
|
171
|
+
|