2010-05-04 18:21:07 +00:00
""" How many hands did player one win in the game of poker?
In the card game poker , a hand consists of five cards and are ranked , from lowest to highest , in the following way :
High Card : Highest value card .
One Pair : Two cards of the same value .
Two Pairs : Two different pairs .
Three of a Kind : Three cards of the same value .
Straight : All cards are consecutive values .
Flush : All cards of the same suit .
Full House : Three of a kind and a pair .
Four of a Kind : Four cards of the same value .
Straight Flush : All cards are consecutive values of same suit .
Royal Flush : Ten , Jack , Queen , King , Ace , in same suit .
The cards are valued in the order :
2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , Jack , Queen , King , Ace .
If two players have the same ranked hands then the rank made up of the highest value wins ; for example , a pair of eights beats a pair of fives ( see example 1 below ) . But if two ranks tie , for example , both players have a pair of queens , then highest cards in each hand are compared ( see example 4 below ) ; if the highest cards tie then the next highest cards are compared , and so on .
Consider the following five hands dealt to two players :
Hand Player 1 Player 2 Winner
1 5 H 5 C 6 S 7 S KD 2 C 3 S 8 S 8 D TD Player 2
Pair of Fives Pair of Eights
2 5 D 8 C 9 S JS AC 2 C 5 C 7 D 8 S QH
Highest card Ace Highest Card Queen Player 1
3 2 D 9 C AS AH AC 3 D 6 D 7 D TD QD
Three Aces Flush with Diamonds Player 2
4 4 D 6 S 9 H QH QC 3 D 6 D 7 H QD QS Player 1
Pair of Queens Pair of Queens
Highest card Nine Highest card Seven
5 2 H 2 D 4 C 4 D 4 S 3 C 3 D 3 S 9 S 9 D Player 1
Full House Full House
With Three Fours With Three Threes
The file , poker . txt , contains one - thousand random hands dealt to two players . Each line of the file contains ten cards ( separated by a single space ) : the first five are Player 1 ' s cards and the last five are Player 2 ' s cards . You can assume that all hands are valid ( no invalid characters or repeated cards ) , each player ' s hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win ?
"""
2010-04-12 15:52:58 +00:00
from p054 import poker
2010-03-30 15:01:13 +00:00
2010-05-04 18:21:07 +00:00
def main ( ) :
2010-03-30 16:03:41 +00:00
wins = 0
counter = 0
2010-04-12 15:52:58 +00:00
with open ( ' p054/poker.txt ' , ' r ' ) as f :
2010-03-30 16:03:41 +00:00
while True :
line = f . readline ( )
if not line :
break
counter = counter + 1
cards = line . strip ( ) . split ( )
2010-03-31 13:54:35 +00:00
one = poker . Hand ( cards [ : 5 ] )
two = poker . Hand ( cards [ - 5 : ] )
2010-03-31 20:18:14 +00:00
result = cmp ( one , two )
2010-03-30 16:03:41 +00:00
if result > 0 :
wins = wins + 1
outcome = ' Player One wins '
elif result == 0 :
outcome = ' Tie '
else :
outcome = ' Player Two wins '
print " Hand # {0} : {1} \n {2} \n {3} " . format (
counter ,
outcome ,
one ,
two )
print " Player one won {0} hands " . format ( wins )
2010-05-04 18:21:07 +00:00
if __name__ == ' __main__ ' :
main ( )