Keith Brinker il y a 6 ans
Parent
révision
4942602063

+ 16
- 0
.idea/compiler.xml Voir le fichier

@@ -0,0 +1,16 @@
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>
13
+      <module name="FundamentalDrills-Part1" target="1.5" />
14
+    </bytecodeTargetLevel>
15
+  </component>
16
+</project>

+ 13
- 0
.idea/misc.xml Voir le fichier

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

+ 8
- 0
.idea/modules.xml Voir le fichier

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectModuleManager">
4
+    <modules>
5
+      <module fileurl="file://$PROJECT_DIR$/FundamentalDrills-Part1.iml" filepath="$PROJECT_DIR$/FundamentalDrills-Part1.iml" />
6
+    </modules>
7
+  </component>
8
+</project>

+ 6
- 0
.idea/vcs.xml Voir le fichier

@@ -0,0 +1,6 @@
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>

+ 16
- 0
FundamentalDrills-Part1.iml Voir le fichier

@@ -0,0 +1,16 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4
+    <output url="file://$MODULE_DIR$/target/classes" />
5
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
+    <content url="file://$MODULE_DIR$">
7
+      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8
+      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
9
+      <excludeFolder url="file://$MODULE_DIR$/target" />
10
+    </content>
11
+    <orderEntry type="inheritedJdk" />
12
+    <orderEntry type="sourceFolder" forTests="false" />
13
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
14
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
+  </component>
16
+</module>

+ 84
- 30
src/main/java/io/zipcoder/ArrayDrills.java Voir le fichier

