Skip to content

Commit 20d0e69

Browse files
committed
2020-11-01 Update: Added "Sum without highest and lowest number" and "Count of positives / sum of negatives"
1 parent d00a2d4 commit 20d0e69

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.smlnskgmail.jaman.codewarsjava.kyu8;
2+
3+
import java.util.stream.IntStream;
4+
5+
// https://www.codewars.com/kata/576bb71bbbcf0951d5000044
6+
public class CountOfPositivesSumOfNegatives {
7+
8+
private final int[] input;
9+
10+
public CountOfPositivesSumOfNegatives(int[] input) {
11+
this.input = input;
12+
}
13+
14+
public int[] solution() {
15+
if (input == null) {
16+
return new int[]{};
17+
}
18+
int positivesCount = (int) IntStream
19+
.of(input)
20+
.filter(value -> value > 0)
21+
.count();
22+
int negativesSum = IntStream
23+
.of(input)
24+
.filter(value -> value < 0)
25+
.sum();
26+
if (positivesCount == 0 && negativesSum == 0) {
27+
return new int[]{};
28+
}
29+
return new int[] { positivesCount, negativesSum };
30+
}
31+
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.smlnskgmail.jaman.codewarsjava.kyu8;
2+
3+
import java.util.Arrays;
4+
5+
// https://www.codewars.com/kata/576b93db1129fcf2200001e6
6+
public class SumWithoutHighestAndLowestNumber {
7+
8+
private final int[] input;
9+
10+
public SumWithoutHighestAndLowestNumber(int[] input) {
11+
this.input = input;
12+
}
13+
14+
public int solution() {
15+
if (input == null || input.length < 2) {
16+
return 0;
17+
}
18+
final int[] minimumValue = { Integer.MAX_VALUE };
19+
final int[] maximumValue = { Integer.MIN_VALUE };
20+
return Arrays.stream(input).peek(operand -> {
21+
if (operand > maximumValue[0]) {
22+
maximumValue[0] = operand;
23+
}
24+
if (operand < minimumValue[0]) {
25+
minimumValue[0] = operand;
26+
}
27+
}).sum() - minimumValue[0] - maximumValue[0];
28+
}
29+
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.smlnskgmail.jaman.codewarsjava.kyu8;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertArrayEquals;
6+
7+
public class CountOfPositivesSumOfNegativesTest {
8+
9+
@Test
10+
public void countPositivesSumNegatives_BasicTest() {
11+
assertArrayEquals(
12+
new int[] { 10, -65 },
13+
new CountOfPositivesSumOfNegatives(
14+
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15 }
15+
).solution()
16+
);
17+
}
18+
19+
@Test
20+
public void countPositivesSumNegatives_InputWithZeroes() {
21+
assertArrayEquals(
22+
new int[] { 8, -50 },
23+
new CountOfPositivesSumOfNegatives(
24+
new int[] { 0, 2, 3, 0, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14 }
25+
).solution()
26+
);
27+
}
28+
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.smlnskgmail.jaman.codewarsjava.kyu8;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class SumWithoutHighestAndLowestNumberTest {
8+
9+
@Test
10+
public void BasicTests() {
11+
assertEquals(
12+
16,
13+
new SumWithoutHighestAndLowestNumber(new int[] { 6, 2, 1, 8, 10 }).solution()
14+
);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)