|
@@ -1,5 +1,8 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.Collections;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+
|
3
|
6
|
public class ArrayDrills {
|
4
|
7
|
|
5
|
8
|
|
|
@@ -9,8 +12,16 @@ public class ArrayDrills {
|
9
|
12
|
* example : firstLast(6, [1,2,6); // Should return true
|
10
|
13
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
14
|
*/
|
12
|
|
- public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
15
|
+ public Boolean firstLast(Integer value, Integer[] input) {
|
|
16
|
+ int firstElement = input[0];
|
|
17
|
+ int lastLastElement = input[input.length - 1];
|
|
18
|
+ if (firstElement == value || lastLastElement == value) {
|
|
19
|
+ return true;
|
|
20
|
+ }
|
|
21
|
+ // get first element
|
|
22
|
+ // get last element
|
|
23
|
+ // check if first Element or last element = value
|
|
24
|
+ return false;
|
14
|
25
|
}
|
15
|
26
|
|
16
|
27
|
/**
|
|
@@ -18,10 +29,15 @@ public class ArrayDrills {
|
18
|
29
|
* example : sameFirstLast([1,2,3]); // Should return false
|
19
|
30
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
31
|
*/
|
21
|
|
- public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
23
|
|
- }
|
|
32
|
+ public Boolean sameFirstLast(Integer[] input) {
|
|
33
|
+ int firstElement = input[0];
|
|
34
|
+ int lastElement = input[input.length - 1];
|
|
35
|
+ if (lastElement == firstElement) {
|
|
36
|
+ return true;
|
|
37
|
+ }
|
24
|
38
|
|
|
39
|
+ return false;
|
|
40
|
+ }
|
25
|
41
|
|
26
|
42
|
/**
|
27
|
43
|
* Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element.
|
|
@@ -29,8 +45,16 @@ public class ArrayDrills {
|
29
|
45
|
* example : commonEnd([1, 2, 3], [7, 3]); // Should return true
|
30
|
46
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
47
|
*/
|
32
|
|
- public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
48
|
+ public Boolean commonEnd(Integer[] input1, Integer[] input2) {
|
|
49
|
+ Integer firstArrayElement = input1[0];
|
|
50
|
+ Integer firstArrayLastElement = input1[input1.length - 1];
|
|
51
|
+ Integer secondArrayElement = input2[0];
|
|
52
|
+ Integer secondArrayLastElement = input2[input2.length - 1];
|
|
53
|
+ if (firstArrayElement.equals(secondArrayElement) || firstArrayLastElement.equals(secondArrayLastElement)) {
|
|
54
|
+ return true;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ return false;
|
34
|
58
|
}
|
35
|
59
|
|
36
|
60
|
/**
|
|
@@ -39,7 +63,9 @@ public class ArrayDrills {
|
39
|
63
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
64
|
*/
|
41
|
65
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
66
|
+ Collections.rotate(Arrays.asList(input), 2);
|
|
67
|
+ System.out.println(Arrays.toString(input));
|
|
68
|
+ return input;
|
43
|
69
|
}
|
44
|
70
|
|
45
|
71
|
|
|
@@ -50,7 +76,12 @@ public class ArrayDrills {
|
50
|
76
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
77
|
*/
|
52
|
78
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
79
|
+ int max = Collections.max(Arrays.asList(input));
|
|
80
|
+ for(int i = 0; i < input.length; i++) {
|
|
81
|
+ input[i] = max;
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ return input;
|
54
|
85
|
}
|
55
|
86
|
|
56
|
87
|
|
|
@@ -61,17 +92,40 @@ public class ArrayDrills {
|
61
|
92
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
93
|
*/
|
63
|
94
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
95
|
+ //assign new arrays
|
|
96
|
+ Integer[] midWay = new Integer[2];
|
|
97
|
+ midWay[0] = findMid(input1);
|
|
98
|
+ midWay[1] = findMid(input2);
|
|
99
|
+ return midWay;
|
65
|
100
|
}
|
66
|
101
|
|
|
102
|
+ //made an assisting method that will find the middle index for...too much looping involved and I got lazy
|
|
103
|
+
|
|
104
|
+ public int findMid(Integer[] newInput) {
|
|
105
|
+ if (newInput.length % 2 == 0) {
|
|
106
|
+ return newInput[newInput.length/2] + newInput[newInput.length/2 - 1];
|
|
107
|
+ }
|
67
|
108
|
|
|
109
|
+ return newInput[newInput.length/2];
|
|
110
|
+ }
|
68
|
111
|
/**
|
69
|
112
|
* Start with 2 int arrays, a and b, each length 2.
|
70
|
113
|
* Consider the sum of the values in each array.
|
71
|
114
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
115
|
*/
|
73
|
116
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
117
|
+ Integer sumA = 0;
|
|
118
|
+ Integer sumB = 0;
|
|
119
|
+ for(int i = 0; i < a.length; i++){
|
|
120
|
+ sumA += a[i];
|
|
121
|
+ sumB += b[i];
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ if (sumA == sumB){
|
|
125
|
+ return a;
|
|
126
|
+ }
|
|
127
|
+//this does not pass the 3rd test case for this...should make another an assisting method to call upon and do the work for me...
|
|
128
|
+ return b;
|
75
|
129
|
}
|
76
|
130
|
|
77
|
131
|
/**
|
|
@@ -81,6 +135,10 @@ public class ArrayDrills {
|
81
|
135
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
136
|
*/
|
83
|
137
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
138
|
+ Integer [] resultArr = new Integer[3];
|
|
139
|
+ resultArr[0] = nums[(nums.length/2) - 1];
|
|
140
|
+ resultArr[1] = nums[(nums.length/2)];
|
|
141
|
+ resultArr[2] = nums[(nums.length/2) + 1];
|
|
142
|
+ return resultArr;
|
85
|
143
|
}
|
86
|
144
|
}
|