ord()取ASCII码,chr() 换成字符
class Solution:
def validIPAddress(self, queryIP: str) -> str:
orda,oraf,ordA,ordF,ordz,ordZ = ord('a'),ord('f'),ord('A'),ord('F'),ord('z'),ord('Z')
if '.' in queryIP and ':' not in queryIP:
l1 = queryIP.split('.')
if len(l1)!=4:return 'Neither'
for l in l1:
if l=='':return 'Neither'
for cha in l:
if orda<=ord(cha)<=oraf or ordA<=ord(cha)<=ordF:return 'Neither'#有英文
if '.' in l:return 'Neither'
if l[0]=='0':
if l!='0':return 'Neither'
else:
l = int(l)
if not 0<l<=255:return 'Neither'
return 'IPv4'
elif ':' in queryIP and '.' not in queryIP:
l2 = queryIP.split(':')
if len(l2)!=8: return 'Neither'
for l in l2:
for cha in l:
if oraf<ord(cha)<=ordz or ordF<ord(cha)<=ordZ:return 'Neither'#有英文
if l=='':return 'Neither'
if ':' in l:return 'Neither'
if len(l)>4:return 'Neither'
return 'IPv6'
else:return 'Neither'