瀏覽代碼

sortArrayIntoEvensThenOdds

Nick Satinover 6 年之前
父節點
當前提交
4fde066677
共有 2 個文件被更改,包括 162 次插入2 次删除
  1. 62
    2
      arraz/src/main/java/Arraz.java
  2. 100
    0
      arraz/src/test/java/ArrazTest.java

+ 62
- 2
arraz/src/main/java/Arraz.java 查看文件

@@ -186,7 +186,7 @@ public class Arraz {
186 186
     }
187 187
 
188 188
     public ArrayList<Integer> makeMeAnArrayListFromArray(int[] intsArr){
189
-        ArrayList<Integer> retArr = new ArrayList<Integer>();
189
+        ArrayList<Integer> retArr = new ArrayList<>();
190 190
         for (int i: intsArr) {
191 191
             retArr.add(i);
192 192
         }
@@ -237,7 +237,10 @@ public class Arraz {
237 237
                 arrayList.add(i);
238 238
             }
239 239
         }
240
-        sum = arrayList.stream().mapToDouble(Integer::doubleValue).sum();
240
+        sum = arrayList.stream()
241
+                .mapToDouble(Integer::doubleValue)
242
+                .sum();
243
+
241 244
         return sum / arrayList.size();
242 245
     }
243 246
 
@@ -312,4 +315,61 @@ public class Arraz {
312 315
         }
313 316
         return retArr;
314 317
     }
318
+
319
+    public int[] removeFirstItemAndCopy(int[] arr) {
320
+        int[] retArr = new int[arr.length - 1];
321
+
322
+        for (int i = 1; i < arr.length; i++) {
323
+            retArr[i-1] = arr[i];
324
+        }
325
+        return retArr;
326
+    }
327
+
328
+    public int[] insertAtStartAndCopy(int[] arr, int insert) {
329
+        int[] retArr = new int[arr.length + 1];
330
+        retArr[0] = insert;
331
+
332
+        for (int i = 0; i < arr.length; i++) {
333
+            retArr[i+1] = arr[i];
334
+        }
335
+        return retArr;
336
+    }
337
+
338
+    public int[] insertAtEndAndCopy(int[] arr, int insert) {
339
+        int[] retArr = new int[arr.length + 1];
340
+        retArr[retArr.length - 1] = insert;
341
+
342
+        for (int i = 0; i < arr.length; i++) {
343
+            retArr[i] = arr[i];
344
+        }
345
+        return retArr;
346
+    }
347
+
348
+    public int[] sortArrayIntoEvensThenOdds(int[] arr) {
349
+        ArrayList<Integer> even = new ArrayList<>();
350
+        ArrayList<Integer> odd = new ArrayList<>();
351
+        int[] retArr = new int[arr.length];
352
+
353
+        for (int i: arr) {
354
+            if (i % 2 == 0){
355
+                even.add(i);
356
+            }
357
+            else {
358
+                odd.add(i);
359
+            }
360
+        }
361
+
362
+        int index = 0;
363
+        for (int i: even) {
364
+            retArr[index] = i;
365
+            index++;
366
+        }
367
+
368
+        for (int i: odd) {
369
+            retArr[index] = i;
370
+            index++;
371
+        }
372
+
373
+        return retArr;
374
+    }
315 375
 }

+ 100
- 0
arraz/src/test/java/ArrazTest.java 查看文件

@@ -668,4 +668,104 @@ public class ArrazTest {
668 668
         //THEN
669 669
         Assert.assertEquals(arr[2], actual[actual.length - 1]);
670 670
     }
671
+
672
+    @Test
673
+    public void test1removeFirstItemAndCopy(){
674
+        //GIVEN
675
+        int[] arr = {1, 2, 3, 4, 5};
676
+        //WHEN
677
+        int[] retArr = arraz.removeFirstItemAndCopy(arr);
678
+        int expected = 4;
679
+        int actual = retArr.length;
680
+        //THEN
681
+        Assert.assertEquals(expected, actual);
682
+    }
683
+
684
+    @Test
685
+    public void test2removeFirstItemAndCopy(){
686
+        //GIVEN
687
+        int[] arr = {1, 2, 3, 4, 5};
688
+        //WHEN
689
+        int[] retArr = arraz.removeFirstItemAndCopy(arr);
690
+        int expected = 2;
691
+        int actual = retArr[0];
692
+        //THEN
693
+        Assert.assertEquals(expected, actual);
694
+    }
695
+
696
+    @Test
697
+    public void test1insertAtStartAndCopy(){
698
+        //GIVEN
699
+        int[] arr = {1, 2, 3, 4, 5};
700
+        int insert = 777;
701
+        //WHEN
702
+        int[] retArr = arraz.insertAtStartAndCopy(arr, insert);
703
+        int expected = 6;
704
+        int actual = retArr.length;
705
+        //THEN
706
+        Assert.assertEquals(expected, actual);
707
+    }
708
+
709
+    @Test
710
+    public void test2insertAtStartAndCopy(){
711
+        //GIVEN
712
+        int[] arr = {1, 2, 3, 4, 5};
713
+        int insert = 777;
714
+        //WHEN
715
+        int[] retArr = arraz.insertAtStartAndCopy(arr, insert);
716
+        int expected = 777;
717
+        int actual = retArr[0];
718
+        //THEN
719
+        Assert.assertEquals(expected, actual);
720
+    }
721
+
722
+    @Test
723
+    public void test1insertAtEndAndCopy(){
724
+        //GIVEN
725
+        int[] arr = {1, 2, 3, 4, 5};
726
+        int insert = 777;
727
+        //WHEN
728
+        int[] retArr = arraz.insertAtEndAndCopy(arr, insert);
729
+        int expected = 777;
730
+        int actual = retArr[retArr.length - 1];
731
+        //THEN
732
+        Assert.assertEquals(expected, actual);
733
+    }
734
+
735
+    @Test
736
+    public void test2insertAtEndAndCopy(){
737
+        //GIVEN
738
+        int[] arr = {1, 2, 3, 4, 5};
739
+        int insert = 777;
740
+        //WHEN
741
+        int[] retArr = arraz.insertAtEndAndCopy(arr, insert);
742
+        int expected = 6;
743
+        int actual = retArr.length;
744
+        //THEN
745
+        Assert.assertEquals(expected, actual);
746
+    }
747
+    // sortArrayIntoEvensThenOdds
748
+    @Test
749
+    public void test1sortArrayIntoEvensThenOdds(){
750
+        //GIVEN
751
+        int[] arr = {4,5,102,6,-7,12,-32,92,8};
752
+        //WHEN
753
+        int[] retArr = arraz.sortArrayIntoEvensThenOdds(arr);
754
+        int expected = 4;
755
+        int actual = retArr[0];
756
+        //THEN
757
+        Assert.assertEquals(expected, actual);
758
+    }
759
+
760
+    @Test
761
+    public void test2sortArrayIntoEvensThenOdds(){
762
+        //GIVEN
763
+        int[] arr = {4,5,102,6,-7,12,-32,92,8};
764
+        //WHEN
765
+        int[] retArr = arraz.sortArrayIntoEvensThenOdds(arr);
766
+        int expected = -7;
767
+        int actual = retArr[retArr.length - 1];
768
+        //THEN
769
+        Assert.assertEquals(expected, actual);
770
+    }
671 771
 }