浏览代码

committing drill1

chitraBegerhotta 6 年前
父节点
当前提交
84c8f6b198

+ 2
- 0
.gitignore 查看文件

@@ -13,6 +13,8 @@
13 13
 .idea/**/sqlDataSources.xml
14 14
 .idea/**/dynamic.xml
15 15
 .idea/**/uiDesigner.xml
16
+.idea/**/misc.xml
17
+.idea/.name
16 18
 
17 19
 # Gradle:
18 20
 .idea/**/gradle.xml

+ 13
- 0
.idea/compiler.xml 查看文件

@@ -0,0 +1,13 @@
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
+  </component>
13
+</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>

+ 100
- 11
src/main/java/io/zipcoder/ArrayDrills.java 查看文件

@@ -10,7 +10,15 @@ public class ArrayDrills {
10 10
      *           firstLast(6, [1,2,3]); // Should return false
11 11
      */
12 12
     public Boolean firstLast(Integer value, Integer[] input){
13
-        return null;
13
+        for(int i=0; i<input.length; i++){
14
+            if(value == input[i] || value == input[input.length-1]){
15
+              return true;
16
+            }
17
+
18
+
19
+        }
20
+
21
+        return false;
14 22
     }
15 23
 
16 24
     /**
@@ -19,8 +27,16 @@ public class ArrayDrills {
19 27
      *           sameFirstLast([1,2,1]); // Should return true
20 28
      */
21 29
     public Boolean sameFirstLast(Integer[] input){
22
-        return null;
23
-    }
30
+        for(int i=0; i<input.length; i++) {
31
+            if(input.length >= 1 && input[i] == input[input.length-1]) {
32
+                return true;
33
+            }
34
+        }
35
+        return false;
36
+
37
+        }
38
+
39
+
24 40
 
25 41
 
26 42
     /**
@@ -30,8 +46,13 @@ public class ArrayDrills {
30 46
      *           commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
31 47
      */
32 48
     public Boolean commonEnd(Integer[] input1, Integer[] input2){
33
-        return null;
34
-    }
49
+        if(input1[0] == input2[0] || input1[input1.length-1] == input2[input2.length-1]){
50
+            return true;
51
+        }
52
+        return false;
53
+        }
54
+
55
+
35 56
 
36 57
     /**
37 58
      * Given an array of ints, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
@@ -39,8 +60,17 @@ public class ArrayDrills {
39 60
      *           rotateLeft([5, 11, 9]); // Should return [11,9,5]
40 61
      */
41 62
     public Integer[] rotateLeft(Integer[] input){
42
-        return null;
43
-    }
63
+
64
+        Integer[] newArr = new Integer[input.length];
65
+
66
+        for(int i=0; i<input.length-1; i++){
67
+          newArr[i] = input[i+1];
68
+        }
69
+        newArr[newArr.length-1] = input[0];
70
+        return newArr;
71
+        }
72
+
73
+
44 74
 
45 75
 
46 76
     /**
@@ -50,7 +80,26 @@ public class ArrayDrills {
50 80
      *           maxValue([5, 11, 9]); // Should return [11,11,11]
51 81
      */
52 82
     public Integer[] maxValue(Integer[] input){
53
-        return null;
83
+        Integer[] newArr = new Integer[input.length];
84
+        int largestValue = input[0];
85
+        int k = 0;
86
+        for(int i=1; i<input.length; i++) {
87
+            if (input[i] > largestValue) {
88
+                largestValue = input[i];
89
+            }
90
+        }
91
+            for(int i=0; i<input.length; i++ ){
92
+                if(input[i] != largestValue){
93
+                    input[i] = largestValue;
94
+                    newArr[k] = input[i];
95
+                    k++;
96
+                }
97
+                else {
98
+                    newArr[k] = input[i];
99
+                    k++;
100
+                }
101
+            }
102
+        return newArr;
54 103
     }
55 104
 
56 105
 
@@ -61,7 +110,27 @@ public class ArrayDrills {
61 110
      *           middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
62 111
      */
63 112
     public Integer[] middleWay(Integer[] input1, Integer[] input2){
64
-        return null;
113
+        Integer[] newArr = new Integer[2];
114
+        int len1 = input1.length;
115
+        int len2 = input2.length;
116
+
117
+        if(len1%2 == 0){
118
+            newArr[0] = input1[len1/2] + input1[(len1-2)/2];
119
+
120
+        }
121
+        else {
122
+            newArr[0] = input1[len1/2];
123
+
124
+        }
125
+        if(len2%2 == 0){
126
+            newArr[1] = input2[len2/2] + input2[(len2-2)/2];
127
+
128
+        }
129
+        else {
130
+            newArr[1] = input2[len2/2];
131
+        }
132
+
133
+        return newArr;
65 134
     }
66 135
 
67 136
 
@@ -71,7 +140,18 @@ public class ArrayDrills {
71 140
      * Return the array which has the largest sum. In event of a tie, return a.
72 141
      */
73 142
     public Integer[] biggerTwo(Integer[] a, Integer[] b){
74
-        return null;
143
+
144
+        int total1 = a[0] + a[1];
145
+
146
+          int total2 = b[0] + b[1];
147
+
148
+          if(total1 >= total2)
149
+              return a;
150
+
151
+
152
+
153
+        return b;
154
+
75 155
     }
76 156
 
77 157
     /**
@@ -81,6 +161,15 @@ public class ArrayDrills {
81 161
      *           midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
82 162
      */
83 163
     public Integer[] midThree(Integer[] nums){
84
-        return null;
164
+
165
+        Integer[] newArr = new Integer[3];
166
+
167
+        int len = nums.length;
168
+
169
+        newArr[0] = nums[(len-2)/2];
170
+        newArr[1] = nums[len/2];
171
+        newArr[2] = nums[(len+1)/2];
172
+
173
+        return newArr;
85 174
     }
86 175
 }

+ 1
- 1
src/test/java/io/zipcoder/ArrayDrillsTest.java 查看文件

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

二进制
target/classes/io/zipcoder/ArrayDrills.class 查看文件


二进制
target/test-classes/io/zipcoder/ArrayDrillsTest.class 查看文件