题解 | #验证IP地址#
大数加法
http://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 验证IP地址
# @param IP string字符串 一个IP地址字符串
# @return string字符串
#
import re
class Solution:
def solve(self , IP: str) -> str:
# write code here
if "." in IP:
list=IP.split('.')
for i in list:
l=i[0]
if i[0]=='0' or i.isdigit()==False or int(i)>255 or int(i)<0:
return "Neither"
return "IPv4"
elif ":" in IP:
list=IP.split(':')
if len(list)!=8:
return "Neither"
for i in list:
if i=="" or not re.findall(r'^[0-9a-fA-F]{1,4}$',i):
return "Neither"
return "IPv6"
else:
return "Neither"