|
@@ -10,16 +10,21 @@ public class ArrayDrills {
|
10
|
10
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
11
|
*/
|
12
|
12
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
13
|
+ if(value.equals(input[0])||value.equals(input[input.length-1])){
|
|
14
|
+ return true;
|
|
15
|
+ }
|
|
16
|
+ return false;
|
14
|
17
|
}
|
15
|
18
|
|
|
19
|
+
|
16
|
20
|
/**
|
17
|
21
|
* Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.
|
18
|
22
|
* example : sameFirstLast([1,2,3]); // Should return false
|
19
|
23
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
24
|
*/
|
21
|
25
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
26
|
+ return (input.length>=1 && input[0 ].equals(input[input.length-1]));
|
|
27
|
+
|
23
|
28
|
}
|
24
|
29
|
|
25
|
30
|
|
|
@@ -30,7 +35,8 @@ public class ArrayDrills {
|
30
|
35
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
36
|
*/
|
32
|
37
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
38
|
+ return (input1[0].equals(input2[0])||input1[input1.length-1].equals(input2[input2.length-1]));
|
|
39
|
+
|
34
|
40
|
}
|
35
|
41
|
|
36
|
42
|
/**
|
|
@@ -39,7 +45,14 @@ public class ArrayDrills {
|
39
|
45
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
46
|
*/
|
41
|
47
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
48
|
+ //review notes in book of break down
|
|
49
|
+ Integer[] rotatedArray = new Integer[input.length];
|
|
50
|
+ rotatedArray[rotatedArray.length - 1] = input[0];
|
|
51
|
+
|
|
52
|
+ for(Integer i = 0; i<rotatedArray.length-1;i++){
|
|
53
|
+ rotatedArray[i]=input[i+1];
|
|
54
|
+ }
|
|
55
|
+ return rotatedArray;
|
43
|
56
|
}
|
44
|
57
|
|
45
|
58
|
|
|
@@ -50,6 +63,16 @@ public class ArrayDrills {
|
50
|
63
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
64
|
*/
|
52
|
65
|
public Integer[] maxValue(Integer[] input){
|
|
66
|
+ //halfway done, don't really understand what I'm doing wrong here.
|
|
67
|
+ Integer[] array2 = new Integer[3];
|
|
68
|
+
|
|
69
|
+ Integer temporary = input[0];
|
|
70
|
+ for(Integer i =1; i<input.length;i++){
|
|
71
|
+ if(temporary<input[i]){
|
|
72
|
+ temporary=input[i];
|
|
73
|
+
|
|
74
|
+ }
|
|
75
|
+ }
|
53
|
76
|
return null;
|
54
|
77
|
}
|
55
|
78
|
|
|
@@ -61,6 +84,8 @@ public class ArrayDrills {
|
61
|
84
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
85
|
*/
|
63
|
86
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
|
87
|
+
|
|
88
|
+
|
64
|
89
|
return null;
|
65
|
90
|
}
|
66
|
91
|
|
|
@@ -71,7 +96,13 @@ public class ArrayDrills {
|
71
|
96
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
97
|
*/
|
73
|
98
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
99
|
+ Integer addUp = a[0]+a[1];
|
|
100
|
+ Integer addUp1 = b[0]+b[1];
|
|
101
|
+
|
|
102
|
+ if(addUp>addUp1){
|
|
103
|
+ return a;
|
|
104
|
+ }
|
|
105
|
+ return b;
|
75
|
106
|
}
|
76
|
107
|
|
77
|
108
|
/**
|
|
@@ -81,6 +112,15 @@ public class ArrayDrills {
|
81
|
112
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
113
|
*/
|
83
|
114
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
115
|
+
|
|
116
|
+ Integer[] array2 = new Integer[3];
|
|
117
|
+
|
|
118
|
+ Integer a = nums.length;
|
|
119
|
+ Integer middleIndex = nums.length / 2;
|
|
120
|
+ array2[0] = nums[middleIndex-1];
|
|
121
|
+ array2[1] = nums[middleIndex];
|
|
122
|
+ array2[2] = nums[middleIndex+1];
|
|
123
|
+
|
|
124
|
+ return array2;
|
85
|
125
|
}
|
86
|
126
|
}
|