Sha256: a8f7e7800957706b93910461b24411e0e457949fd8122b7b6e4d5820e8dcf4e2

Contents?: true

Size: 1.14 KB

Versions: 71

Compression:

Stored size: 1.14 KB

Contents

def best_hands(hands):
    return allmax(hands, key=hand_rank)


def allmax(iterable, key=None):
    result, maxval = [], None
    key = key or (lambda x: x)
    for x in iterable:
        xval = key(x)
        if not result or xval > maxval:
            result, maxval = [x], xval
        elif xval == maxval:
            result.append(x)
    return result


def hand_rank(hand):
    hand = hand.replace("10", "T").split()
    card_ranks = ["..23456789TJQKA".index(r) for r, s in hand]
    groups = [(card_ranks.count(i), i) for i in set(card_ranks)]
    groups.sort(reverse=True)
    counts, ranks = zip(*groups)
    if ranks == (14, 5, 4, 3, 2):
        ranks = (5, 4, 3, 2, 1)
    straight = (len(counts) == 5) and (max(ranks) - min(ranks) == 4)
    flush = len(set([s for r, s in hand])) == 1
    return (9 if counts == (5,) else
            8 if straight and flush else
            7 if counts == (4, 1) else
            6 if counts == (3, 2) else
            5 if flush else
            4 if straight else
            3 if counts == (3, 1, 1) else
            2 if counts == (2, 2, 1) else
            1 if counts == (2, 1, 1, 1) else
            0, ranks)

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/poker/example.py
trackler-2.2.1.179 tracks/python/exercises/poker/example.py
trackler-2.2.1.178 tracks/python/exercises/poker/example.py
trackler-2.2.1.177 tracks/python/exercises/poker/example.py
trackler-2.2.1.176 tracks/python/exercises/poker/example.py
trackler-2.2.1.175 tracks/python/exercises/poker/example.py
trackler-2.2.1.174 tracks/python/exercises/poker/example.py
trackler-2.2.1.173 tracks/python/exercises/poker/example.py
trackler-2.2.1.172 tracks/python/exercises/poker/example.py
trackler-2.2.1.171 tracks/python/exercises/poker/example.py
trackler-2.2.1.170 tracks/python/exercises/poker/example.py
trackler-2.2.1.169 tracks/python/exercises/poker/example.py
trackler-2.2.1.167 tracks/python/exercises/poker/example.py
trackler-2.2.1.166 tracks/python/exercises/poker/example.py
trackler-2.2.1.165 tracks/python/exercises/poker/example.py
trackler-2.2.1.164 tracks/python/exercises/poker/example.py
trackler-2.2.1.163 tracks/python/exercises/poker/example.py
trackler-2.2.1.162 tracks/python/exercises/poker/example.py
trackler-2.2.1.161 tracks/python/exercises/poker/example.py
trackler-2.2.1.160 tracks/python/exercises/poker/example.py