Problem


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 inclusive, 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.

    There is no first number.


Input/Output

  • [input] string inputString

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 30.

  • [output] boolean

    true if inputString satisfies the IPv4 address naming rules, false otherwise.



Solution

def isIPv4Address(inputString):

splt = inputString.split('.')

return len(splt) == 4 and all(i.isdigit() and 0 <= int(i) <= 255 for i in splt)

*splt : inputString을 점(dot)을 기준으로 나누어 반환한 값을 저장한 변수

'Programming > Algorithm' 카테고리의 다른 글

[Algorithm] avoidObstacles  (0) 2018.03.02
[Algorithm] arrayMaximalAdjacentDifference  (0) 2018.03.02
[Algorithm] areEquallyStrong  (0) 2018.03.02
[Algorithm] palindromeRearranging  (0) 2018.02.26
[Algorithm] arrayChange  (0) 2018.02.26

+ Recent posts