File tree Expand file tree Collapse file tree 4 files changed +50
-0
lines changed
weird-ipv6-hex-string-parsing Expand file tree Collapse file tree 4 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 431
431
- [ VIN Checker] ( vin-checker " 60a54750138eac0031eb98e1 ")
432
432
- [ Wave Sorting] ( wave-sorting " 596f28fd9be8ebe6ec0000c1 ")
433
433
- [ We are the Robots d[ (0)(0)] b] ( we-are-the-robots-d-0-0-b " 587ae98e2ab0ef32ef00004c ")
434
+ - [ Weird IPv6 hex string parsing] ( weird-ipv6-hex-string-parsing " 5f5bc8a04e485f002d85b303 ")
434
435
- [ What century is it?] ( what-century-is-it " 52fb87703c1351ebd200081f ")
435
436
- [ What's A Name In?] ( whats-a-name-in " 59daf400beec9780a9000045 ")
436
437
- [ What's Your Poison?] ( whats-your-poison " 58c47a95e4eb57a5b9000094 ")
Original file line number Diff line number Diff line change
1
+ # [ Weird IPv6 hex string parsing] ( https://www.codewars.com/kata/weird-ipv6-hex-string-parsing " https://www.codewars.com/kata/5f5bc8a04e485f002d85b303 ")
2
+
3
+ ## Description
4
+
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.
7
+
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" ``
11
+
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.
14
+
15
+ #### Examples
16
+
17
+ ```
18
+ "1111:1111:1111:1111:1111:1111:1111:1111" => "4" + "4" + "4" + "4" + "4" + "4" + "4" + "4" => "44444444"
19
+ "1111-1111-1111-1111-1111-1111-1111-1111" => "4" + "4" + "4" + "4" + "4" + "4" + "4" + "4" => "44444444"
20
+ "ABCD_1111_ABCD_1111_ABCD_1111_ABCD_1111" => "46" + "4" + "46" + "4" + "46" + "4" + "46" + "4" => 464464464464
21
+ ```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments