-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp54.py
executable file
·203 lines (185 loc) · 5.93 KB
/
p54.py
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
#!/usr/bin/env python
#
# Jack Ferguson 2018
#
# Problem 54
#
# q: Find how many times player 1 wins the poker hand
# a: 376
#
# this could be SO MUCH BETTER please change eventually
rank = {"2": 0, "3": 1, "4": 2, "5": 3, "6": 4, "7": 5, "8": 6, "9": 7, "T": 8, "J": 9, "Q": 10, "K": 11, "A": 12}
def is_flush(hand):
suit = hand[0][1]
for c in hand:
if c[1] != suit: return False
return True
def is_straight(hand):
min_rank = 13 # one more than the highest rank to start
ranks = set()
for c in hand:
r = rank[c[0]]
ranks.add(r)
if r < min_rank: min_rank = r
for i in range(1,5):
if min_rank + i not in ranks: return [False, 0]
return [True, min_rank]
def is_fullhouse(hand):
trio = find_trio(hand)
return [trio[0] and find_pair(trio[2])[0], high_card_value(hand)]
def is_royal_flush(hand):
return is_flush(hand) and is_straight(hand)[0] and is_straight(hand)[1] == 8
def is_straight_flush(hand):
return [is_flush(hand) and is_straight(hand)[0], is_straight(hand)[1]]
def is_foursome(hand):
pair = find_pair(hand)
other_pair = []
if pair[0]: other_pair = find_pair([pair[2]])
else: return [False, 0]
return [pair[0] and other_pair[0] and pair[1] == other_pair[1], pair[1]]
def find_pair(hand): # return is_pair, rank of pair, remaining cards
hand_copy = [c for c in hand]
for i in range(0, len(hand)):
for j in range(i+1, len(hand)):
if rank[hand[i][0]] == rank[hand[j][0]]: # found
ans = [True, rank[hand[i][0]]]
hand_copy.pop(i)
hand_copy.pop(j-1)
return ans + [hand_copy]
return [False, 0, hand_copy] # not found
def has_two_pairs(hand):
pair1 = find_pair(hand)
if not pair1[0]: return [False, 0]
else:
pair2 = find_pair(pair1[2])
return [pair2[0], max(pair1[1], pair2[1]), pair2[2]]
def find_trio(hand):
hand_copy = [c for c in hand]
for i in range(0, len(hand)-2):
for j in range(i+1, len(hand)-1):
for k in range(j + 1, len(hand)):
if rank[hand[i][0]] == rank[hand[j][0]] and rank[hand[j][0]] == rank[hand[k][0]]:
ans = [True, rank[hand[i][0]]]
hand_copy.pop(i)
hand_copy.pop(j-1)
hand_copy.pop(k-2)
return ans + [hand_copy]
return [False, 0, hand_copy]
def high_card_value(hand):
max_val = 0
for c in hand:
if rank[c[0]] > max_val: max_val = rank[c[0]]
return max_val
def find_winner(h1, h2):
if is_royal_flush(h1):
return 1
elif is_royal_flush(h2):
return 2
elif is_straight_flush(h1)[0]:
if is_straight_flush(h2)[0]:
if high_card_value(h1) > high_card_value(h2):
return 1 # WHAT IF SAME?
else:
return 2
else:
return 1
elif is_straight_flush(h2)[0]:
return 2
elif is_foursome(h1)[0]:
if is_foursome(h2)[0]:
if high_card_value(h1) > high_card_value(h2):
return 1 # WHAT IF SAME?
else:
return 2
elif is_foursome(h2)[0]:
return 2
elif is_fullhouse(h1)[0]:
if is_fullhouse(h2)[0]:
if high_card_value(h1) > high_card_value(h2):
return 1 # WHAT IF SAME?
else:
return 2
else:
return 1
elif is_fullhouse(h2)[0]:
return 2
elif is_flush(h1):
if is_flush(h2):
if high_card_value(h1) > high_card_value(h2):
return 1 # WHAT IF SAME?
else:
return 2
else:
return 1
elif is_flush(h2):
return 2
elif is_straight(h1)[0]:
if is_straight(h2)[0]:
if high_card_value(h1) > high_card_value(h2):
return 1 # WHAT IF SAME?
else:
return 2
else:
return 1
elif is_straight(h2)[0]:
return 2
elif find_trio(h1)[0]:
if find_trio(h2)[0]:
if find_trio(h1)[1] > find_trio(h2)[1]:
return 1
elif find_trio(h1)[1] == find_trio(h2)[1]:
if high_card_value(find_trio(h1)[2]) > high_card_value(find_trio(h2)[2]):
return 1 # WHAT IF SAME?
else:
return 2
else:
return 2
else:
return 1
elif find_trio(h2)[0]:
return 2
elif has_two_pairs(h1)[0]:
if has_two_pairs(h2)[0]:
if has_two_pairs(h1)[1] > has_two_pairs(h2)[1]:
return 1
elif has_two_pairs(h1)[1] == has_two_pairs(h2)[1]:
if has_two_pairs(h1)[2][0] > has_two_pairs(h2)[2][0]:
return 1 # WHAT IF SAME?
else:
return 2
else:
return 2
else:
return 1
elif has_two_pairs(h2)[0]:
return 2
elif find_pair(h1)[0]:
if find_pair(h2)[0]:
if find_pair(h1)[1] > find_pair(h2)[1]:
return 1
elif find_pair(h1)[1] == find_pair(h2)[1]:
if high_card_value(find_pair(h1)[2]) > high_card_value(find_pair(h2)[2]):
return 1 # WHAT IF SAME?
else:
return 2
else:
return 2
else:
return 1
elif find_pair(h2)[0]:
return 2
elif high_card_value(h1) > high_card_value(h2):
return 1 # WHAT IF SAME?
else: return 2
file_name = "data/p54_poker.txt"
p1_hands = []
p2_hands = []
ans = 0
f = open(file_name, 'r')
for line in f:
p1_hands.append(line[:-1].split(" ")[:5])
p2_hands.append(line[:-1].split(" ")[5:])
for i in range(0, len(p1_hands)):
if find_winner(p1_hands[i], p2_hands[i]) == 1:
ans += 1
print 'p54: ' + str(ans)