#37 J-N000

Open
J-N000 wants to merge 1 commits from J-N000/ZCW-FundamentalDrills-1:master into master

+ 1
- 0
.idea/.name View File

1
+FundamentalDrills-Part1

+ 14
- 0
.idea/compiler.xml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="CompilerConfiguration">
4
+    <annotationProcessing>
5
+      <profile name="Maven default annotation processors profile" enabled="true">
6
+        <sourceOutputDir name="target/generated-sources/annotations" />
7
+        <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
8
+        <outputRelativeToContentRoot value="true" />
9
+        <module name="FundamentalDrills-Part1" />
10
+      </profile>
11
+    </annotationProcessing>
12
+    <bytecodeTargetLevel target="8" />
13
+  </component>
14
+</project>

+ 14
- 0
.idea/misc.xml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
4
+  <component name="MavenProjectsManager">
5
+    <option name="originalFiles">
6
+      <list>
7
+        <option value="$PROJECT_DIR$/pom.xml" />
8
+      </list>
9
+    </option>
10
+  </component>
11
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
12
+    <output url="file://$PROJECT_DIR$/out" />
13
+  </component>
14
+</project>

+ 6
- 0
.idea/vcs.xml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+  </component>
6
+</project>

+ 34
- 9
src/main/java/io/zipcoder/ArrayDrills.java View File

10
      *           firstLast(6, [1,2,3]); // Should return false
10
      *           firstLast(6, [1,2,3]); // Should return false
11
      */
11
      */
12
     public Boolean firstLast(Integer value, Integer[] input){
12
     public Boolean firstLast(Integer value, Integer[] input){
13
-        return null;
13
+        return input[0] == value || input[input.length - 1] == value;
14
     }
14
     }
15
 
15
 
16
     /**
16
     /**
19
      *           sameFirstLast([1,2,1]); // Should return true
19
      *           sameFirstLast([1,2,1]); // Should return true
20
      */
20
      */
21
     public Boolean sameFirstLast(Integer[] input){
21
     public Boolean sameFirstLast(Integer[] input){
22
-        return null;
22
+        return input.length > 1 && input[0] == input[input.length - 1];
23
     }
23
     }
24
 
24
 
25
 
25
 
30
      *           commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
30
      *           commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
31
      */
31
      */
32
     public Boolean commonEnd(Integer[] input1, Integer[] input2){
32
     public Boolean commonEnd(Integer[] input1, Integer[] input2){
33
-        return null;
33
+        return input1[0] == input2[0] || input1[input1.length - 1] == input2[input2.length - 1];
34
     }
34
     }
35
 
35
 
36
     /**
36
     /**
39
      *           rotateLeft([5, 11, 9]); // Should return [11,9,5]
39
      *           rotateLeft([5, 11, 9]); // Should return [11,9,5]
40
      */
40
      */
41
     public Integer[] rotateLeft(Integer[] input){
41
     public Integer[] rotateLeft(Integer[] input){
42
-        return null;
42
+        return new Integer[] {input[1], input[2], input[0]};
43
     }
43
     }
44
 
44
 
45
 
45
 
50
      *           maxValue([5, 11, 9]); // Should return [11,11,11]
50
      *           maxValue([5, 11, 9]); // Should return [11,11,11]
51
      */
51
      */
52
     public Integer[] maxValue(Integer[] input){
52
     public Integer[] maxValue(Integer[] input){
53
-        return null;
53
+        int max = Integer.MIN_VALUE;
54
+        for (Integer e : input) {
55
+            max = Math.max(max, e);
56
+        }
57
+        for (int i = 0; i < input.length; i++) {
58
+            input[i] = max;
59
+        }
60
+        return input;
54
     }
61
     }
55
 
62
 
56
 
63
 
60
      * example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
67
      * example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
61
      *           middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
68
      *           middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
62
      */
69
      */
70
+    public boolean isEvenArray(Integer[] input) {
71
+        return input.length % 2 == 0;
72
+    }
63
     public Integer[] middleWay(Integer[] input1, Integer[] input2){
73
     public Integer[] middleWay(Integer[] input1, Integer[] input2){
64
-        return null;
74
+        int i1 = input1.length / 2;
75
+        int i2 = input2.length / 2;
76
+        return new Integer[] {
77
+                isEvenArray(input1) ? input1[i1] + input1[i1 - 1] : input1[i1],
78
+                isEvenArray(input2) ? input2[i2] + input2[i2 - 1] : input2[i2]
79
+        };
65
     }
80
     }
66
 
81
 
67
 
82
 
71
      * Return the array which has the largest sum. In event of a tie, return a.
86
      * Return the array which has the largest sum. In event of a tie, return a.
72
      */
87
      */
73
     public Integer[] biggerTwo(Integer[] a, Integer[] b){
88
     public Integer[] biggerTwo(Integer[] a, Integer[] b){
74
-        return null;
89
+        int sumA = 0;
90
+        int sumB = 0;
91
+        for (int i = 0; i < 2; i++) {
92
+            sumA += a[i];
93
+            sumB += b[i];
94
+        }
95
+        return sumA >= sumB ? a : b;
75
     }
96
     }
76
 
97
 
77
     /**
98
     /**
80
      * example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
101
      * example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
81
      *           midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
102
      *           midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
82
      */
103
      */
83
-    public Integer[] midThree(Integer[] nums){
84
-        return null;
104
+    public Integer[] midThree(Integer[] nums) {
105
+        return new Integer[] {
106
+                nums[nums.length / 2 - 1],
107
+                nums[nums.length / 2],
108
+                nums[nums.length / 2 + 1]
109
+        };
85
     }
110
     }
86
 }
111
 }

+ 1
- 1
src/test/java/io/zipcoder/ArrayDrillsTest.java View File

179
     public void biggerTwo3(){
179
     public void biggerTwo3(){
180
         Integer[] inputArray1 = {-1 ,20};
180
         Integer[] inputArray1 = {-1 ,20};
181
         Integer[] inputArray2 = {2, 15};
181
         Integer[] inputArray2 = {2, 15};
182
-        Integer[] expected = {1,20};
182
+        Integer[] expected = {-1,20};
183
         Integer[] actual = arrayDrills.biggerTwo(inputArray1, inputArray2);
183
         Integer[] actual = arrayDrills.biggerTwo(inputArray1, inputArray2);
184
         Assert.assertArrayEquals(expected, actual);
184
         Assert.assertArrayEquals(expected, actual);
185
     }
185
     }

BIN
target/classes/io/zipcoder/ArrayDrills.class View File


BIN
target/test-classes/io/zipcoder/ArrayDrillsTest.class View File