Browse Source

findSmallestAnd2ndSmallest

Nick Satinover 6 years ago
parent
commit
32c3897c3c
5 changed files with 215 additions and 2 deletions
  1. 1
    1
      arraz/.idea/compiler.xml
  2. 1
    1
      arraz/arraz.iml
  3. 12
    0
      arraz/pom.xml
  4. 74
    0
      arraz/src/main/java/Arraz.java
  5. 127
    0
      arraz/src/test/java/ArrazTest.java

+ 1
- 1
arraz/.idea/compiler.xml View File

@@ -10,7 +10,7 @@
10 10
       </profile>
11 11
     </annotationProcessing>
12 12
     <bytecodeTargetLevel>
13
-      <module name="arraz" target="1.5" />
13
+      <module name="arraz" target="8" />
14 14
     </bytecodeTargetLevel>
15 15
   </component>
16 16
 </project>

+ 1
- 1
arraz/arraz.iml View File

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

+ 12
- 0
arraz/pom.xml View File

@@ -7,6 +7,18 @@
7 7
     <groupId>rocks.zipcode.arraysgonewild</groupId>
8 8
     <artifactId>arraz</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 74
- 0
arraz/src/main/java/Arraz.java View File

@@ -1,5 +1,6 @@
1 1
 import java.util.ArrayList;
2 2
 import java.util.Arrays;
3
+import java.util.TreeMap;
3 4
 
4 5
 public class Arraz {
5 6
 
@@ -217,5 +218,78 @@ public class Arraz {
217 218
         return true;
218 219
     }
219 220
 
221
+    public double averageArrayWithoutMaxMin(int[] arr){
222
+        MaxMin maxMin = findMaxMinOfArray(arr);
223
+        double sum;
224
+        boolean haveMax = false;
225
+        boolean haveMin = false;
220 226
 
227
+        ArrayList<Integer> arrayList = new ArrayList<Integer>();
228
+
229
+        for (int i: arr) {
230
+            if (i == maxMin.max && haveMax == false ){
231
+                haveMax = true;
232
+            }
233
+            else if (i == maxMin.min && haveMin == false){
234
+                haveMin = true;
235
+            }
236
+            else {
237
+                arrayList.add(i);
238
+            }
239
+        }
240
+        sum = arrayList.stream().mapToDouble(Integer::doubleValue).sum();
241
+        return sum / arrayList.size();
242
+    }
243
+
244
+    public boolean arrayHas65and77(int[] arr){
245
+        boolean has65 = false;
246
+        boolean has77 = false;
247
+
248
+        for (int i:arr) {
249
+            if (i == 65){
250
+                has65 = true;
251
+            }
252
+            if (i == 77){
253
+                has77 = true;
254
+            }
255
+        }
256
+        return has65 && has77;
257
+    }
258
+
259
+    public boolean theTotalofTensIs30(int[] arr) {
260
+        int sum = 0;
261
+
262
+        for (int i:arr) {
263
+            if (i == 10){
264
+                sum += i;
265
+            }
266
+        }
267
+        return sum == 30;
268
+    }
269
+
270
+    public int[] findSmallestAnd2ndSmallest(int[] arr){
271
+        int minValue = Integer.MAX_VALUE;
272
+
273
+        for (int d:arr) {
274
+            if (d < minValue){
275
+                minValue = d;
276
+            }
277
+        }
278
+
279
+        int secondMin = find2ndSmallestValueFromArray(arr, minValue);
280
+        int[] retArr = {minValue, secondMin};
281
+
282
+        return retArr;
283
+    }
284
+
285
+    private int find2ndSmallestValueFromArray(int[] arr, int minValue){
286
+        int secondMin = Integer.MAX_VALUE;
287
+
288
+        for (int d:arr) {
289
+            if (d > minValue && d < secondMin){
290
+                secondMin = d;
291
+            }
292
+        }
293
+        return secondMin;
294
+    }
221 295
 }

+ 127
- 0
arraz/src/test/java/ArrazTest.java View File

@@ -496,4 +496,131 @@ public class ArrazTest {
496 496
         //THEN
497 497
         Assert.assertEquals(expected, actual);
498 498
     }
