|
@@ -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,12 @@ 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
|
+ int first = input[0];
|
|
16
|
+ int last = input[input.length-1];
|
|
17
|
+ boolean containsValue = false;
|
|
18
|
+ if(value == first || value == last){
|
|
19
|
+ containsValue = true;}
|
|
20
|
+ return containsValue;
|
14
|
21
|
}
|
15
|
22
|
|
16
|
23
|
/**
|
|
@@ -19,7 +26,13 @@ public class ArrayDrills {
|
19
|
26
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
27
|
*/
|
21
|
28
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
29
|
+ int first = input[0];
|
|
30
|
+ int last = input[input.length-1];
|
|
31
|
+ boolean meetsCondition = false;
|
|
32
|
+ if (input.length>1 && (first ==last)){
|
|
33
|
+ meetsCondition = true;
|
|
34
|
+ }
|
|
35
|
+ return meetsCondition;
|
23
|
36
|
}
|
24
|
37
|
|
25
|
38
|
|
|
@@ -30,7 +43,10 @@ public class ArrayDrills {
|
30
|
43
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
44
|
*/
|
32
|
45
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
46
|
+ boolean meetsCondition = false;
|
|
47
|
+ if(input1[0] == input2[0]){meetsCondition = true;}
|
|
48
|
+ else if (input1[input1.length-1]==input2[input2.length-1]){meetsCondition = true;}
|
|
49
|
+ return meetsCondition;
|
34
|
50
|
}
|
35
|
51
|
|
36
|
52
|
/**
|
|
@@ -39,7 +55,12 @@ public class ArrayDrills {
|
39
|
55
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
56
|
*/
|
41
|
57
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
58
|
+ int placeholder = input[0];
|
|
59
|
+ for (int i = 1; i < input.length; i++){
|
|
60
|
+ input[i-1] = input[i];
|
|
61
|
+ }
|
|
62
|
+ input[input.length-1] = placeholder;
|
|
63
|
+ return input;
|
43
|
64
|
}
|
44
|
65
|
|
45
|
66
|
|
|
@@ -50,7 +71,14 @@ public class ArrayDrills {
|
50
|
71
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
72
|
*/
|
52
|
73
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
74
|
+ int max = 0;
|
|
75
|
+ for(Integer element: input){
|
|
76
|
+ if(element > max){
|
|
77
|
+ max = element;
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+ Arrays.fill(input, max);
|
|
81
|
+ return input;
|
54
|
82
|
}
|
55
|
83
|
|
56
|
84
|
|
|
@@ -61,17 +89,53 @@ 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[] newArray = new Integer[2];
|
|
93
|
+ newArray[0] = middleValuesSumAnyLength(input1);
|
|
94
|
+ newArray[1] = middleValuesSumAnyLength(input2);
|
|
95
|
+ return newArray;
|
65
|
96
|
}
|
66
|
97
|
|
|
98
|
+ public boolean arrayLenthIsEven(Integer[] array){
|
|
99
|
+ boolean isEven = false;
|
|
100
|
+ if(array.length%2 == 0)
|
|
101
|
+ isEven = true;
|
|
102
|
+ return isEven;
|
|
103
|
+ }
|
67
|
104
|
|
|
105
|
+ public Integer middleValuesSumIfArrayIsEven(Integer[] array){
|
|
106
|
+ int leftOfMiddle = array[array.length/2];
|
|
107
|
+ int rightOfMiddle = array[array.length/2-1];
|
|
108
|
+ return leftOfMiddle+rightOfMiddle;
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ public Integer middleValueIfArrayIsOdd(Integer[] array){
|
|
112
|
+ return array[(array.length-1)/2];
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ public Integer middleValuesSumAnyLength(Integer[] array){
|
|
116
|
+ Integer middleValue = 0;
|
|
117
|
+ if (arrayLenthIsEven(array)){
|
|
118
|
+ middleValue = middleValuesSumIfArrayIsEven(array);
|
|
119
|
+ }else{
|
|
120
|
+ middleValue = array[(array.length-1)/2];
|
|
121
|
+ }
|
|
122
|
+ return middleValue;
|
|
123
|
+ }
|
68
|
124
|
/**
|
69
|
125
|
* Start with 2 int arrays, a and b, each length 2.
|
70
|
126
|
* Consider the sum of the values in each array.
|
71
|
127
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
128
|
*/
|
73
|
129
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
130
|
+ Integer[] largerArray = a;
|
|
131
|
+ if(sumOfArraySize2(b)>sumOfArraySize2(a)){
|
|
132
|
+ largerArray = b;
|
|
133
|
+ }
|
|
134
|
+ return largerArray;
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ public Integer sumOfArraySize2(Integer[] array){
|
|
138
|
+ return array[0]+array[1];
|
75
|
139
|
}
|
76
|
140
|
|
77
|
141
|
/**
|
|
@@ -81,6 +145,11 @@ public class ArrayDrills {
|
81
|
145
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
146
|
*/
|
83
|
147
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
148
|
+ Integer[] newArray = new Integer[3];
|
|
149
|
+ int lastIndex = (nums.length+1)/2;
|
|
150
|
+ for(int i = 0;i < newArray.length;i++, lastIndex++){
|
|
151
|
+ newArray[i] = nums[lastIndex-2];
|
|
152
|
+ }
|
|
153
|
+ return newArray;
|
85
|
154
|
}
|
86
|
155
|
}
|