-
Notifications
You must be signed in to change notification settings - Fork 39
/
ipv6.py
executable file
·46 lines (30 loc) · 1.05 KB
/
ipv6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sys
result = []
def is_leading_zero(token):
return len(token) > 1 and token[0] == "0"
def is_valid_hex_digit(token):
for c in token:
if c not in "0123456789abcdef":
return False
return True
def is_valid_length_token(token):
return len(token) > 0 and len(token) < 5
def is_enough_group(ip):
return len(ip.split(":") == 8
while True:
line = sys.stdin.readline()
if not line:
print "\n".join(result) if len(result) else "EMPTY"
break
else:
if is_enough_group(ip):
tokens = ip.split(":")
for token in tokens:
bad = False
if is_valid_length_token(token) and not is_leading_zero(token) and is_valid_hex_digit(token):
bad = False
else:
bad = True
break
if not bad:
result.append(ip)