Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ThinkPython/code/PokerHandSoln.py at master · DevLang-Python/ThinkPython · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
DevLang-Python
ThinkPython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ThinkPython
/
code
/
PokerHandSoln.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
226 lines (174 loc) · 5.98 KB
master
Breadcrumbs
ThinkPython
/
code
/
PokerHandSoln.py
Copy path
Top
File metadata and controls
Code
Blame
226 lines (174 loc) · 5.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""This module contains code from
Think Python by Allen B. Downey
http://thinkpython.com
Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import
sys
from
Card
import
*
class
Hist
(
dict
):
"""A map from each item (x) to its frequency."""
def
__init__
(
self
,
seq
=
[]):
"Creates a new histogram starting with the items in seq."
for
x
in
seq
:
self
.
count
(
x
)
def
count
(
self
,
x
,
f
=
1
):
"Increments the counter associated with item x."
self
[
x
]
=
self
.
get
(
x
,
0
)
+
f
if
self
[
x
]
==
0
:
del
self
[
x
]
class
PokerHand
(
Hand
):
"""Represents a poker hand."""
all_labels
=
[
'straightflush'
,
'fourkind'
,
'fullhouse'
,
'flush'
,
'straight'
,
'threekind'
,
'twopair'
,
'pair'
,
'highcard'
]
def
make_histograms
(
self
):
"""Computes histograms for suits and hands.
Creates attributes:
suits: a histogram of the suits in the hand.
ranks: a histogram of the ranks.
sets: a sorted list of the rank sets in the hand.
"""
self
.
suits
=
Hist
()
self
.
ranks
=
Hist
()
for
c
in
self
.
cards
:
self
.
suits
.
count
(
c
.
suit
)
self
.
ranks
.
count
(
c
.
rank
)
self
.
sets
=
self
.
ranks
.
values
()
self
.
sets
.
sort
(
reverse
=
True
)
def
has_highcard
(
self
):
"""Returns True if this hand has a high card."""
return
len
(
self
.
cards
)
def
check_sets
(
self
,
*
t
):
"""Checks whether self.sets contains sets that are
at least as big as the requirements in t.
t: list of int
"""
for
need
,
have
in
zip
(
t
,
self
.
sets
):
if
need
>
have
:
return
False
return
True
def
has_pair
(
self
):
"""Checks whether this hand has a pair."""
return
self
.
check_sets
(
2
)
def
has_twopair
(
self
):
"""Checks whether this hand has two pair."""
return
self
.
check_sets
(
2
,
2
)
def
has_threekind
(
self
):
"""Checks whether this hand has three of a kind."""
return
self
.
check_sets
(
3
)
def
has_fourkind
(
self
):
"""Checks whether this hand has four of a kind."""
return
self
.
check_sets
(
4
)
def
has_fullhouse
(
self
):
"""Checks whether this hand has a full house."""
return
self
.
check_sets
(
3
,
2
)
def
has_flush
(
self
):
"""Checks whether this hand has a flush."""
for
val
in
self
.
suits
.
values
():
if
val
>=
5
:
return
True
return
False
def
has_straight
(
self
):
"""Checks whether this hand has a straight."""
# make a copy of the rank histogram before we mess with it
ranks
=
self
.
ranks
.
copy
()
ranks
[
14
]
=
ranks
.
get
(
1
,
0
)
# see if we have 5 in a row
return
self
.
in_a_row
(
ranks
,
5
)
def
in_a_row
(
self
,
ranks
,
n
):
"""Checks whether the histogram has n ranks in a row.
hist: map from rank to frequency
n: number we need to get to
"""
count
=
0
for
i
in
range
(
1
,
15
):
if
ranks
.
get
(
i
,
0
):
count
+=
1
if
count
==
5
:
return
True
else
:
count
=
0
return
False
def
has_straightflush
(
self
):
"""Checks whether this hand has a straight flush.
Clumsy algorithm.
"""
# make a set of the (rank, suit) pairs we have
s
=
set
()
for
c
in
self
.
cards
:
s
.
add
((
c
.
rank
,
c
.
suit
))
if
c
.
rank
==
1
:
s
.
add
((
14
,
c
.
suit
))
# iterate through the suits and ranks and see if we
# get to 5 in a row
for
suit
in
range
(
4
):
count
=
0
for
rank
in
range
(
1
,
15
):
if
(
rank
,
suit
)
in
s
:
count
+=
1
if
count
==
5
:
return
True
else
:
count
=
0
return
False
def
has_straightflush
(
self
):
"""Checks whether this hand has a straight flush.
Better algorithm (in the sense of being more demonstrably
correct).
"""
# partition the hand by suit and check each
# sub-hand for a straight
d
=
{}
for
c
in
self
.
cards
:
d
.
setdefault
(
c
.
suit
,
PokerHand
()).
add_card
(
c
)
# see if any of the partitioned hands has a straight
for
hand
in
d
.
values
():
if
len
(
hand
.
cards
)
<
5
:
continue
hand
.
make_histograms
()
if
hand
.
has_straight
():
return
True
return
False
def
classify
(
self
):
"""Classifies this hand.
Creates attributes:
labels:
"""
self
.
make_histograms
()
self
.
labels
=
[]
for
label
in
PokerHand
.
all_labels
:
f
=
getattr
(
self
,
'has_'
+
label
)
if
f
():
self
.
labels
.
append
(
label
)
class
PokerDeck
(
Deck
):
"""Represents a deck of cards that can deal poker hands."""
def
deal_hands
(
deck
,
num_cards
=
5
,
num_hands
=
10
):
hands
=
[]
for
i
in
range
(
num_hands
):
hand
=
PokerHand
()
deck
.
move_cards
(
hand
,
num_cards
)
hand
.
classify
()
hands
.
append
(
hand
)
return
hands
def
main
(
*
args
):
# the label histogram: map from label to number of occurances
lhist
=
Hist
()
# loop n times, dealing 7 hands per iteration, 7 cards each
n
=
10000
for
i
in
range
(
n
):
if
i
%
1000
==
0
:
print
i
deck
=
PokerDeck
()
deck
.
shuffle
()
hands
=
deck
.
deal_hands
(
7
,
7
)
for
hand
in
hands
:
for
label
in
hand
.
labels
:
lhist
.
count
(
label
)
# print the results
total
=
7.0
*
n
print
total
,
'hands dealt:'
for
label
in
PokerHand
.
all_labels
:
freq
=
lhist
.
get
(
label
,
0
)
if
freq
==
0
:
continue
p
=
total
/
freq
print
'%s happens one time in %.2f'
%
(
label
,
p
)
if
__name__
==
'__main__'
:
main
(
*
sys
.
argv
)
You can’t perform that action at this time.