Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion kata/8-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
- [Multiply](multiply "50654ddff44f800200000004")
- [My head is at the wrong end!](my-head-is-at-the-wrong-end "56f699cd9400f5b7d8000b55")
- [Name on billboard](name-on-billboard "570e8ec4127ad143660001fd")
- [Neutralisation](neutralisation "65128732b5aff40032a3d8f0")
- [Never visit a . . . !?](never-visit-a "56c5847f27be2c3db20009c3")
- [No zeros for heroes](no-zeros-for-heros "570a6a46455d08ff8d001002")
- [OOP: Object Oriented Piracy ](oop-object-oriented-piracy "54fe05c4762e2e3047000add")
Expand Down Expand Up @@ -180,7 +181,7 @@
- [Rock Paper Scissors!](rock-paper-scissors "5672a98bdbdd995fad00000f")
- [Sentence Smash](sentence-smash "53dc23c68a0c93699800041d")
- [Short Long Short](short-long-short "50654ddff44f800200000007")
- [simple calculator ](simple-calculator "5810085c533d69f4980001cf")
- [Simple calculator ](simple-calculator "5810085c533d69f4980001cf")
- [Simple Fun #1: Seats in Theater](simple-fun-number-1-seats-in-theater "588417e576933b0ec9000045")
- [Simple multiplication](simple-multiplication "583710ccaa6717322c000105")
- [Simple validation of a username with regex](simple-validation-of-a-username-with-regex "56a3f08aa9a6cc9b75000023")
Expand Down
32 changes: 32 additions & 0 deletions kata/8-kyu/neutralisation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# [Neutralisation](https://www.codewars.com/kata/neutralisation "https://www.codewars.com/kata/65128732b5aff40032a3d8f0")

Given two strings comprised of `+` and `-`, return a new string which shows how the two strings interact in the following way:

- When positives and positives interact, they *remain positive*.
- When negatives and negatives interact, they *remain negative*.
- But when negatives and positives interact, they *become neutral*, and are shown as the number `0`.

### Worked Example

```
("+-+", "+--") ➞ "+-0"
# Compare the first characters of each string, then the next in turn.
# "+" against a "+" returns another "+".
# "-" against a "-" returns another "-".
# "+" against a "-" returns "0".
# Return the string of characters.
```

### Examples

```
("--++--", "++--++") ➞ "000000"

("-+-+-+", "-+-+-+") ➞ "-+-+-+"

("-++-", "-+-+") ➞ "-+00"
```

### Notes

The two strings will be the same length.
8 changes: 8 additions & 0 deletions kata/8-kyu/neutralisation/main/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import static java.util.stream.Collectors.joining;
import static java.util.stream.IntStream.range;

interface Solution {
static String neutralise(String s1, String s2) {
return range(0, s1.length()).mapToObj(i -> s1.charAt(i) == s2.charAt(i) ? s1.charAt(i) + "" : "0").collect(joining());
}
}
38 changes: 38 additions & 0 deletions kata/8-kyu/neutralisation/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class SolutionTest {
@ParameterizedTest
@CsvSource(textBlock = """
'','',''
+,+,+
-,-,-
+,-,0
-,+,0
-+,++,0+
--,-+,-0
+--,+--,+--
-++,-+-,-+0
+-+,-++,00+
-++,+--,000
-++-,-+-+,-+00
--++,++++,00++
---+,-+++,-00+
-----,-----,-----
--++--,++--++,000000
-+-+-+,-+-+-+,-+-+-+
+-----+-,---++-++,0--00-+0
--+++-+-,+++++---,00+++-0-
+++--+---,++----++-,++0--000-
-++-+-++-,+-++++---,00+0+000-
---++-+--,-+++--++-,-00+0-+0-
++-++--++-,-+++-++-++,0+0+0000+0
+-----+++-,--+-+-++--,0-0-0-++0-
-+--+-+---,-+--+-+-+-,-+--+-+-0-
""")
void sample(String s1, String s2, String expected) {
assertEquals(expected, Solution.neutralise(s1, s2));
}
}