Given a string query_ip, return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, or "Neither" if it is not a correct IP address of either type.
A valid IPv4 address has the form "x1.x2.x3.x4" where 0 <= xi <= 255 and xi cannot contain leading zeros. For example, "192.168.1.1" and "192.168.1.0" are valid IPv4 addresses, while "192.168.01.1", "192.168.1.00" and "192.168@1.1" are invalid IPv4 addresses.
A valid IPv6 address has the form "x1:x2:x3:x4:x5:x6:x7:x8" where:
-
1 <= xi.length <= 4
-
xi
is a hexadecimal string, which may contain digits, lowercase English letters (
'a'
to
'f'
) and uppercase English letters (
'A'
to
'F'
).
-
Leading zeros are allowed in IPv6.
For example, "2001:0db8:85a3:0000:0000:8a2e:0370:7334" and "2001:db8:85a3:0:0:8A2e:0370:7334" are valid IPv6 addresses, while "2001:0db8:85a3::8A2e:037j:7334" and "02001:0db8:85a3:0000:0000:8a2e:0370:7334" are invalid IPv6 addresses.
Function Signature
def valid_ip_address(query_ip: str) -> str:
Rules
-
Return exactly one of the strings
"IPv4"
,
"IPv6"
or
"Neither"
, with that capitalization.
-
No group may be empty, so a leading, trailing or doubled separator makes the address invalid. The shortened IPv6 form with
::
is not accepted.
-
An IPv4 group equal to
"0"
is valid. A group that starts with
0
and has more digits, such as
"01"
or
"00"
, is not.
Constraints
-
query_ip
consists only of English letters, digits and the characters
'.'
and
':'
.
Examples
Example 1
Input: query_ip = "172.16.254.1"
Output: "IPv4"
It has 4 groups separated by dots (172, 16, 254 and 1). Each is in the range 0 to 255 and none has a leading zero.
Example 2
Input: query_ip = "2001:0db8:85a3:0:0:8A2e:0370:7334"
Output: "IPv6"
It has 8 groups separated by colons, and each group is 1 to 4 hexadecimal characters. Mixed case and leading zeros such as 0db8 are allowed in IPv6.
Example 3
Input: query_ip = "256.256.256.256"
Output: "Neither"
It has the IPv4 shape, but every group is above 255. It contains no colons, so it cannot be an IPv6 address either.