Validate IP4 address Validate IP Address
I got this problem during an interview. And would like to get some code review. I also wrote several tests with the expected output, and they all passed as expected.

Validate an IP address (IPv4). An address is valid if and only if it
is in the form &quot;X.X.X.X&quot;, where each X is a number from 0 to 255.
For example, &quot;12.34.5.6&quot;, &quot;0.23.25.0&quot;, and &quot;255.255.255.255&quot; are valid
IP addresses, while &quot;12.34.56.oops&quot;, &quot;1.2.3.4.5&quot;, and
&quot;123.235.153.425&quot; are invalid IP addresses.

Examples:

&quot;&quot;&quot;    

ip = '192.168.0.1'
output: true

ip = '0.0.0.0'
output: true

ip = '123.24.59.99'
output: true

ip = '192.168.123.456'
output: false
&quot;&quot;&quot;

def validateIP(ip):
  #split them by '.' , and store them in an array
  #check the array if the length is 4 length
  arr = ip.split('.')
  if len(arr) != 4:
    return False
  #0 check for special edge cases when non-digit
  #1. check if they are digit, 
  #2. check if check the integer is between 0 and 255
  
  for part in arr:
    if len(part) &gt; 1:
      if part[0] == '0':
        return False
    if not part.isdigit():
      return False
    digit = int(part)
    if digit &lt; 0 or digit &gt; 255:
      return False
  return True

#case#0

ip0=&quot;08.0.0.0&quot; # False
test0= validateIP(ip0)
print(test0)

#case#1
ip1 = &quot;192.168.0.1&quot;
test1 = validateIP(ip1)
print(test1)

#case#2
ip2 = '0.0.0.0'
test2 = validateIP(ip2)
print(test2)

#case#3
ip3 = '123.24.59.99'
test3 = validateIP(ip3)
print(test3)

#case#4
ip4 = '192.168.123.456'
test4 = validateIP(ip4)
print(test4)

#case5
ip5 = &quot;255.255.255.255&quot;
test5 = validateIP(ip5)
print(test5)
def validateIP(ip):



I would expect a name starting is (a useful hint that it returns a Boolean rather than some more complex validation data structure) and explicitly mentioning IP v4 (since the current name is misleading). E.g. is_valid_IPv4_address.




  #split them by '.' , and store them in an array
  #check the array if the length is 4 length
  arr = ip.split('.')
  if len(arr) != 4:
    return False



The comments don't tell me anything which the code doesn't already. In general, good comments explain why, not what.




  #0 check for special edge cases when non-digit
  #1. check if they are digit, 
  #2. check if check the integer is between 0 and 255

  for part in arr:
    .. various conditions which return False
  return True



IMO it would be more Pythonic to use all: I would boil the whole function down to

    parts = ip.split('.')
    return len(parts) == 4 and all(is_valid_IPv4_address_part(part) for part in parts)





    if len(part) &gt; 1:
      if part[0] == '0':
        return False



This isn't in the spec. It's a reasonable constraint, but you should check with the person who gave you the spec before writing the code, or at least put in a comment saying that you're making an assumption about the true intentions of the specifier.




    if not part.isdigit():
      return False



This is buggy. (Before testing I thought there was an issue which should be bounced back to the specifier. Upon testing, I found that some of my test cases caused validateIP to throw an exception).

What is the expected output for these test cases?

¹.¹.¹.¹
١.١.١.١
...
①.①.①.①