Tennessee Gibbs пре 6 година
родитељ
комит
8dfcb4cbbc

+ 1
- 0
.idea/.name Прегледај датотеку

1
+FundamentalDrills-Part1

+ 16
- 0
.idea/compiler.xml Прегледај датотеку

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 Прегледај датотеку

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_9" project-jdk-name="9" project-jdk-type="JavaSDK">
11
+    <output url="file://$PROJECT_DIR$/classes" />
12
+  </component>
13
+</project>

+ 8
- 0
.idea/modules.xml Прегледај датотеку

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 Прегледај датотеку

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 Прегледај датотеку

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>

+ 112
- 34
src/main/java/io/zipcoder/ArrayDrills.java Прегледај датотеку

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

+ 1
- 2
src/test/java/io/zipcoder/ArrayDrillsTest.java Прегледај датотеку

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
-import com.sun.tools.corba.se.idl.InterfaceGen;
4
 import org.junit.Assert;
3
 import org.junit.Assert;
5
 import org.junit.Before;
4
 import org.junit.Before;
6
 import org.junit.Test;
5
 import org.junit.Test;
179
     public void biggerTwo3(){
178
     public void biggerTwo3(){
180
         Integer[] inputArray1 = {-1 ,20};
179
         Integer[] inputArray1 = {-1 ,20};
181
         Integer[] inputArray2 = {2, 15};
180
         Integer[] inputArray2 = {2, 15};
182
-        Integer[] expected = {1,20};
181
+        Integer[] expected = {-1,20};
183
         Integer[] actual = arrayDrills.biggerTwo(inputArray1, inputArray2);
182
         Integer[] actual = arrayDrills.biggerTwo(inputArray1, inputArray2);
184
         Assert.assertArrayEquals(expected, actual);
183
         Assert.assertArrayEquals(expected, actual);
185
     }
184
     }

BIN
target/classes/io/zipcoder/ArrayDrills.class Прегледај датотеку


BIN
target/test-classes/io/zipcoder/ArrayDrillsTest.class Прегледај датотеку