|
@@ -1,5 +1,10 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import com.sun.tools.corba.se.idl.InterfaceGen;
|
|
4
|
+
|
|
5
|
+import java.util.Arrays;
|
|
6
|
+
|
|
7
|
+
|
3
|
8
|
public class ArrayDrills {
|
4
|
9
|
|
5
|
10
|
|
|
@@ -10,7 +15,10 @@ public class ArrayDrills {
|
10
|
15
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
16
|
*/
|
12
|
17
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
18
|
+ if(value.equals(input[0]) || value.equals(input[input.length - 1])){
|
|
19
|
+ return true;
|
|
20
|
+ }
|
|
21
|
+ else return false;
|
14
|
22
|
}
|
15
|
23
|
|
16
|
24
|
/**
|
|
@@ -18,8 +26,11 @@ public class ArrayDrills {
|
18
|
26
|
* example : sameFirstLast([1,2,3]); // Should return false
|
19
|
27
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
28
|
*/
|
21
|
|
- public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
29
|
+ public Boolean sameFirstLast(Integer[] input) {
|
|
30
|
+ if (input.length >= 1 && input[0] == input[input.length - 1]) {
|
|
31
|
+ return true;
|
|
32
|
+ }
|
|
33
|
+ else return false;
|
23
|
34
|
}
|
24
|
35
|
|
25
|
36
|
|
|
@@ -30,7 +41,13 @@ public class ArrayDrills {
|
30
|
41
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
42
|
*/
|
32
|
43
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
44
|
+ if(input1[0] == input2[0]){
|
|
45
|
+ return true;
|
|
46
|
+ }
|
|
47
|
+ else if(input1[input1.length - 1] == input2[input2.length - 1]){
|
|
48
|
+ return true;
|
|
49
|
+ }
|
|
50
|
+ else return false;
|
34
|
51
|
}
|
35
|
52
|
|
36
|
53
|
/**
|
|
@@ -39,7 +56,8 @@ public class ArrayDrills {
|
39
|
56
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
57
|
*/
|
41
|
58
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
59
|
+ Integer[] newArray = {input[1], input[2], input[0]};
|
|
60
|
+ return newArray;
|
43
|
61
|
}
|
44
|
62
|
|
45
|
63
|
|
|
@@ -50,7 +68,17 @@ 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[] newArray = new Integer[input.length];
|
|
72
|
+ int max;
|
|
73
|
+ max = input[0];
|
|
74
|
+
|
|
75
|
+ for(int value : input){
|
|
76
|
+ if(value > max){
|
|
77
|
+ max = value;
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+ Arrays.fill(newArray,max);
|
|
81
|
+ return newArray;
|
54
|
82
|
}
|
55
|
83
|
|
56
|
84
|
|
|
@@ -61,7 +89,20 @@ public class ArrayDrills {
|
61
|
89
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
90
|
*/
|
63
|
91
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
92
|
+ Integer[] newArr = new Integer[2];
|
|
93
|
+ int half1 = input1.length/2;
|
|
94
|
+ int half2 = input2.length/2;
|
|
95
|
+
|
|
96
|
+ if(input1.length % 2 == 0){
|
|
97
|
+ newArr[0]= input1[half1-1] + input1[half1];
|
|
98
|
+ }
|
|
99
|
+ else newArr[0] = input1[half1];
|
|
100
|
+
|
|
101
|
+ if(input2.length % 2 == 0){
|
|
102
|
+ newArr[1] = input2[half2-1] + input2[half2];
|
|
103
|
+ }
|
|
104
|
+ else newArr[1] = input2[half2];
|
|
105
|
+ return newArr;
|
65
|
106
|
}
|
66
|
107
|
|
67
|
108
|
|
|
@@ -70,8 +111,12 @@ public class ArrayDrills {
|
70
|
111
|
* Consider the sum of the values in each array.
|
71
|
112
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
113
|
*/
|
|
114
|
+ //Was test supposed to have -1? Changed it already btw
|
73
|
115
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
116
|
+ if(a[0] + a[1] > b[0] + b[1]){
|
|
117
|
+ return a;
|
|
118
|
+ }
|
|
119
|
+ else return b;
|
75
|
120
|
}
|
76
|
121
|
|
77
|
122
|
/**
|
|
@@ -81,6 +126,12 @@ public class ArrayDrills {
|
81
|
126
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
127
|
*/
|
83
|
128
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
129
|
+ Integer[] newArr = new Integer[3];
|
|
130
|
+ int half = nums.length/2;
|
|
131
|
+ newArr[0] = nums[half - 1];
|
|
132
|
+ newArr[1] = nums[half];
|
|
133
|
+ newArr[2] = nums[half+1];
|
|
134
|
+ return newArr;
|
85
|
135
|
}
|
|
136
|
+
|
86
|
137
|
}
|