|
@@ -1,5 +1,8 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+
|
3
|
6
|
public class ArrayDrills {
|
4
|
7
|
|
5
|
8
|
|
|
@@ -10,7 +13,7 @@ public class ArrayDrills {
|
10
|
13
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
14
|
*/
|
12
|
15
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
16
|
+ return (value.equals(input[0])||value.equals(input[input.length-1]));
|
14
|
17
|
}
|
15
|
18
|
|
16
|
19
|
/**
|
|
@@ -19,7 +22,7 @@ public class ArrayDrills {
|
19
|
22
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
23
|
*/
|
21
|
24
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
25
|
+ return (input[0].equals(input[input.length-1]));
|
23
|
26
|
}
|
24
|
27
|
|
25
|
28
|
|
|
@@ -30,7 +33,7 @@ public class ArrayDrills {
|
30
|
33
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
34
|
*/
|
32
|
35
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
36
|
+ return (input1[0].equals(input2[0]) || input1[input1.length-1].equals(input2[input2.length-1]));
|
34
|
37
|
}
|
35
|
38
|
|
36
|
39
|
/**
|
|
@@ -38,8 +41,15 @@ public class ArrayDrills {
|
38
|
41
|
* example : rotateLeft([1, 2, 3]); // Should return [2,3,1]
|
39
|
42
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
43
|
*/
|
41
|
|
- public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
44
|
+ public Integer[] rotateLeft(Integer[] input) {
|
|
45
|
+
|
|
46
|
+ Integer [] rotateLeft = new Integer[input.length];
|
|
47
|
+ rotateLeft[rotateLeft.length - 1] = input[0];
|
|
48
|
+
|
|
49
|
+ for (Integer i = 0; i < rotateLeft.length - 1; i++) {
|
|
50
|
+ rotateLeft[i] = input[i + 1];
|
|
51
|
+ }
|
|
52
|
+ return rotateLeft;
|
43
|
53
|
}
|
44
|
54
|
|
45
|
55
|
|
|
@@ -50,7 +60,14 @@ public class ArrayDrills {
|
50
|
60
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
61
|
*/
|
52
|
62
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
63
|
+ Arrays.sort(input);
|
|
64
|
+ ArrayList<Integer> newArray = new ArrayList<Integer>();
|
|
65
|
+ for(int i =0; i < input.length; i++){
|
|
66
|
+ newArray.add(input[input.length-1]);
|
|
67
|
+ }
|
|
68
|
+ Integer[] result= newArray.toArray(new Integer[newArray.size()]);
|
|
69
|
+
|
|
70
|
+ return result;
|
54
|
71
|
}
|
55
|
72
|
|
56
|
73
|
|
|
@@ -60,27 +77,55 @@ public class ArrayDrills {
|
60
|
77
|
* example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
|
61
|
78
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
79
|
*/
|
63
|
|
- public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
80
|
+ public Integer[] middleWay(Integer[] input1, Integer[] input2) {
|
|
81
|
+
|
|
82
|
+ Integer[] newArray = new Integer[2];
|
|
83
|
+
|
|
84
|
+ newArray[0] = middleOfArray(input1);
|
|
85
|
+ newArray[1] = middleOfArray(input2);
|
|
86
|
+
|
|
87
|
+ return newArray;
|
65
|
88
|
}
|
66
|
89
|
|
|
90
|
+ public int middleOfArray(Integer[] input){
|
|
91
|
+ if((input.length % 2 == 0)){
|
|
92
|
+ return (input[input.length / 2] + input[(input.length / 2)-1]);
|
|
93
|
+ }
|
|
94
|
+ else if((input.length % 2 != 0)){
|
|
95
|
+ return input[input.length / 2];
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ return 0;
|
|
99
|
+ }
|
67
|
100
|
|
68
|
101
|
/**
|
69
|
102
|
* Start with 2 int arrays, a and b, each length 2.
|
70
|
103
|
* Consider the sum of the values in each array.
|
71
|
104
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
105
|
*/
|
73
|
|
- public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
106
|
+ public Integer[] biggerTwo(Integer[] a, Integer[] b) {
|
|
107
|
+ int arrayOne = a[0] + a[1];
|
|
108
|
+ int arrayTwo = b[0] + b[1];
|
|
109
|
+
|
|
110
|
+ if (arrayOne == arrayTwo) {
|
|
111
|
+ return a;
|
|
112
|
+ } else if (arrayOne > arrayTwo) {
|
|
113
|
+ return a;
|
|
114
|
+ } else
|
|
115
|
+ return b;
|
75
|
116
|
}
|
76
|
117
|
|
|
118
|
+
|
77
|
119
|
/**
|
78
|
120
|
* Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array.
|
79
|
121
|
* The array length will be at least 3.
|
80
|
122
|
* example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
|
81
|
123
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
124
|
*/
|
83
|
|
- public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
85
|
|
- }
|
|
125
|
+ public Integer[] midThree(Integer[] nums) {
|
|
126
|
+ Integer n = nums.length / 2;
|
|
127
|
+ Integer[] middleThree = {nums[n - 1], nums[n], nums[n + 1]};
|
|
128
|
+ return middleThree;
|
|
129
|
+ }
|
86
|
130
|
}
|
|
131
|
+
|