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