-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp054.py
More file actions
317 lines (291 loc) · 7.87 KB
/
Copy pathp054.py
File metadata and controls
317 lines (291 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import doctest
# Learn rules
# Suites: H D C S
# Cards: 2 3 4 5 6 7 8 9 10 J Q K A
cards = {c:i for i, c in enumerate('23456789TJQKA', start=2)}
cards_keys = '23456789TJQKA'
suites = {c:i for i, c in enumerate('HDCS', start=1)}
def is_contiguous(arr):
""" return True if values of array are consecutive integers
>>> is_contiguous([2, 5, 3, 4])
True
>>> is_contiguous([9, 5, 8, 7, 6])
True
>>> is_contiguous([1, 5, 3, 4])
False
>>> is_contiguous([1, 2, 3, 0])
True
"""
mn, mx = min(arr), max(arr)
s = sum(arr)
sn = (mn*(mn-1))/2 if mn!=0 else 0
sx = (mx*(mx+1))/2
if s == sx-sn:
return True
else:
return False
def is_straight_flush(hand):
""" All cards are consecutive values of the same suite
>>> is_straight_flush('9H TH JH KH QH'.split())
(True, 13)
>>> is_straight_flush('9H TD JH KH QH'.split())
False
"""
# same suite
suite = hand[0][1]
vals = []
for c in hand:
if suite != c[1]:
return False
vals.append(cards[c[0]])
# check if vals are consecutive or not
if is_contiguous(vals):
return (True, max(vals))
else:
return False
def is_straight(hand):
""" All cards are consecutive values
>>> is_straight('9H TH JH KH QH'.split())
True
>>> is_straight('9H TD JH KH QH'.split())
True
"""
# same suite
suite = hand[0][1]
vals = []
for c in hand:
vals.append(cards[c[0]])
# check if vals are consecutive or not
if is_contiguous(vals):
return True
else:
return False
def is_full_house(hand):
""" Three of a kind and a pair
>>> is_full_house('2H 2D 4C 4D 4S'.split())
(True, 4)
"""
count = {c:0 for c in cards.keys()}
for card in hand:
count[card[0]] += 1
for c in count:
if count[c] != 0 and count[c] != 2 and count[c] != 3:
return None
triple = 0
for k in count:
if count[k] == 3:
triple = cards[k]
return (True, triple)
def is_flush(hand):
""" All cards of the same suite
>>> is_flush('3D 6D 7D TD QD'.split())
True
>>> is_flush('3D 6D 7D TH QD'.split())
False
"""
suite = hand[0][1]
for c in hand:
if suite != c[1]:
return False
return True
def is_three_of_a_kind(hand):
""" four cards of the same value
>>> is_three_of_a_kind('2H 2D 2C 2S 4H'.split())
>>> is_three_of_a_kind('2H 2D 2C 3S 4H'.split())
(True, 2)
"""
count = {c:0 for c in cards.keys()}
for card in hand:
count[card[0]] += 1
for c in count:
if count[c] == 3:
return (True, cards[c])
return None
def is_two_pairs(hand):
count = {c:0 for c in cards.keys()}
for card in hand:
count[card[0]] += 1
freq_count = {k:0 for k in range(6)}
for k in count:
freq_count[count[k]] += 1
if freq_count[2] == 2:
for k in reversed(cards_keys):
if count[k]==2:
return (True, cards[k])
return None
def is_one_pair(hand):
count = {c:0 for c in cards.keys()}
for card in hand:
count[card[0]] += 1
freq_count = {k:0 for k in range(6)}
for k in count:
freq_count[count[k]] += 1
if freq_count[2] == 1:
for k in reversed(cards_keys):
if count[k]==2:
return (True, cards[k])
return None
def highest_card(hand):
vals = []
for card in hand:
vals.append(cards[card[0]])
return max(vals)
def is_four_of_a_kind(hand):
""" four cards of the same value
>>> is_four_of_a_kind('2H 2D 2C 2S 4H'.split())
(True, 2)
>>> is_four_of_a_kind('2H 2D 2C 3S 4H'.split())
"""
count = {c:0 for c in cards.keys()}
for card in hand:
count[card[0]] += 1
for c in count:
if count[c] == 4:
return (True, cards[c])
return None
# royal flush
def is_royal_flush(hand):
""" Ten Jack Queen King Ace, from the same suite
>>> is_royal_flush('TS JS QS KS AS'.split())
True
>>> is_royal_flush('TH JS QS KS AS'.split())
False
"""
# same suit
suite = hand[0][1]
count = {c:0 for c in cards.keys()}
for c in hand:
if suite != c[1]:
return False
count[c[0]] += 1
# all in same suit
for c in 'T J Q K A'.split():
if count[c] != 1:
return False
return True
def poker(p1, p2):
p1_wins = False
# royal flush
if is_royal_flush(p1) and not is_royal_flush(p2):
# return True
p1_wins = True
# return p1_wins
return (True, 10)
if is_royal_flush(p2) and not is_royal_flush(p1):
p1_wins = False
return p1_wins
if is_royal_flush(p1) and is_royal_flush(p2):
if highest_card(p1) > highest_card(p2):
return (True, 10)
elif highest_card(p1) < highest_card(p2):
return False
# straight flush
if is_straight_flush(p1) and not is_straight_flush(p2):
p1_wins = True
return (True, 9)
if is_straight_flush(p2) and not is_straight_flush(p1):
p1_wins = False
return p1_wins
if is_straight_flush(p1) and is_straight_flush(p2):
p1_sf = is_straight_flush(p1)
p2_sf = is_straight_flush(p2)
if p1_sf[1] > p2_sf[1]:
return (True, 9)
elif p1_sf[1] < p2_sf[1]:
return False
# four of a kind
p1_4 = is_four_of_a_kind(p1)
p2_4 = is_four_of_a_kind(p2)
if p1_4 and p2_4:
if p1_4[1] > p2_4[1]:
return (True, 8)
else:
return False
elif p1_4:
return (True, 8)
elif p2_4:
return False
# full house
p1_fh = is_full_house(p1)
p2_fh = is_full_house(p2)
if p1_fh and p2_fh:
if p1_fh[1] > p2_fh[1]:
return (True, 7)
elif p1_fh[1] < p2_fh[1]:
return False
elif p1_fh:
return (True, 7)
elif p2_fh:
return False
# flush
if is_flush(p1) and not is_flush(p2):
return (True, 6)
if is_flush(p2) and not is_flush(p1):
return False
if is_flush(p1) and is_flush(p2):
if highest_card(p1) > highest_card(p2):
return (True, 6)
elif highest_card(p1) < highest_card(p2):
return False
# straight
if is_straight(p1) and not is_straight(p2):
p1_wins = (True, 5)
return p1_wins
if is_straight(p2) and not is_straight(p1):
p1_wins = False
return p1_wins
# three of a kind
p1_3 = is_three_of_a_kind(p1)
p2_3 = is_three_of_a_kind(p2)
if p1_3 and p2_3:
if p1_3[1] > p2_3[1]:
return (True, 4)
elif p1_3[1] < p2_3[1]:
return False
elif p1_3:
return (True, 4)
elif p2_3:
return False
# two pairs
p1_2 = is_two_pairs(p1)
p2_2 = is_two_pairs(p2)
if p1_2 and p2_2:
if p1_2[1] > p2_2[1]:
return (True, 3)
elif p1_2[1] < p2_2[1]:
return False
elif p1_2:
return (True, 3)
elif p2_2:
return False
# pair
p1_1p = is_one_pair(p1)
p2_1p = is_one_pair(p2)
if p1_1p and p2_1p:
if p1_1p[1] > p2_1p[1]:
return (True, 2)
elif p1_1p[1] < p2_1p[1]:
return False
elif p1_1p:
return (True, 2)
elif p2_1p:
return False
# highest card
# p1_wins = True
if highest_card(p1) > highest_card(p2):
return (True, 1)
elif highest_card(p1) < highest_card(p2):
return False
return -1000
if __name__ == '__main__':
doctest.testmod()
p1_wins = 0
with open('../data/p054_poker.txt') as f:
for l in f.readlines():
c = l.strip().split()
p1 = c[:5]
p2 = c[5:]
if poker(p1, p2):
print('P1: {} P2: {}'.format(p1, p2))
p1_wins += 1 # poker(p1, p2)
print('{} wins'.format(p1_wins))