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 == Falsenb_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 == Falseyou could break out of the
valid_charactersloops as soon as you've found a match.in Python,
forloops accept aelsepart meaning "this loop exited the normal way: it didn't encounter abreak". You could use this to returnFalseas 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
Applied code formatting in the code inside the list item "you could count values instead of adding them to a list"
Isac
- 584
- 4
- 12