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