Ctrl K

What Kind of Address Is It

Medium
You are given a string address. Decide whether it holds a valid IPv4 address, a valid IPv6 address or neither, and return "IPv4", "IPv6" or "Neither" accordingly. **IPv4** is four groups separated by dots: a.b.c.d. Each group is a decimal number from 0 to 255 with no leading zeros. For example, "8.8.4.4" and "100.0.0.0" are fine, while "8.08.4.4", "1.2.3" and "1.2.3.256" are not. **IPv6** is eight groups separated by colons. Each group has 1 to 4 hex digits: 0–9, a–f or A–F. Leading zeros are allowed. For example, "0db8:0:0:0:0:0:0:1" is fine, while "0db8::1" (empty groups) and "12345:0:0:0:0:0:0:1" (five digits in a group) are not.
Example 1
address="10.0.255.7" → 'IPv4'

Four decimal numbers from 0 to 255 with no leading zeros, separated by dots.

Example 2
address="fe80:0:0:0:abcd:12:FF:1" → 'IPv6'

Eight groups of 1 to 4 hex digits separated by colons; letter case does not matter.

Example 3
address="10.0.07.1" → 'Neither'

The group 07 has a leading zero, so this is not IPv4, and it is not IPv6 either.

Constraints
  • 1 ≤ len(address) ≤ 100
  • address contains only English letters, digits and the characters '.' and ':'
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumLongest Mirror Fragment MediumReading a Zigzag by Rows MediumLenient Number Reader MediumNumber in Roman Numerals Medium
loading Python…
Input
address="10.0.255.7"
Expected
'IPv4'

Run checks the first 3 tests in your browser. Submit checks all 239 tests on the server, hidden ones included.