|
@@ -3,6 +3,7 @@ package io.zipcoder;
|
3
|
3
|
public class ArrayDrills {
|
4
|
4
|
|
5
|
5
|
|
|
6
|
+
|
6
|
7
|
/**
|
7
|
8
|
* Given an array of ints, return true if value appears as either the first or last element in the array.
|
8
|
9
|
* The array will be length 1 or more.
|
|
@@ -10,8 +11,21 @@ public class ArrayDrills {
|
10
|
11
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
12
|
*/
|
12
|
13
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
14
|
|
- }
|
|
14
|
+
|
|
15
|
+ if(input[0] == value){
|
|
16
|
+
|
|
17
|
+ return true;
|
|
18
|
+
|
|
19
|
+ } else {
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+ }
|
|
23
|
+ return false;
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
15
|
29
|
|
16
|
30
|
/**
|
17
|
31
|
* 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.
|
|
@@ -19,7 +33,15 @@ public class ArrayDrills {
|
19
|
33
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
34
|
*/
|
21
|
35
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
36
|
+ if (input.length >1 && input[0] == input[input.length-1]){
|
|
37
|
+
|
|
38
|
+ return true;
|
|
39
|
+
|
|
40
|
+ } else{
|
|
41
|
+
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ return false;
|
23
|
45
|
}
|
24
|
46
|
|
25
|
47
|
|
|
@@ -30,7 +52,14 @@ public class ArrayDrills {
|
30
|
52
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
53
|
*/
|
32
|
54
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
55
|
+ if (input1[input1.length-1] == input2[input2.length-1]){
|
|
56
|
+
|
|
57
|
+ return true;
|
|
58
|
+ } else{
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+ }
|
|
62
|
+ return false;
|
34
|
63
|
}
|
35
|
64
|
|
36
|
65
|
/**
|
|
@@ -39,10 +68,18 @@ public class ArrayDrills {
|
39
|
68
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
69
|
*/
|
41
|
70
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
71
|
+ Integer[] arr = new Integer[input.length-1];
|
|
72
|
+ arr[0] = input[input.length-1];
|
|
73
|
+ for(int i=1;i<input.length;i++) {
|
|
74
|
+ arr[i] = input[i - 1];
|
|
75
|
+
|
|
76
|
+ }
|
|
77
|
+ return null;
|
43
|
78
|
}
|
44
|
79
|
|
45
|
80
|
|
|
81
|
+
|
|
82
|
+
|
46
|
83
|
/**
|
47
|
84
|
* Given an array of ints, figure out which is element in the array is largest,
|
48
|
85
|
* and set all the other elements to be that value. Return the changed array.
|