MCPcopy Create free account
hub / github.com/ActiveState/code / groups

Function groups

recipes/Python/415504_Poker_hand_primitives/recipe-415504.py:41–74  ·  view source on GitHub ↗

Checks for pairs, threes-of-a-kind, fours-of-a-kind, and fives-of-a-kind Inputs: list of non-wildcards plus wildcard count 2,3,4, ... 10, 11 for Jack, 12 for Queen, 13 for King, 14 for Ace Hand can be any length (i.e. it works for seven card games) Output: tup

(hand, numwildcards=0)

Source from the content-addressed store, hash-verified

39
40
41def groups(hand, numwildcards=0):
42 """Checks for pairs, threes-of-a-kind, fours-of-a-kind,
43 and fives-of-a-kind
44
45 Inputs: list of non-wildcards plus wildcard count
46 2,3,4, ... 10, 11 for Jack, 12 for Queen,
47 13 for King, 14 for Ace
48 Hand can be any length (i.e. it works for seven card games)
49 Output: tuple with counts for each value (high cards first)
50 for example (3, 14), (2, 11) full-house Aces over Jacks
51 for example (2, 9), (2, 7) two-pair Nines and Sevens
52 Maximum count is limited to five (there is no seven of a kind).
53 Original list is not mutated.
54
55 >>> groups([11,14,11,14,14])
56 [(3, 14), (2, 11)]
57 >>> groups([7, 9, 10, 9, 7])
58 [(2, 9), (2, 7)]
59 >>> groups([11,14,11,14], 1)
60 [(3, 14), (2, 11)]
61 >>> groups([9,9,9,9,8], 2)
62 [(5, 9), (2, 8)]
63 >>> groups([], 7)
64 [(5, 14), (2, 13)]
65 """
66
67 result = []
68 counts = [(hand.count(v), v) for v in range(2,15)]
69 for c, v in sorted(counts, reverse=True):
70 newcount = min(5, c + numwildcards) # Add wildcards upto five
71 numwildcards -= newcount - c # Wildcards remaining
72 if newcount > 1:
73 result.append((newcount, v))
74 return result
75
76
77

Callers

nothing calls this directly

Calls 5

rangeFunction · 0.85
minFunction · 0.85
sortedFunction · 0.50
countMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected