| Bytes | Lang | Time | Link |
|---|---|---|---|
| 232 | Python 3 | 160811T144926Z | Copper |
| 091 | Perl 5 | 160812T050819Z | n̴̖̋h̷͉̃ |
| 260 | Python 3 | 160811T163817Z | YOU |
| 110 | Brachylog | 160811T170315Z | Leaky Nu |
| 024 | Pyth | 160811T185301Z | Anders K |
| 135 | JavaScript ES6 | 160811T152603Z | Neil |
| 121 | C gcc/linux | 160811T160005Z | orlp |
Python 3, 232 bytes
import re,itertools as t,ipaddress as k
R=range
i=input()
for d in R(5):
for p in t.combinations(R(len(i)),d):
n=i;o=0
for a in p:n=n[:a+o]+'.'+n[a+o:];o+=1
try:k.ip_address(n);print(n*(not re.search(r'\D?0\d',n)))
except:0
Pretty simple: We place periods everywhere and print if the IP address with the periods placed is valid. We check the validity of the IP adresses by (ab)using ipaddress.ip_address, which raises an exception if the input is not a valid IP address. The challenge defines some additional rules that ip_address doesn't handle (namely, that there can be no leading zeroes), so we check for those too with a regular expression, then print.
Outputs each solution on a new line, mixed with an arbitrary number of blank lines.
Example run:
$ echo 012345 | python fixip.py
0.1.23.45
0.1.234.5
0.12.3.45
0.12.34.5
0.123.4.5
$ echo 000123 | python fixip.py
0.0.0.123
_
Here's my older, 248-byte Python 2 solution. The second and third indent levels are \t (raw tab) and \t (raw tab plus space) respectively. This plays really badly with Markdown, so the tabs have been replaced by two spaces.
import socket,re,itertools as t
R=range
i=input()
for d in R(5):
for p in t.combinations(R(len(i)),d):
n=i;o=0
for a in p:n=n[:a+o]+'.'+n[a+o:];o+=1
try:
socket.inet_aton(n)
if n.count('.')==3and not re.search(r'\D?0\d',n):print n
except:0
Requires the input surrounded with quotes (e.g. "123.456.789"). Outputs each generated IP address on a new line.
Saved 9 bytes thanks to @grawity!
Perl 5, 91 bytes
<>=~/^(([1-9]?|1\d|2[0-4])\d|25[0-5])\.?((?1))\.?((?1))\.?((?1))$(?{print"$1.$3.$4.$5 "})^/
The program expects a single line of a single input and outputs space-delimited list of candidates.
Explanation
The program exploits the backtracking feature of regex to loop over all possibilities of forming a valid IPv4 address from the input string.
^(([1-9]?|1\d|2[0-4])\d|25[0-5])\.?((?1))\.?((?1))\.?((?1))$
The IPv4 regex with optional ., nothing of note here.
(?{print"$1.$3.$4.$5 "})
A code evaluation expression which prints out the content of the capturing groups.
^
Make the match fails and force backtracking.
Example run
$ echo "012345" | perl G89503.pl
0.12.34.5 0.12.3.45 0.1.23.45 0.1.234.5 0.123.4.5
Python 3, 262 260 bytes
p,l,L,T=set(),-1,len,tuple
while l<L(p):l=L(p);p|={T(z[:i]+(y[:j],y[j:])+z[i+1:])for z in set(p)or[T(input().split("."))]for i,y in enumerate(z)for j in range(1,L(y))}
print(['.'.join(x)for x in p if L(x)==4and all(y=='0'or y[0]!='0'and int(y)<256for y in x)])
No libraries used, but late and longer, may be I am missing some obvious golfing techniques.
Results anyway.
for x in 192.168.1234 192.1681234 1921681.234 1921681234 192.168.1204 192.168.123 192.168.256 120345 012345 000123; do
echo $x | python3 ipv4.py
done;
['192.168.123.4', '192.168.1.234', '192.168.12.34']
['192.16.81.234', '192.168.1.234', '192.168.123.4', '192.168.12.34']
['19.216.81.234', '192.168.1.234', '192.16.81.234']
['19.216.81.234', '192.168.123.4', '192.168.12.34', '192.16.81.234', '192.168.1.234']
['192.168.1.204', '192.168.120.4']
['192.16.8.123', '19.2.168.123', '1.92.168.123', '192.168.1.23', '192.168.12.3', '192.1.68.123']
['192.168.25.6', '192.168.2.56']
['1.20.3.45', '1.203.4.5', '12.0.34.5', '120.3.4.5', '1.20.34.5', '12.0.3.45']
['0.1.23.45', '0.12.3.45', '0.12.34.5', '0.123.4.5', '0.1.234.5']
['0.0.0.123']
Brachylog, 110 bytes
:ef:{".",@N.|.}ac:3f:{@n:"."rz:cacb.}a.
,4yeN,?:N:6i.@nMl4,M:{:7&<256}a
~c[A:B]h:@N:Bc.
:ef:{,"0":"9"y:.m?}ac.
Pyth, 24 bytes
f&q4lJcT\.!-J`M256jL\../
How it works
./Q all partitions of input
jL\. join each on .
f filter for results T such that:
cT\. split T on .
J assign to J
l length
q4 equals 4
& … and:
-J`M256 J minus the list of representations of [0, …, 255]
! is false (empty)
Pyth, 17 bytes, very slow
@FjLL\.,^U256 4./
Warning. Do not run. Requires approximately 553 GiB of RAM.
How it works
, two-element list of:
^U256 4 all four-element lists of [0, …, 255]
./Q all partitions of input
jLL\. join each element of both on .
@F fold intersection
JavaScript (ES6), 147 141 135 bytes
f=(s,n=0)=>(a=s.split`.`)[3]?a.every(s=>s==`0`|s[0]>0&s<256)?s+' ':'':[...s].map((_,i)=>i>n?f(s.slice(0,i)+`.`+s.slice(i),i):``).join``
<input placeholder=Input oninput=o.textContent=f(this.value)><div id=o style=font-family:monospace;width:1em>Output
Edit: saved 6 bytes thanks to @apsillers. Saved another 6 bytes by copying @YOU's validity test.
C (gcc/linux), 125 121 bytes
i;f(char*a){do{char*y=a,s[99],*x=inet_ntop(2,&i,s,99);for(;*x&&!(*x^*y&&*x^46);++x)y+=*x==*y;*x|*y||puts(s);}while(++i);}
Loops over all possible IPv4 addresses, and does a custom comparison that skips over extra dots in the generated ip address (but not in the main comparison address) to decide whether to print or not. Very slow, but should finish within 1 hour on a reasonable PC.