# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 验证IP地址 # @param IP string字符串 一个IP地址字符串 # @return string字符串 # class Solution: def solve(self , IP: str) -> str: # write code here if "." in IP: # IPv4情况 l = IP.split('.') if len(l) !=4: # 数组长度不等于4,返回Neither return "Neither" for i in range(4): if not l[i].isdigit(): # 数组包含非数字,返回Neither return "Neither" if i == 0: # IPv4地址第一位情况 if l[0][0] =="0" or int(l[0]) not in range(1,256): return "Neither" continue if l[i][0] =="0" or int(l[i]) not in range(1,256): # IPv4地址其他位情况 return "Neither" return "IPv4" if ":" in IP: # IPv6情况 if not "::" in IP: #不包含::情况 l = IP.split(":") if len(l)!=8: return "Neither" for i in range(len(l)): if len(l[i]) not in range(1,5): return "Neither" for j in l[i]: jo = ord(j) if not ((jo >= ord('0') and jo <= ord('9')) or (jo >= ord('a') and jo <= ord('f')) or (jo >= ord('A') and jo <= ord('F'))): return "Neither" return "IPv6" return "Neither"