辅导Python应用程序、讲解Python设计、讲解Python Applications语言程序
- 首页 >> Python编程Assignment #4
Professor Ahmad Namini
Python and Applications to Business Analytics Fall 2018, Module 1
October 8, 2018
Exercise 1. Poker is a popular game throughout the world, whereby a player eventually
(through common cards or just their cards) has five cards which is then ranked based on
the probability that that 5-card hand rank can occur. Without the use of a wild card, the
highest hands (corresponding to the lowest possible probability of occurring) are as follows:
Hand Rank Name Notes
1 Straight Flush All cards are of the same suit and in an ordered sequence
2 Four of a Kind Four of the same rank
3 Full House Three of one rank and a pair of another rank
4 Flush All cards of of the same suit
5 Straight All cards form a ordered sequence
6 Three of a Kind Three of one rank
7 Two Pair Two pairs of the same rank
8 One Pair One pair of the same
9 High Card A hand of nothing
Card Suites are ”Hearts”, ”Spades”, ”Diamonds”, ”Clubs” while card ranks are ”2”, ”3”,
”4”, ”5”, ”6”, ”7”, ”8”, ”9”, ”10”, ”Jack”, ”Queen”, ”King”, ”Ace”.
1. Using the following code, which has classes for a card, a poker hand, and a deck of
cards, modify the code to write a computer program to determine the probability of
each hand rank.
2. Using the following code, modify the code so that after the first two cards dealt, what
is the probability of the poker hands rank conditioned on your first two cards.
import c o l l e c t i o n s
import i t e r t o o l s
import random
SUIT LIST = ( ” H e a r t s ” , ” Spades ” , ”Diamonds” , ” C lubs ” )
NUMERAL LIST = ( ”2” , ”3 ” , ”4” , ” 5” , ”6” , ” 7” , ”8” , ” 9” , ” 10 ” , ” Jack ” , ”Queen” , ”King ” , ”Ace” )
c l a s s c a rd :
def i n i t ( s e l f , numera l , s u i t ) :
s e l f . numera l = numera l
s e l f . s u i t = s u i t
s e l f . c a rd = s e l f . numera l , s e l f . s u i t
def r e p r ( s e l f ) :
return s e l f . numera l + ”?” + s e l f . s u i t
c l a s s pok er hand ( ) :
def i n i t ( s e l f , c a r d l i s t ) :
1
s e l f . c a r d l i s t = c a r d l i s t
def r e p r ( s e l f ) :
s h o r t d e s c = ” Noth ing . ”
n um e r a l d i c t = c o l l e c t i o n s . d e f a u l t d i c t ( in t )
s u i t d i c t = c o l l e c t i o n s . d e f a u l t d i c t ( in t )
fo r my card in s e l f . c a r d l i s t :
n um e r a l d i c t [ my card . numera l ] += 1
s u i t d i c t [ my card . s u i t ] += 1
# P a i r
i f len ( n um e r a l d i c t ) == 4 :
s h o r t d e s c = ”One p a i r . ”
# Two p a i r o r 3?o f?a?k i n d
e l i f len ( n um e r a l d i c t ) == 3 :
i f 3 in n um e r a l d i c t . v a l u e s ( ) :
s h o r t d e s c =”Three?o f?a?k ind . ”
e l s e :
s h o r t d e s c =”Two p a i r . ”
# F u l l h o u s e o r 4?o f?a?k i n d
e l i f len ( n um e r a l d i c t ) == 2 :
i f 2 in n um e r a l d i c t . v a l u e s ( ) :
s h o r t d e s c =” F u l l hous e . ”
e l s e :
s h o r t d e s c =”Four?o f?a?k ind . ”
e l s e :
# F l u s h e s and s t r a i g h t s
s t r a i g h t , f l u s h = F a l s e , F a l s e
i f len ( s u i t d i c t ) == 1 :
f l u s h = True
m in numera l = min ( [ NUMERAL LIST . ind e x ( x ) fo r x in n um e r a l d i c t . k ey s ( ) ] )
max numeral = max( [ NUMERAL LIST . ind e x ( x ) fo r x in n um e r a l d i c t . k ey s ( ) ] )
i f in t ( max numeral ) ? in t ( m in numera l ) == 4 :
s t r a i g h t = True
# Ace can b e l ow
l o w s t r a i g h t = se t ( ( ”Ace” , ” 2” , ”3” , ” 4” , ”5” ) )
i f not se t ( n um e r a l d i c t . k ey s ( ) ) . d i f f e r e n c e ( l o w s t r a i g h t ) :
s t r a i g h t = True
i f s t r a i g h t and not f l u s h :
s h o r t d e s c =” S t r a i g h t . ”
e l i f f l u s h and not s t r a i g h t :
s h o r t d e s c =” F lush . ”
e l i f f l u s h and s t r a i g h t :
s h o r t d e s c =” S t r a i g h t f l u s h . ”
enum erat ion = ”/” . j o i n ( [ s t r ( x ) fo r x in s e l f . c a r d l i s t ] )
return ”{ enum erat ion } ({ s h o r t d e s c }) ” . format (?? l o c a l s ( ) )
c l a s s deck ( se t ) :
def i n i t ( s e l f ) :
fo r numera l , s u i t in i t e r t o o l s . p r odu c t (NUMERAL LIST , SUIT LIST ) :
s e l f . add ( c a rd ( numera l , s u i t ) )
def g e t c a r d ( s e l f ) :
a c a r d = random . samp le ( s e l f , 1 ) [ 0 ]
s e l f . remove ( a c a r d )
return a c a r d
def g e t h and ( s e l f , n um b e r o f c a r d s =5) :
i f n um b e r o f c a r d s == 5 :
return pok er hand ( [ s e l f . g e t c a r d ( ) fo r x in range ( n um b e r o f c a r d s ) ] )
e l s e :
r a i s e NotImp lementedError
fo r i in range ( 1 0 ) :
pr int ( deck ( ) . g e t h and ( ) )