Problem #54 is one of the many problems on Project Euler that is very annoying to implement by hand, but can be solved quickly with the right library. The question reads:
Project Euler Problem 54: Poker hands 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 5H 5C 6S 7S KD Pair of Fives 2C 3S 8S 8D TD Pair of Eights Player 2 2 5D 8C 9S JS AC Highest card Ace 2C 5C 7D 8S QH Highest card Queen Player 1 3 2D 9C AS AH AC Three Aces 3D 6D 7D TD QD Flush with Diamonds Player 2 4 4D 6S 9H QH QC Pair of Queens Highest card Nine 3D 6D 7H QD QS Pair of Queens Highest card Seven Player 1 5 2H 2D 4C 4D 4S Full House With Three Fours 3C 3D 3S 9S 9D Full House with Three Threes Player 1 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?
As you might imagine, implementing this by hand is extremely nasty. Luckily, there are already some libraries in common languages such as Python that can solve this for us. Here is my solution:
Solution #1: Library Approach
Luckily, the Python library eval7 can solve this exact problem for us by defining the Card library which can sort the various poker hands. By going through all of the hands that are listed in the file, we can simply use this library to compare the two hands in each line and count the number of times the first player wins. Here is an implementation of this approach in Python 2.7:
'''
Author: Walker Kroubalkian
Library Approach to Project Euler Problem #54
'''
import time
import eval7
f = open("PE54Hands.txt","r")
if(f.mode == "r"):
contents = f.readlines()
realContents = []
for x in contents:
realContents.append(map(str,x.split()))
else:
raise ValueError("Cannot read from file")
finalList = realContents
def projectEulerProblemFiftyFour(myList):
total = 0
for x in myList:
a = x[0:5]
b = x[5:]
oneHand = []
for card in a:
oneHand.append(eval7.Card(card[0]+card[1].lower()))
twoHand = []
for card in b:
twoHand.append(eval7.Card(card[0]+card[1].lower()))
if(eval7.evaluate(oneHand)>eval7.evaluate(twoHand)):
total+=1
return total
start = time.time()
print projectEulerProblemFiftyFour(finalList)
print ("--- %s seconds ---" % (time.time()-start))
'''
Prints
376
--- 0.00889301300049 seconds ---
for input of finalList = given list of hands.
'''
And with that, we’re done. Knowing the right libraries can save a lot of time when solving Project Euler problems.
Thanks for reading! See you tomorrow.