diff --git a/kata/8-kyu/index.md b/kata/8-kyu/index.md index 9c7d0caa..e54daf6e 100644 --- a/kata/8-kyu/index.md +++ b/kata/8-kyu/index.md @@ -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") @@ -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") diff --git a/kata/8-kyu/neutralisation/README.md b/kata/8-kyu/neutralisation/README.md new file mode 100644 index 00000000..05dfaf3f --- /dev/null +++ b/kata/8-kyu/neutralisation/README.md @@ -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. \ No newline at end of file diff --git a/kata/8-kyu/neutralisation/main/Solution.java b/kata/8-kyu/neutralisation/main/Solution.java new file mode 100644 index 00000000..26b69176 --- /dev/null +++ b/kata/8-kyu/neutralisation/main/Solution.java @@ -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()); + } +} \ No newline at end of file diff --git a/kata/8-kyu/neutralisation/test/SolutionTest.java b/kata/8-kyu/neutralisation/test/SolutionTest.java new file mode 100644 index 00000000..26f96343 --- /dev/null +++ b/kata/8-kyu/neutralisation/test/SolutionTest.java @@ -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)); + } +} \ No newline at end of file