499
+
500
+    @Test
501
+    public void test1averageArrayWithoutMaxMin() {
502
+        //GIVEN
503
+        int[] arr = {1, 2, 3, 4, 5};
504
+        //WHEN
505
+        double expected = 3;
506
+
507
+        double actual = arraz.averageArrayWithoutMaxMin(arr);
508
+        //THEN
509
+        Assert.assertEquals(expected, actual);
510
+    }
511
+
512
+    @Test
513
+    public void test2averageArrayWithoutMaxMin() {
514
+        //GIVEN
515
+        int[] arr = {1, 4, 3, 4, 5, 5};
516
+        //WHEN
517
+        double expected = 4;
518
+
519
+        double actual = arraz.averageArrayWithoutMaxMin(arr);
520
+        //THEN
521
+        Assert.assertEquals(expected, actual);
522
+    }
523
+
524
+    @Test
525
+    public void test1arrayHas65and77() {
526
+        //GIVEN
527
+        int[] arr = {1, 2, 3, 4, 5};
528
+        //WHEN
529
+        boolean expected = false;
530
+
531
+        boolean actual = arraz.arrayHas65and77(arr);
532
+        //THEN
533
+        Assert.assertEquals(expected, actual);
534
+    }
535
+
536
+    @Test
537
+    public void test2arrayHas65and77() {
538
+        //GIVEN
539
+        int[] arr = {1, 2, 3, 65, 5};
540
+        //WHEN
541
+        boolean expected = false;
542
+
543
+        boolean actual = arraz.arrayHas65and77(arr);
544
+        //THEN
545
+        Assert.assertEquals(expected, actual);
546
+    }
547
+
548
+    @Test
549
+    public void test3arrayHas65and77() {
550
+        //GIVEN
551
+        int[] arr = {1, 2, 3, 77, 5};
552
+        //WHEN
553
+        boolean expected = false;
554
+
555
+        boolean actual = arraz.arrayHas65and77(arr);
556
+        //THEN
557
+        Assert.assertEquals(expected, actual);
558
+    }
559
+    @Test
560
+    public void test4arrayHas65and77() {
561
+        //GIVEN
562
+        int[] arr = {1, 2, 3, 77, 65};
563
+        //WHEN
564
+        boolean expected = true;
565
+
566
+        boolean actual = arraz.arrayHas65and77(arr);
567
+        //THEN
568
+        Assert.assertEquals(expected, actual);
569
+    }
570
+
571
+    @Test
572
+    public void testTheTotalofTensIs30(){
573
+        //GIVEN
574
+        int[] arr = {10, 2, 10, 77, 10};
575
+        //WHEN
576
+        boolean expected = true;
577
+
578
+        boolean actual = arraz.theTotalofTensIs30(arr);
579
+        //THEN
580
+        Assert.assertEquals(expected, actual);
581
+    }
582
+
583
+    @Test
584
+    public void test2TheTotalofTensIs30(){
585
+        //GIVEN
586
+        int[] arr = {10, 2, 0, 77, 10};
587
+        //WHEN
588
+        boolean expected = false;
589
+
590
+        boolean actual = arraz.theTotalofTensIs30(arr);
591
+        //THEN
592
+        Assert.assertEquals(expected, actual);
593
+    }
594
+
595
+    @Test
596
+    public void test1FindSmallestAnd2ndSmallest() {
597
+        //GIVEN
598
+        int[] arr = {1, 2, 3, 4, 5};
599
+        //WHEN
600
+        int expectedSmallest = 1;
601
+        int expected2ndSmallest = 2;
602
+
603
+        int[] retArr = arraz.findSmallestAnd2ndSmallest(arr);
604
+        int actualSmallest = retArr[0];
605
+        int actual2ndSmallest = retArr[1];
606
+        //THEN
607
+        Assert.assertEquals(expectedSmallest, actualSmallest);
608
+        Assert.assertEquals(expected2ndSmallest, actual2ndSmallest);
609
+    }
610
+
611
+    @Test
612
+    public void test2FindSmallestAnd2ndSmallest() {
613
+        //GIVEN
614
+        int[] arr = {1, 2, 3, 4, 5, -3, -1000, 13, 13, 44444};
615
+        //WHEN
616
+        int expectedSmallest = -1000;
617
+        int expected2ndSmallest = -3;
618
+
619
+        int[] retArr = arraz.findSmallestAnd2ndSmallest(arr);
620
+        int actualSmallest = retArr[0];
621
+        int actual2ndSmallest = retArr[1];
622
+        //THEN
623
+        Assert.assertEquals(expectedSmallest, actualSmallest);
624
+        Assert.assertEquals(expected2ndSmallest, actual2ndSmallest);
625
+    }
499 626
 }