Skip to main content
Applied code formatting in the code inside the list item "you could count values instead of adding them to a list"
Source Link
  • you could count values instead of adding them to a list

    nb_valid = 0

    for current in address: for valid in valid_characters: if valid == current: nb_valid += 1

    ...
    return len(address) == nb_valid and len(address_list) == 8 and invalid_segment == False

     nb_valid = 0
    
     for current in address:
         for valid in valid_characters:
             if valid == current:
                 nb_valid += 1
    
     ...    
     return len(address) == nb_valid and len(address_list) == 8 and invalid_segment == False
    
  • you could break out of the valid_characters loops as soon as you've found a match.

  • in Python, for loops accept a else part meaning "this loop exited the normal way: it didn't encounter a break". You could use this to return False as soon as a character is invalid.

    for current in address: for valid in valid_characters: if valid == current: nb_valid += 1 break else: # nobreak - invalid character return False

  • you could count values instead of adding them to a list

    nb_valid = 0

    for current in address: for valid in valid_characters: if valid == current: nb_valid += 1

    ...
    return len(address) == nb_valid and len(address_list) == 8 and invalid_segment == False

  • you could break out of the valid_characters loops as soon as you've found a match.

  • in Python, for loops accept a else part meaning "this loop exited the normal way: it didn't encounter a break". You could use this to return False as soon as a character is invalid.

    for current in address: for valid in valid_characters: if valid == current: nb_valid += 1 break else: # nobreak - invalid character return False

  • you could count values instead of adding them to a list

     nb_valid = 0
    
     for current in address:
         for valid in valid_characters:
             if valid == current:
                 nb_valid += 1
    
     ...    
     return len(address) == nb_valid and len(address_list) == 8 and invalid_segment == False
    
  • you could break out of the valid_characters loops as soon as you've found a match.

  • in Python, for loops accept a else part meaning "this loop exited the normal way: it didn't encounter a break". You could use this to return False as soon as a character is invalid.

    for current in address: for valid in valid_characters: if valid == current: nb_valid += 1 break else: # nobreak - invalid character return False

added 215 characters in body
Source Link
SylvainD
  • 29.8k
  • 1
  • 49
  • 93

TODO

def ipv6_addr_is_valid(address):
    address_list = address.split(":")
    return len(address_list) == 8 and all(c in valid_characters for c in address) and all(len(c) <= 4 for c in address_list)

TODO

def ipv6_addr_is_valid(address):
    address_list = address.split(":")
    return len(address_list) == 8 and all(c in valid_characters for c in address) and all(len(c) <= 4 for c in address_list)
added 505 characters in body
Source Link
SylvainD
  • 29.8k
  • 1
  • 49
  • 93

You could use all/any to make some parts of your code more concise. It doesn't help too much in your case because you are using return already.

def ipv6_addr_is_valid(address):
    if any(c not in valid_characters for c in address):
        return False
    address_list = address.split(":")

    if any(len(c) > 4 for c in address_list):
        return False  # Invalid segment

    return len(address_list) == 8

ToWhich can be continuedre-organised:

TODO

To be continued


You could use all/any to make some parts of your code more concise. It doesn't help too much in your case because you are using return already.

def ipv6_addr_is_valid(address):
    if any(c not in valid_characters for c in address):
        return False
    address_list = address.split(":")

    if any(len(c) > 4 for c in address_list):
        return False  # Invalid segment

    return len(address_list) == 8

Which can be re-organised:

TODO

Source Link
SylvainD
  • 29.8k
  • 1
  • 49
  • 93
Loading