Sometimes when I'm typing out an IPv4 address, I get all the numbers right, but I forget to type one or more periods. I'd like to have a program (or function) that takes my broken IPv4 address and outputs all possible valid placements of the missing periods.
Input
The input will always be a string that is a transformation of a valid IPv4 address (see particulars below). It will always have been transformed solely by the elimination of one or more period characters.
You submission does not need to handle inputs outside of this format.
Output
A collection or list, in no particular order or format, of strings representing all valid IPv4 addresses that can be created from the input by the insertion of period characters into the input.
- The output may be a language-native list or other ordered or unordered collection type.
- Alternatively, it may be a string sequence of IPv4 address delimited in some clear way.
- If you use a single-character delimiter to delimit your string, periods and digits are not allowed as that single-character delimiter. I realize that, unlike numbers, periods as delimiters are not ambiguous (since every fourth period would necessarily be a delimiter) but for the sake of readability, I am disallowing it.
IPv4 address format
While IPv4 addresses are really just a sequence of four binary octets, this challenge uses a restricted dotted decimal format.
- An IPv4 address is a four decimal values separated by three periods.
- Each of the four values are in the range
0
to255
, inclusive. - Leading zeros are not allowed in any number value. (Standalone one-character
0
is allowed; any other number beginning with a zero is not:052
,00
, etc.)
Test Cases
Input is on the first line, output on second line (here, structured as a comma-separated list of quoted strings, separated by commas, surrounded by [
]
, but you may use any reasonable format or structure, as specified above). Some examples have notes on a third line to highlight the application of a particular rule.
192.168.1234
["192.168.1.234", "192.168.12.34", "192.168.123.4"]
192.1681234
["192.16.81.234", "192.168.1.234", "192.168.12.34", "192.168.123.4"]
(Note: 192.1681.2.34 (etc.) is illegal because 1681 is greater than 255)
1921681.234
["19.216.81.234", "192.16.81.234", "192.168.1.234"]
1921681234
["19.216.81.234", "192.16.81.234", "192.168.1.234", "192.168.12.34", "192.168.123.4"]
192.168.1204
["192.168.1.204", "192.168.120.4"]
(Note: 192.168.12.04 is illegal because of leading zero)
192.168.123
["1.92.168.123", "19.2.168.123", "192.1.68.123", "192.16.8.123", "192.168.1.23", "192.168.12.3"]
192.168.256
["192.168.2.56", "192.168.25.6"]
(Note: Any combination that would leave 256 intact is illegal)
120345
["1.20.3.45", "1.20.34.5", "1.203.4.5", "12.0.3.45", "12.0.34.5", "120.3.4.5"]
(Note: 12.03.4.5 (etc.) is illegal due to leading zero.)
012345
["0.1.23.45", "0.1.234.5", "0.12.3.45", "0.12.34.5", "0.123.4.5"]
(Note: the first segment must be 0, because `01` or `012` would be illegal.)
000123
["0.0.0.123"]
(I made these examples by hand, so please alert me to any mistakes you may find.)