diff --git a/kata/6-kyu/index.md b/kata/6-kyu/index.md index cc110361..b18ab139 100644 --- a/kata/6-kyu/index.md +++ b/kata/6-kyu/index.md @@ -431,6 +431,7 @@ - [VIN Checker](vin-checker "60a54750138eac0031eb98e1") - [Wave Sorting](wave-sorting "596f28fd9be8ebe6ec0000c1") - [We are the Robots d[(0)(0)]b](we-are-the-robots-d-0-0-b "587ae98e2ab0ef32ef00004c") +- [Weird IPv6 hex string parsing](weird-ipv6-hex-string-parsing "5f5bc8a04e485f002d85b303") - [What century is it?](what-century-is-it "52fb87703c1351ebd200081f") - [What's A Name In?](whats-a-name-in "59daf400beec9780a9000045") - [What's Your Poison?](whats-your-poison "58c47a95e4eb57a5b9000094") diff --git a/kata/6-kyu/weird-ipv6-hex-string-parsing/README.md b/kata/6-kyu/weird-ipv6-hex-string-parsing/README.md new file mode 100644 index 00000000..f23381c7 --- /dev/null +++ b/kata/6-kyu/weird-ipv6-hex-string-parsing/README.md @@ -0,0 +1,21 @@ +# [Weird IPv6 hex string parsing](https://www.codewars.com/kata/weird-ipv6-hex-string-parsing "https://www.codewars.com/kata/5f5bc8a04e485f002d85b303") + +## Description + +As the title already told you, you have to parse a IPv6 hex string in a weird way. For each block in the string you have to parse its +characters as if they were separate hexadecimal values, add them up, and join the results into a string. + +Here's an example of an IPv6 string: `1B1D:AF01:3847:F8C4:20E9:0111:DFEA:AAAA`. And here's how you'd convert its first block to an integer: +`"1B1D" => 0x1 + 0xB + 0x1 + 0xD = 26`. After all the blocks have been processed in the same way, the results should be joined together: +``"26" + "26" + "22" + "39" + "25" + "3" + "52" + "40"`` -> ``"262622392535240"`` + +**Note**: some character other than colon (`:`) may be used in the input string, but it is guaranteed that hexadecimal digits will never be +used as separators. + +#### Examples + +``` +"1111:1111:1111:1111:1111:1111:1111:1111" => "4" + "4" + "4" + "4" + "4" + "4" + "4" + "4" => "44444444" +"1111-1111-1111-1111-1111-1111-1111-1111" => "4" + "4" + "4" + "4" + "4" + "4" + "4" + "4" => "44444444" +"ABCD_1111_ABCD_1111_ABCD_1111_ABCD_1111" => "46" + "4" + "46" + "4" + "46" + "4" + "46" + "4" => 464464464464 +``` \ No newline at end of file diff --git a/kata/6-kyu/weird-ipv6-hex-string-parsing/main/Solution.java b/kata/6-kyu/weird-ipv6-hex-string-parsing/main/Solution.java new file mode 100644 index 00000000..cbe9911e --- /dev/null +++ b/kata/6-kyu/weird-ipv6-hex-string-parsing/main/Solution.java @@ -0,0 +1,8 @@ +import static java.util.stream.Collectors.joining; +import static java.util.stream.Stream.of; + +interface Solution { + static String parseIPv6(String ip) { + return of(ip.split("[^0-9A-F]")).map(s -> s.chars().map(c -> Character.digit(c, 16)).sum() + "").collect(joining()); + } +} \ No newline at end of file diff --git a/kata/6-kyu/weird-ipv6-hex-string-parsing/test/SolutionTest.java b/kata/6-kyu/weird-ipv6-hex-string-parsing/test/SolutionTest.java new file mode 100644 index 00000000..4d2f016c --- /dev/null +++ b/kata/6-kyu/weird-ipv6-hex-string-parsing/test/SolutionTest.java @@ -0,0 +1,20 @@ +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 = """ + 1234:5678:9ABC:D00F:1111:2222:3333:4445, 10264228481217 + 5454:FBFD:9ABC:AAAA:FFFF:2222:FBDE:0101, 18544240608532 + 0000:0000:0000:0000:0000:0000:0000:0000, 00000000 + FFFF:FFFF:BBBB:CCCC:1212:AABC:0000:1111, 6060444864304 + ACDD-0101-9ABC-AAAA-FFFF-2222-FBDE-ACCC, 48242406085346 + 5454rFBFDr9ABCrAA0ArFAFFr2222rFBDEr0101, 18544230558532 + F234#5678#9ABC#D00F#1111#2222#3333#4485, 24264228481221 + """) + void sample(String ip, String expected) { + assertEquals(expected, Solution.parseIPv6(ip)); + } +} \ No newline at end of file