Skip to content

Commit 71c0c94

Browse files
committed
1 parent f17a651 commit 71c0c94

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

kata/6-kyu/weird-ipv6-hex-string-parsing/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
## Description
44

5-
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.
5+
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
6+
characters as if they were separate hexadecimal values, add them up, and join the results into a string.
67

7-
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"``
8-
9-
**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.
8+
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:
9+
`"1B1D" => 0x1 + 0xB + 0x1 + 0xD = 26`. After all the blocks have been processed in the same way, the results should be joined together:
10+
``"26" + "26" + "22" + "39" + "25" + "3" + "52" + "40"`` -> ``"262622392535240"``
1011

12+
**Note**: some character other than colon (`:`) may be used in the input string, but it is guaranteed that hexadecimal digits will never be
13+
used as separators.
1114

1215
#### Examples
1316

1417
```
1518
"1111:1111:1111:1111:1111:1111:1111:1111" => "4" + "4" + "4" + "4" + "4" + "4" + "4" + "4" => "44444444"
1619
"1111-1111-1111-1111-1111-1111-1111-1111" => "4" + "4" + "4" + "4" + "4" + "4" + "4" + "4" => "44444444"
1720
"ABCD_1111_ABCD_1111_ABCD_1111_ABCD_1111" => "46" + "4" + "46" + "4" + "46" + "4" + "46" + "4" => 464464464464
18-
```
19-
20-
Happy coding!
21+
```
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.Stream.of;
3+
4+
interface Solution {
5+
static String parseIPv6(String ip) {
6+
return of(ip.split("[^0-9A-F]")).map(s -> s.chars().map(c -> Character.digit(c, 16)).sum() + "").collect(joining());
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
1234:5678:9ABC:D00F:1111:2222:3333:4445, 10264228481217
10+
5454:FBFD:9ABC:AAAA:FFFF:2222:FBDE:0101, 18544240608532
11+
0000:0000:0000:0000:0000:0000:0000:0000, 00000000
12+
FFFF:FFFF:BBBB:CCCC:1212:AABC:0000:1111, 6060444864304
13+
ACDD-0101-9ABC-AAAA-FFFF-2222-FBDE-ACCC, 48242406085346
14+
5454rFBFDr9ABCrAA0ArFAFFr2222rFBDEr0101, 18544230558532
15+
F234#5678#9ABC#D00F#1111#2222#3333#4485, 24264228481221
16+
""")
17+
void sample(String ip, String expected) {
18+
assertEquals(expected, Solution.parseIPv6(ip));
19+
}
20+
}

0 commit comments

Comments
 (0)