-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.- isIPv4Address.py
42 lines (31 loc) · 1.21 KB
/
21.- isIPv4Address.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
# pylint: disable=too-few-public-methods
# pylint: disable=invalid-name
"""
An IP address is a numerical label assigned to each device (e.g., computer, printer)
participating in a computer network that uses the Internet Protocol for communication.
There are two versions of the Internet protocol, and thus two versions of addresses.
One of them is the IPv4 address.
IPv4 addresses are represented in dot-decimal notation, which consists of four
decimal numbers, each ranging from 0 to 255, separated by dots, e.g., 172.16.254.1.
Given a string, find out if it satisfies the IPv4 address naming rules.
Example
For inputString = "172.16.254.1", the output should be
isIPv4Address(inputString) = true;
For inputString = "172.316.254.1", the output should be
isIPv4Address(inputString) = false.
316 is not in range [0, 255].
For inputString = ".254.255.0", the output should be
isIPv4Address(inputString) = false.
"""
# Code
def isIPv4Address(inputString):
if(inputString.count(".") > 3):
return False
try:
numbers = list(map(int, inputString.split(".")))
except ValueError:
return False
for number in numbers:
if (number < 0 or number > 255):
return False
return True