题解 | #验证IP地址#
验证IP地址
https://www.nowcoder.com/practice/55fb3c68d08d46119f76ae2df7566880
class Solution:
def solve(self , IP: str) -> str:
for ip in IP:
if not ip in ".:0123456789ABCDEFabcdef":
return "Neither"
if(IP.count('.')==3):
for e in IP.split('.'):
if not e.isdigit() or int(e)>255 or str(int(e))!=e:
return "Neither"
return "IPv4"
elif(IP.count(':')==7):
for e in IP.split(':'):
if(len(e)>4 or len(e)==0):
return "Neither"
return "IPv6"
else:
return "Neither"
格力公司福利 319人发布