-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 54
More file actions
180 lines (151 loc) · 4.19 KB
/
Copy pathproblem 54
File metadata and controls
180 lines (151 loc) · 4.19 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
from collections import Counter
def high_card(hand):
convert = {'A': 14, 'K': 13, 'Q': 12, 'J': 11, 'T': 10, '9': 9,'8': 8, '7': 7, '6': 6, '5': 5, '4': 4, '3': 3, '2': 2, '1': 1}
highest = 0
for i in hand:
if convert[i] > highest:
highest = convert[i]
return highest
def convert_to_list(s):
cards = s.split(' ')
card_value= []
card_type = []
for i in cards:
card_value.append(i[0])
card_type.append(i[1])
return [card_value, card_type]
def number_of_pairs(cards):
count = Counter(cards) - Counter(set(cards))
pairs = 0
for i in count:
if count[i] == 1:
pairs += 1
if pairs:
return pairs + 1
return 0
def three_of_a_kind(cards):
count = Counter(cards) - Counter(set(cards))
for i in count:
if count[i] == 2:
return 4
return 0
def straight(cards):
convert = {14: 'A', 13: 'K', 12: 'Q', 11: 'J', 10: 'T', 9:'9',8: '8', 7: '7', 6: '6', 5: '5', 4: '4', 3: '3', 2: '2', 1 : '1'}
bigest_num = high_card(cards)
if convert[bigest_num - 1] in cards:
if convert[bigest_num - 2] in cards:
if convert[bigest_num - 3] in cards:
if convert[bigest_num - 4] in cards:
return 5
return 0
def flush(cards):
if len(set(cards)) == 1:
return 6
return 0
def full_house(cards):
if three_of_a_kind(cards):
if number_of_pairs(cards) == 2:
return 7
return 0
def four_of_a_kind(cards):
duplicate = Counter(cards) - Counter(set(cards))
for i in duplicate:
if duplicate[i] == 3:
return 8
return 0
def straight_flush(cards, v):
if straight(cards) and flush(v):
return 9
return 0
def royal_flush(cards, v):
if set(['T', 'J', 'Q', 'K', 'A']) == set(cards):
if len(set(v)) == 1:
return 10
return 0
def paired_number(cards):
convert = {'A': 14, 'K': 13, 'Q': 12, 'J': 11, 'T': 10, '9': 9,'8': 8, '7': 7, '6': 6, '5': 5, '4': 4, '3': 3, '2': 2, '1': 1}
repeated = (Counter(cards) - Counter(set(cards))).keys()
highest = 0
for i in repeated:
if convert[i] > highest:
highest = convert[i]
return highest
f = open('poker.txt')
games = f.read()
f.close()
two_hands = games.strip().split('\n')
hands = []
for i in two_hands:
hand_1 = convert_to_list(i[:14])
hand_2 = convert_to_list(i[15:])
hands.append([hand_1, hand_2])
player1 = 0
for i in hands:
hand1_num = i[0][0]
hand2_num = i[1][0]
hand1_symbols = i[0][1]
hand2_symbols = i[1][1]
p1 = 0
p2 = 0
check = False
if number_of_pairs(hand1_num):
p1 = number_of_pairs(hand1_num)
check = True
if three_of_a_kind(hand1_num):
p1 = three_of_a_kind(hand1_num)
check = True
if straight(hand1_num):
p1 = straight(hand1_num)
check = True
if flush(hand1_symbols):
p1 = flush(hand1_symbols)
check = True
if full_house(hand1_num):
p1 = full_house(hand1_num)
check = True
if four_of_a_kind(hand1_num):
p1 = four_of_a_kind(hand1_num)
check = True
if straight_flush(hand1_num, hand1_symbols):
p1 = straight_flush(hand1_num, hand1_symbols)
check = True
if royal_flush(hand1_num, hand1_symbols):
p1 = royal_flush(hand1_num, hand1_symbols)
check = True
if number_of_pairs(hand2_num):
p2 = number_of_pairs(hand2_num)
check = True
if three_of_a_kind(hand2_num):
p2 = three_of_a_kind(hand2_num)
check = True
if straight(hand2_num):
p2 = straight(hand2_num)
check = True
if flush(hand2_symbols):
p2 = flush(hand2_symbols)
check = True
if full_house(hand2_num):
p2 = full_house(hand2_num)
check = True
if four_of_a_kind(hand2_num):
p2 = four_of_a_kind(hand2_num)
check = True
if straight_flush(hand2_num, hand2_symbols):
p2 = straight_flush(hand2_num, hand2_symbols)
check = True
if royal_flush(hand2_num, hand2_symbols):
p2 = royal_flush(hand2_num, hand2_symbols)
check = True
if p1 > p2:
player1 += 1
elif p1 == p2:
if check:
if paired_number(i[0][0]) > paired_number(i[1][0]):
player1 += 1
elif paired_number(i[0][0]) == paired_number(i[1][0]):
if high_card(i[0][0]) > high_card(i[1][0]):
player1 += 1
else:
if high_card(i[0][0]) > high_card(i[1][0]):
player1 += 1
print (player1)