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