Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching first and last of items in list

Tags:

python

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.

like image 253
T.J. Avatar asked Dec 31 '25 10:12

T.J.


1 Answers

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.

like image 106
Joe Iddon Avatar answered Jan 03 '26 00:01

Joe Iddon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!