Sha256: 53fe89944468fa06d5db01c70e6ecd41b5a5318b0868ff1b5af53cdaa121b1a0

Contents?: true

Size: 1.09 KB

Versions: 325

Compression:

Stored size: 1.09 KB

Contents

def poker(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):
    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

325 entries across 325 versions & 1 rubygems

Version Path
trackler-2.2.1.107 tracks/python/exercises/poker/example.py
trackler-2.2.1.106 tracks/python/exercises/poker/example.py
trackler-2.2.1.105 tracks/python/exercises/poker/example.py
trackler-2.2.1.104 tracks/python/exercises/poker/example.py
trackler-2.2.1.103 tracks/python/exercises/poker/example.py
trackler-2.2.1.102 tracks/python/exercises/poker/example.py
trackler-2.2.1.101 tracks/python/exercises/poker/example.py
trackler-2.2.1.100 tracks/python/exercises/poker/example.py
trackler-2.2.1.99 tracks/python/exercises/poker/example.py
trackler-2.2.1.98 tracks/python/exercises/poker/example.py
trackler-2.2.1.97 tracks/python/exercises/poker/example.py
trackler-2.2.1.96 tracks/python/exercises/poker/example.py
trackler-2.2.1.95 tracks/python/exercises/poker/example.py
trackler-2.2.1.94 tracks/python/exercises/poker/example.py
trackler-2.2.1.93 tracks/python/exercises/poker/example.py
trackler-2.2.1.92 tracks/python/exercises/poker/example.py
trackler-2.2.1.91 tracks/python/exercises/poker/example.py
trackler-2.2.1.90 tracks/python/exercises/poker/example.py
trackler-2.2.1.89 tracks/python/exercises/poker/example.py
trackler-2.2.1.88 tracks/python/exercises/poker/example.py