-
Notifications
You must be signed in to change notification settings - Fork 17
/
validate-ip-address.py
52 lines (36 loc) · 1.22 KB
/
validate-ip-address.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import string
class Solution:
def validIPAddress(self, IP: str) -> str:
def validate_ipv6(ip: str) -> bool:
groups = ip.split(":")
if len(groups) != 8:
return False
for group in groups:
if len(group) < 1:
return False
for char in group:
if char.lower() not in string.hexdigits:
return False
if len(group) > 4:
return False
return True
def validate_ipv4(ip: str) -> bool:
octets = ip.split(".")
if len(octets) != 4:
return False
for octet in octets:
if len(octet) < 1:
return False
if octet.startswith("0") and len(octet) > 1:
return False
for char in octet:
if char not in string.digits:
return False
if int(octet) > 255:
return False
return True
if validate_ipv4(IP):
return "IPv4"
elif validate_ipv6(IP):
return "IPv6"
return "Neither"