12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

178 Chapter 18. InheritanceIS-A relationship: The relationshipbetween a childclass and itsparent class.HAS-A relationship: The relationship between two classes where instances of one class containreferences toinstances of theother.class diagram: Adiagramthatshowstheclassesinaprogramandtherelationshipsbetweenthem.multiplicity: A notation in a class diagram that shows, for a HAS-A relationship, how many referencesthereare toinstances of another class.18.11 ExercisesExercise 18.5 The following are the possible hands in poker, in increasing order of value (anddecreasing order of probability):pair: twocards withthesame ranktwopair: twopairs of cards withthe samerankthree of akind: threecards withthe samerankstraight: five cards with ranks in sequence (aces can be high or low, so Ace-2-3-4-5 is a straightand sois10-Jack-Queen-King-Ace,butQueen-King-Ace-2-3isnot.)flush: five cards withthe samesuitfullhouse: threecards withone rank, twocards withanotherfour of akind: four cards withthesame rankstraight flush: five cards insequence (as defined above) and withthesame suitThe goal of theseexercises istoestimatetheprobability ofdrawing thesevarious hands.1. Download thefollowing files fromthinkpython.com/code:Card.py : A complete version of theCard,DeckandHandclasses inthischapter.PokerHand.py : An incomplete implementation of a class that represents a poker hand, andsomecode that testsit.2. If you runPokerHand.py,it deals seven 7-card poker hands and checks to see if any of themcontains aflush. Read this code carefully before you go on.3. Add methods to PokerHand.py named has_pair, has_twopair, etc. that return True orFalseaccordingtowhetherornotthehandmeetstherelevantcriteria. Yourcodeshouldworkcorrectlyfor“hands”thatcontainanynumberofcards(although5and7arethemostcommonsizes).4. Write a method named classify that figures out the highest-value classification for a handand sets the label attribute accordingly. For example, a 7-card hand might contain a flushand apair; itshould belabeled “flush”.5. When you are convinced that your classification methods are working, the next step is toestimatetheprobabilitiesofthevarioushands. WriteafunctioninPokerHand.pythatshufflesa deck of cards, divides it into hands, classifies the hands, and counts the number of timesvarious classifications appear.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!