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.159 tracks/python/exercises/poker/example.py
trackler-2.2.1.158 tracks/python/exercises/poker/example.py
trackler-2.2.1.157 tracks/python/exercises/poker/example.py
trackler-2.2.1.156 tracks/python/exercises/poker/example.py
trackler-2.2.1.155 tracks/python/exercises/poker/example.py
trackler-2.2.1.154 tracks/python/exercises/poker/example.py
trackler-2.2.1.153 tracks/python/exercises/poker/example.py
trackler-2.2.1.152 tracks/python/exercises/poker/example.py
trackler-2.2.1.151 tracks/python/exercises/poker/example.py
trackler-2.2.1.150 tracks/python/exercises/poker/example.py
trackler-2.2.1.149 tracks/python/exercises/poker/example.py
trackler-2.2.1.148 tracks/python/exercises/poker/example.py
trackler-2.2.1.147 tracks/python/exercises/poker/example.py
trackler-2.2.1.146 tracks/python/exercises/poker/example.py
trackler-2.2.1.145 tracks/python/exercises/poker/example.py
trackler-2.2.1.144 tracks/python/exercises/poker/example.py
trackler-2.2.1.143 tracks/python/exercises/poker/example.py
trackler-2.2.1.142 tracks/python/exercises/poker/example.py
trackler-2.2.1.141 tracks/python/exercises/poker/example.py
trackler-2.2.1.140 tracks/python/exercises/poker/example.py