import re
def solution( IP: str) -> str:
tuple1=re.split('[.:]',IP)
if len(tuple1)==4:
for i in range(4):
return "IPv4" if 0<int(tuple1[i])<=255 else "Neither"
elif len(tuple1)==8:
for i in range(8):
return "IPv6" if 1<=len(tuple1[i])<=4 and tuple1[i]!="00"and tuple1[i]!="000" and tuple1[i]!="0000" else "Neither"
else:
return "Neither"
assert solution("172.16.254.1") == "IPv4"
assert solution("2001:0db8:85a3:0:0:8A2E:0370:7334") == "IPv6"
assert solution("256.256.256.256") == "Neither"
def solution(IP: str) -> str:
import string,re
ips = re.split('[.:]',IP)
if len(ips) == 4:
for i in ips:
if not i.isdigit() or i.startswith('0') or int(i) <= 0 or int(i) >= 255:
return "Neither"
else:
return "IPv4"
elif '' not in ips and len(ips) == 8:
for i in ips:
if len(i) > 4 or not i[0].isdigit():
return "Neither"
for j in i:
if j not in string.hexdigits:
return "Neither"
return "IPv6"
else:
return "Neither"
def solution( IP: str) -> str:
if '.' in IP:
IP_list=IP.split('.')
if sum([1 for i in range(len(IP_list)) if 0<=int(IP_list[i])<=255])==4:
return "IPv4"
else:
return "Neither"
elif ':' in IP:
IP_list=IP.split(':')
for i in range(len(IP_list)):
if IP_list[i]=='' or (IP_list[i].isdigit() and len(IP_list[i])>1 and int(IP_list[i])==1):
return "Neither"
return "IPv6"
else:
return "Neither"
assert solution("172.16.254.1") == "IPv4"
assert solution("2001:0db8:85a3:0:0:8A2E:0370:7334") == "IPv6"
assert solution("256.256.256.256") == "Neither"