My goal is to iterate over a list of names and return True only if they can be arranged so the last letter of a name is the same as the first letter of the next name.
class Team(object):
def __init__(self, names):
self.names = names
def __iter__(self):
from collections import Counter
first = Counter(map(lambda n: n[0].lower(), self.names))
last = Counter(map(lambda n: n[-1].lower(), self.names))
diff = last - first
return any(diff.values()) <= 1
def isCoolTeam(team):
return bool(Team(team))
print(isCoolTeam(["Rob",
"Bobby",
"Billy"]))
It should return False, but for some reason every input returns true.
The function any() returns a boolean (True / False) and all booleans are less than or equal to (<=) 1.
This means that the line:
return any(diff.values()) <= 1
will always evaluate to True.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With