Retired

Parse IPv6 Address (retired)

Description:

This kata is an extension to my previous kata, Shorten IPv6 Address.

An IPv6 address consists of 8 groups of 4 hexadecimal digits. It can never have more or less than 8 groups. Each group has at most 4 digits, but leading zeros can be removed. If a group has only zeros, it can be shortened to ::. Multiple consecutive groups consisting of only zeros can also be shortened to ::
Therefore:
a0cd:0:0:0:0:0:0:b4c1 is equal to
a0cd::0:0:0:0:0:b4c1 is equal to
a0cd::b4c1.

a0cd::b4c1::0d3a is not valid, because there is uncertainty about the positions of the zero-groups. At most one instance of :: can occur in any given IPv6 address.

To convert IPv4 addresses to IPv6, an IPv6 addres can also contain an IPv4 addres. This is called an IPv4-mapped addres. It consist of five consecutive zero-groups, followed by an ffff group, followed by either:

  • the IPv4 address itself, or
  • the hexadecimal representation of the IPv4 address. For example:
    0000:0000:0000:0000:0000:ffff:192.168.1.1
    0000:0000:0000:0000:0000:ffff:c0a8:0101
    The above two addresses are equal, and both represent the IPv4 address 192.168.1.1. In the second case, 192 is converted to c0, 168 to a8, etc. These addresses can (and should) be shortened as usual.

    Your job is to write the function parse(), which will receive a string as input, and should
    (1) check if the input string is a valid IPv6 address (or a valid IPv4-mapped address). If the input is not valid, return False.
    (2) convert the input address to its shortened form.
    (3) if the input string is an IPv4-mapped addres, return both the hexadecimal and decimal representation, in that order. If it is an IPv6 address, simply return the shortened form.
    Note that the given IPv6 address might not necessarily be shortened correctly. In the address a0c3::120c:ce1d:adcf:0:0:0: a single group of zeros has been shortened to ::, but it also has three consecutive groups of zeros. In this case your function should correct this and return a0c3:0:120c:ce1d:adcf:: instead.

    All returned addresses should contain capital letters only.

    Some examples:
parse('094c:00ec:f4ba:c2d9') = False             # Address does not contain enough groups 
parse('094c:00ec:0:0:0:0:0:0:c2d9') = False        # Address contains too many groups
parse('094cd::c2d9') = False                     # First group contains too many digits
parse('094h::c2d9') = False                      # Address contains an invalid character
parse('094h::c2d9::f4ba') = False                # Address contains multiple '::'
parse('1:2::3:4:5:6:7:8') = False                # Address contains too many groups when expanded

parse('094C:0000:0000:00EC:0000:F4BA:C2D9:0003') = '94C::EC:0:F4BA:C2D9:3'  
parse('0000:0000:0000:0000:0000:ffff:192.168.1.1') = ('::FFFF:C0A8:101', '::FFFF:192.168.1.1')       
parse('::ffFF:c0a8:11') = ('::FFFF:C0A8:11', '::FFFF:192.168.0.17')
parse('::ffff:c0a8:0101') = ('::FFFF:C0A8:101', '::FFFF:::192.168.1.1')


If you have any feedback, bugs, suggestions or anything else, please leave it in the comments!

Algorithms
Regular Expressions

More By Author:

Check out these other kata created by TheBMachine

Stats:

CreatedMay 19, 2016
Warriors Trained67
Total Skips0
Total Code Submissions247
Total Times Completed12
Python Completions12
Total Stars7
% of votes with a positive feedback rating50% of 6
Total "Very Satisfied" Votes1
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes1
Total Rank Assessments6
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • TheBMachine Avatar
Ad