-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_ip.py
More file actions
47 lines (31 loc) · 838 Bytes
/
valid_ip.py
File metadata and controls
47 lines (31 loc) · 838 Bytes
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
""" Write a function which checks whether an IP address is valid.
>>> is_valid_ip("128.15.0.6")
True
>>> is_valid_ip("")
False
>>> is_valid_ip("another string")
False
>>> is_valid_ip("this.thing.here.10")
False
>>> is_valid_ip("0.0.0.0")
True
>>> is_valid_ip("....")
False
"""
def is_valid_ip(ip_address):
substrings = ip_address.split(".")
if len(substrings) != 4:
return False
for num in substrings:
if 1 > len(num) > 3:
return False
for char in num:
if char.isdigit() is False:
return False
if int(num) > 255:
return False
return True
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASSED. GO GO GO!\n"