it is all about corner-cases...
class Solution(object):
def validIP4(self, IP):
def validNum4(s):
try:
# leading zero, negative
if (s[] == '' and len(s) > ) or s[] == '-':
return False
# value range
v = int(s)
if not (v >= and v <= ):
return False
except ValueError:
return False
return True arr = IP.split(".")
for s in arr:
if len(s) == :
return False
if not validNum4(s):
return False
return True def validIP6(self, IP):
def validNum6(s):
s = s.lower()
for c in s:
if not ((c >= '' and c <= '') or (c >= 'a' and c <= 'f')):
return False
return True arr = IP.split(":")
for s in arr:
if len(s) == or len(s) > :
return False if not validNum6(s):
return False
return True def validIPAddress(self, IP):
"""
:type IP: str
:rtype: str
"""
bIs4 = len(IP.split(".")) ==
if bIs4:
if self.validIP4(IP):
return "IPv4"
bIs6 = len(IP.split(":")) ==
if bIs6:
if self.validIP6(IP):
return "IPv6"
return "Neither"