texas_referee.four_of_kind()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
RANKS = "23456789TJQKA"
2
SUITS = "scdh"
3
4
from itertools import combinations
5
6
7
def straight_flush(cards_list):
8
    s = straight(cards_list)
9
    f = flush(cards_list)
10
    if s and f:
11
        for i in s:
12
            if i in f:
13
                return [i]
14
15
16
def same_rank(cards_list, num):
17
    result = []
18
    for i in combinations(cards_list, num):
19
        if len(set([j[0] for j in i])) == 1:
20
            result.append(i)
21
    return result
22
23
24
def four_of_kind(cards_list):
25
    four = same_rank(cards_list, 4)
26
    for i in four:
27
        sub_cards_list = [j for j in cards_list if j not in i]
28
        return [(list(i) + [sub_cards_list[0]])]
29
30
31
def full_house(cards_list):
32
    three = same_rank(cards_list, 3)
33
    for i in three:
34
        sub_cards_list = [j for j in cards_list if j not in i]
35
        pairs = same_rank(sub_cards_list, 2)
36
        if pairs:
37
            return [(list(i) + list(pairs[0]))]
38
39
40
def flush(cards_list):
41
    result = []
42
    for i in combinations(cards_list, 5):
43
        if len(set([j[1] for j in i])) == 1:
44
            result.append(i)
45
    return result
46
47
48
def straight(cards_list):
49
    result = []
50
    for i in combinations(cards_list, 5):
51
        rank = list(map(lambda x: x[0], i))
52
        if rank[0] - rank[-1] == 4 and len(set(rank)) == 5:
53
            result.append(i)
54
    return result
55
56
57
def three_of_a_kind(cards_list):
58
    four = same_rank(cards_list, 3)
59
    for i in four:
60
        sub_cards_list = [j for j in cards_list if j not in i]
61
        return [(list(i) + sub_cards_list[:2])]
62
63
64
def two_pair(cards_list):
65
    pairs = same_rank(cards_list, 2)
66
    if len(pairs) >= 2:
67
        two = tuple(list(pairs[0]) + list(pairs[1]))
68
        sub_cards_list = [j for j in cards_list if j not in two]
69
        return [sorted(list(two) + [sub_cards_list[0]], reverse=True)]
70
71
72
def one_pair(cards_list):
73
    pairs = same_rank(cards_list, 2)
74
    if len(pairs) == 1:
75
        two = tuple(list(pairs[0]))
76
        sub_cards_list = [j for j in cards_list if j not in two]
77
        return [sorted(list(two) + sub_cards_list[:3], reverse=True)]
78
79
80
def high_card(cards_list):
81
    return [(cards_list[:5])]
82
83
84
def texas_referee(cards_str):
85
    cards_list = sorted(
86
        [(RANKS.index(i[0]) + 2, SUITS.index(i[1])) for i in cards_str.split(',')],
87
        reverse=True,
88
    )
89
90
    hand_list = (
91
        straight_flush,
92
        four_of_kind,
93
        full_house,
94
        flush,
95
        straight,
96
        three_of_a_kind,
97
        two_pair,
98
        one_pair,
99
        high_card,
100
    )
101
    for fun in hand_list:
102
        cards = fun(cards_list)
103
        if cards:
104
            cards = [str(RANKS[i[0] - 2]) + str(SUITS[i[1]]) for i in cards[0]]
105
            return ','.join(cards)
106
107
108
if __name__ == '__main__':  # pragma: no cover
109
    # These "asserts" using only for self-checking and not necessary for
110
    # auto-testing
111
    assert (
112
        texas_referee("Kh,Qh,Ah,9s,2c,Th,Jh") == "Ah,Kh,Qh,Jh,Th"
113
    ), "High Straight Flush"
114
    assert texas_referee("Qd,Ad,9d,8d,Td,Jd,7d") == "Qd,Jd,Td,9d,8d", "Straight Flush"
115
    assert texas_referee("5c,7h,7d,9s,9c,8h,6d") == "9c,8h,7h,6d,5c", "Straight"
116
    assert texas_referee("Ts,2h,2d,3s,Td,3c,Th") == "Th,Td,Ts,3c,3s", "Full House"
117
    assert (
118
        texas_referee("Jh,Js,9h,Jd,Th,8h,Td") == "Jh,Jd,Js,Th,Td"
119
    ), "Full House vs Flush"
120
    assert texas_referee("Js,Td,8d,9s,7d,2d,4d") == "Td,8d,7d,4d,2d", "Flush"
121
    assert texas_referee("Ts,2h,Tc,3s,Td,3c,Th") == "Th,Td,Tc,Ts,3c", "Four of Kind"
122
    assert texas_referee("Ks,9h,Th,Jh,Kd,Kh,8s") == "Kh,Kd,Ks,Jh,Th", "Three of Kind"
123
    assert texas_referee("2c,3s,4s,5s,7s,2d,7h") == "7h,7s,5s,2d,2c", "Two Pairs"
124
    assert texas_referee("2s,3s,4s,5s,2d,7h,8h") == "8h,7h,5s,2d,2s", "One Pair"
125
    assert texas_referee("3h,4h,Th,6s,Ad,Jc,2h") == "Ad,Jc,Th,6s,4h", "High Cards"
126