Kthomas пре 6 година
родитељ
комит
70ef316cf4

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

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

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

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

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

@@ -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>

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

@@ -1,5 +1,9 @@
1 1
 package io.zipcoder;
2 2
 
3
+import jdk.internal.util.xml.impl.Input;
4
+import java.util.Collections;
5
+import java.util.Arrays;
6
+
3 7
 public class ArrayDrills {
4 8
 
5 9
 
@@ -9,8 +13,16 @@ public class ArrayDrills {
9 13
      * example : firstLast(6, [1,2,6); // Should return true
10 14
      *           firstLast(6, [1,2,3]); // Should return false
11 15
      */
12
-    public Boolean firstLast(Integer value, Integer[] input){
13
-        return null;
16
+    public Boolean firstLast(Integer value, Integer[] input) {
17
+        int firstElement = input[0];
18
+        int lastLastElement = input[input.length - 1];
19
+        if (firstElement == value || lastLastElement == value) {
20
+            return true;
21
+        }
22
+        // get first element
23
+        // get last element
24
+        // check if first Element or last element = value
25
+        return false;
14 26
     }
15 27
 
16 28
     /**
@@ -18,10 +30,15 @@ public class ArrayDrills {
18 30
      * example : sameFirstLast([1,2,3]); // Should return false
19 31
      *           sameFirstLast([1,2,1]); // Should return true
20 32
      */
21
-    public Boolean sameFirstLast(Integer[] input){
22
-        return null;
23
-    }
33
+    public Boolean sameFirstLast(Integer[] input) {
34
+        int firstElement = input[0];
35
+        int lastElement = input[input.length - 1];
36
+        if (lastElement == firstElement) {
37
+            return true;
38
+        }
24 39
 
40
+        return false;
41
+    }
25 42
 
26 43
     /**
27 44
      * Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element.
@@ -29,8 +46,16 @@ public class ArrayDrills {
29 46
      * example : commonEnd([1, 2, 3], [7, 3]); // Should return true
30 47
      *           commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
31 48
      */
32
-    public Boolean commonEnd(Integer[] input1, Integer[] input2){
33
-        return null;
49
+    public Boolean commonEnd(Integer[] input1, Integer[] input2) {
50
+        Integer firstArrayElement = input1[0];
51
+        Integer firstArrayLastElement = input1[input1.length - 1];
52
+        Integer secondArrayElement = input2[0];
53
+        Integer secondArrayLastElement = input2[input2.length - 1];
54
+        if (firstArrayElement.equals(secondArrayElement) || firstArrayLastElement.equals(secondArrayLastElement)) {
55
+            return true;
56
+        }
57
+
58
+        return false;
34 59
     }
35 60
 
36 61
     /**
@@ -39,7 +64,9 @@ public class ArrayDrills {
39 64
      *           rotateLeft([5, 11, 9]); // Should return [11,9,5]
40 65
      */
41 66
     public Integer[] rotateLeft(Integer[] input){
42
-        return null;
67
+        Collections.rotate(Arrays.asList(input), 2);
68
+        System.out.println(Arrays.toString(input));
69
+        return input;
43 70
     }
44 71
 
45 72
 
@@ -50,7 +77,12 @@ public class ArrayDrills {
50 77
      *           maxValue([5, 11, 9]); // Should return [11,11,11]
51 78
      */
52 79
     public Integer[] maxValue(Integer[] input){
53
-        return null;
80
+        int max = Collections.max(Arrays.asList(input));
81
+        for(int i = 0; i < input.length; i++) {
82
+            input[i] = max;
83
+        }
84
+
85
+        return input;
54 86
     }
55 87
 
56 88
 
@@ -61,17 +93,40 @@ public class ArrayDrills {
61 93
      *           middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
62 94
      */
63 95
     public Integer[] middleWay(Integer[] input1, Integer[] input2){
64
-        return null;
96
+        //assign new arrays
97
+        Integer[] midWay = new Integer[2];
98
+        midWay[0] = findMid(input1);
99
+        midWay[1] = findMid(input2);
100
+        return midWay;
65 101
     }
66 102
 
103
+    //made an assisting method that will find the middle index for...too much looping involved and I got lazy
104
+
105
+    public int findMid(Integer[] newInput) {
106
+        if (newInput.length % 2 == 0) {
107
+            return newInput[newInput.length/2] + newInput[newInput.length/2 - 1];
108
+        }
67 109
 
110
+        return newInput[newInput.length/2];
111
+    }
68 112
     /**
69 113
      * Start with 2 int arrays, a and b, each length 2.
70 114
      * Consider the sum of the values in each array.
71 115
      * Return the array which has the largest sum. In event of a tie, return a.
72 116
      */
73 117
     public Integer[] biggerTwo(Integer[] a, Integer[] b){
74
-        return null;
118
+        Integer sumA = 0;
119
+        Integer sumB = 0;
120
+        for(int i = 0; i < a.length; i++){
121
+            sumA += a[i];
122
+            sumB += b[i];
123
+        }
124
+
125
+        if (sumA == sumB){
126
+            return a;
127
+        }
128
+//this does not pass the 3rd test case for this...should make another an assisting method tio
129
+        return b;
75 130
     }
76 131
 
77 132
     /**
@@ -81,6 +136,10 @@ public class ArrayDrills {
81 136
      *           midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
82 137
      */
83 138
     public Integer[] midThree(Integer[] nums){
84
-        return null;
139
+        Integer [] resultArr = new Integer[3];
140
+        resultArr[0] = nums[(nums.length/2) - 1];
141
+        resultArr[1] = nums[(nums.length/2)];
142
+        resultArr[2] = nums[(nums.length/2) + 1];
143
+        return resultArr;
85 144
     }
86 145
 }

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


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