All problems
Validate IPv4 Address
Problem
Return True if the string is a valid IPv4 address, and False otherwise.
An address has exactly four parts separated by dots, each part is one to three digits with no leading zeros unless it is exactly 0, and each numeric value is at most 255. The empty string is invalid.
Hint
Leading zeros are the classic trap, so check for them before the numeric range.
Test cases
case 1
in: ["192.168.1.1"]
expected: true
case 2
in: ["255.255.255.255"]
expected: true
case 3
in: ["256.1.1.1"]
expected: false
case 4
in: ["192.168.01.1"]
expected: false
case 5
in: ["1.1.1"]
expected: false
case 6
in: ["1.1.1.1.1"]
expected: false
Starter codePython
def valid_ipv4(s):
# Your code here
pass6 test cases · run your code against them in the editor