Skip to content

Commit 5456e5e

Browse files
committed
1 parent 9150f5e commit 5456e5e

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

kata/8-kyu/neutralisation/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# [Neutralisation](https://www.codewars.com/kata/neutralisation "https://www.codewars.com/kata/65128732b5aff40032a3d8f0")
22

33
Given two strings comprised of `+` and `-`, return a new string which shows how the two strings interact in the following way:
4+
45
- When positives and positives interact, they *remain positive*.
56
- When negatives and negatives interact, they *remain negative*.
67
- But when negatives and positives interact, they *become neutral*, and are shown as the number `0`.
78

89
### Worked Example
10+
911
```
1012
("+-+", "+--") ➞ "+-0"
1113
# Compare the first characters of each string, then the next in turn.
@@ -16,6 +18,7 @@ Given two strings comprised of `+` and `-`, return a new string which shows how
1618
```
1719

1820
### Examples
21+
1922
```
2023
("--++--", "++--++") ➞ "000000"
2124
@@ -25,4 +28,5 @@ Given two strings comprised of `+` and `-`, return a new string which shows how
2528
```
2629

2730
### Notes
28-
The two strings will be the same length.
31+
32+
The two strings will be the same length.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import static java.util.stream.Collectors.joining;
2+
import static java.util.stream.IntStream.range;
3+
4+
interface Solution {
5+
static String neutralise(String s1, String s2) {
6+
return range(0, s1.length()).mapToObj(i -> s1.charAt(i) == s2.charAt(i) ? s1.charAt(i) + "" : "0").collect(joining());
7+
}
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class SolutionTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
'','',''
10+
+,+,+
11+
-,-,-
12+
+,-,0
13+
-,+,0
14+
-+,++,0+
15+
--,-+,-0
16+
+--,+--,+--
17+
-++,-+-,-+0
18+
+-+,-++,00+
19+
-++,+--,000
20+
-++-,-+-+,-+00
21+
--++,++++,00++
22+
---+,-+++,-00+
23+
-----,-----,-----
24+
--++--,++--++,000000
25+
-+-+-+,-+-+-+,-+-+-+
26+
+-----+-,---++-++,0--00-+0
27+
--+++-+-,+++++---,00+++-0-
28+
+++--+---,++----++-,++0--000-
29+
-++-+-++-,+-++++---,00+0+000-
30+
---++-+--,-+++--++-,-00+0-+0-
31+
++-++--++-,-+++-++-++,0+0+0000+0
32+
+-----+++-,--+-+-++--,0-0-0-++0-
33+
-+--+-+---,-+--+-+-+-,-+--+-+-0-
34+
""")
35+
void sample(String s1, String s2, String expected) {
36+
assertEquals(expected, Solution.neutralise(s1, s2));
37+
}
38+
}

0 commit comments

Comments
 (0)