@@ -7,19 +7,25 @@ public class ArrayDrills {
7 7
      * Given an array of ints, return true if value appears as either the first or last element in the array.
8 8
      * The array will be length 1 or more.
9 9
      * example : firstLast(6, [1,2,6); // Should return true
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){
13
-        return null;
12
+    public Boolean firstLast(Integer value, Integer[] input) {
13
+        for (int i = 0; i < input.length; i++) {
14
+            if (input[i] == value)
15
+                return true;
16
+        }
17
+        return false;
14 18
     }
15 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
-     *           sameFirstLast([1,2,1]); // Should return true
23
+     * sameFirstLast([1,2,1]); // Should return true
20 24
      */
21
-    public Boolean sameFirstLast(Integer[] input){
22
-        return null;
25
+    public Boolean sameFirstLast(Integer[] input) {
26
+        if (input[0] == input[input.length - 1])
27
+            return true;
28
+        else return false;
23 29
     }
24 30
 
25 31
 
@@ -27,19 +33,27 @@ public class ArrayDrills {
27 33
      * Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element.
28 34
      * Both arrays will be length 1 or more.
29 35
      * example : commonEnd([1, 2, 3], [7, 3]); // Should return true
30
-     *           commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
36
+     * commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
31 37
      */
32
-    public Boolean commonEnd(Integer[] input1, Integer[] input2){
33
-        return null;
38
+    public Boolean commonEnd(Integer[] input1, Integer[] input2) {
39
+        if (input1[0] == input2[0])
40
+            return true;
41
+        else if (input1[input1.length - 1] == input2[input2.length - 1])
42
+            return true;
43
+        return false;
34 44
     }
35 45
 
46
+    /* true = (input1[0] == input2[0] || input1[input1.length-1]  == input2[input2.length-1] */
47
+
48
+
36 49
     /**
37 50
      * Given an array of ints, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
38 51
      * example : rotateLeft([1, 2, 3]); // Should return [2,3,1]
39
-     *           rotateLeft([5, 11, 9]); // Should return [11,9,5]
52
+     * rotateLeft([5, 11, 9]); // Should return [11,9,5]
40 53
      */
41
-    public Integer[] rotateLeft(Integer[] input){
42
-        return null;
54
+    public Integer[] rotateLeft(Integer[] input) {
55
+        Integer[] rotating = {input[1], input[2], input[0]};
56
+        return rotating;
43 57
     }
44 58
 
45 59
 
@@ -47,10 +61,19 @@ public class ArrayDrills {
47 61
      * Given an array of ints, figure out which is element in the array is largest,
48 62
      * and set all the other elements to be that value. Return the changed array.
49 63
      * example : maxValue([1, 2, 3]); // Should return [3,3,3]
50
-     *           maxValue([5, 11, 9]); // Should return [11,11,11]
64
+     * maxValue([5, 11, 9]); // Should return [11,11,11]
51 65
      */
52
-    public Integer[] maxValue(Integer[] input){
53
-        return null;
66
+    public Integer[] maxValue(Integer[] input) {
67
+        Integer maxValue = input[0];
68
+            for(Integer i = 0; i< input.length; i++) {
69
+                 if (input[i] > maxValue) {
70
+                           maxValue = input[i]; }
71
+               }
72
+            Integer[]outPut = new Integer[input.length];
73
+            for(int j=0; j<input.length; j++) {
74
+                   outPut[j]=maxValue;
75
+             }
76
+          return outPut;
54 77
     }
55 78
 
56 79
 
@@ -58,29 +81,60 @@ public class ArrayDrills {
58 81
      * Given 2 int arrays, a and b, return a new array length 2 containing their middle elements, if length is odd.
59 82
      * If the array length is even the sum of the middle 2 elements in the array.
60 83
      * 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]
84
+     * middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
62 85
      */
63
-    public Integer[] middleWay(Integer[] input1, Integer[] input2){
64
-        return null;
86
+    public Integer[] middleWay(Integer[] input1, Integer[] input2) {
87
+        Integer[] newInput = new Integer[2];
88
+        if (input1.length % 2 == 0) {
89
+            newInput[0] = ((input1[input1.length / 2]) + (input1[input1.length / 2 - 1]));
90
+        } else if (input1.length % 2 != 0) {
91
+            newInput[0] = input1[input1.length / 2];
92
+        }
93
+        if (input2.length % 2 == 0) {
94
+            newInput[1] = ((input2[input2.length / 2]) + (input2[input2.length / 2 - 1]));
95
+        } else if (input2.length % 2 != 0) {
96
+            newInput[1] = input2[input2.length / 2];
97
+        }
98
+        return newInput;
65 99
     }
66 100
 
67
-
68 101
     /**
69 102
      * Start with 2 int arrays, a and b, each length 2.
70 103
      * Consider the sum of the values in each array.
71 104
      * Return the array which has the largest sum. In event of a tie, return a.
72 105
      */
73
-    public Integer[] biggerTwo(Integer[] a, Integer[] b){
74
-        return null;
106
+    public Integer[] biggerTwo(Integer[] a, Integer[] b) {
107
+        int sumA = 0;
108
+        int sumB = 0;
109
+
110
+        for (int i = 0; i < a.length; i++) {
111
+            sumA += a[i];
112
+            sumB += b[i];
113
+        }
114
+
115
+        if (sumA >= sumB) {
116
+            return a;
117
+
118
+        }
119
+        return b;
75 120
     }
76 121
 
77
-    /**
78
-     * Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array.
79
-     * The array length will be at least 3.
80
-     * 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]
82
-     */
83
-    public Integer[] midThree(Integer[] nums){
84
-        return null;
122
+
123
+
124
+        /**
125
+         * Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array.
126
+         * The array length will be at least 3.
127
+         * example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
128
+         *           midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
129
+         */
130
+        public Integer[] midThree (Integer[]nums){
131
+            Integer[] halfArray = new Integer[3];
132
+            Integer half = (nums.length) / 2;
133
+            halfArray[0] = nums[half-1];
134
+            halfArray[1] = nums[half];
135
+            halfArray[2] = nums[half+1];
136
+            return halfArray;
137
+        }
138
+
139
+
85 140
     }
86
-}

+ 1
- 1
src/test/java/io/zipcoder/ArrayDrillsTest.java Voir le fichier

@@ -177,7 +177,7 @@ public class ArrayDrillsTest {
177 177
 
178 178
     @Test
179 179
     public void biggerTwo3(){
180
-        Integer[] inputArray1 = {-1 ,20};
180
+        Integer[] inputArray1 = {1 ,20};
181 181
         Integer[] inputArray2 = {2, 15};
182 182
         Integer[] expected = {1,20};
183 183
         Integer[] actual = arrayDrills.biggerTwo(inputArray1, inputArray2);

BIN
target/classes/io/zipcoder/ArrayDrills.class Voir le fichier


BIN
target/test-classes/io/zipcoder/ArrayDrillsTest.class Voir le fichier