Euler Problem 54 Answer Via Python

Euler problem 54, poker hands, https://projecteuler.net/problem=54

With a txt file filled with 1000 lines of poker hands from two players, the question is how many hands does Player 1 win?

I would give the answer at very bottom of this post in case of some guys like to crack it out on their own. My solution is via python program language and wrote the code on Sublime Text 2. Since I am a beginner of codes, my implementation is lousy and crappy, although it’s sure to get correct result. So here is just a way to carry out the problem, if you are unable to work it out, think need help, that my codes would at least are worthy of trying and inspiring.

Oh, no. It is shameful to paste my complete codes here, cause it is too straight and bumpy, even not optimized. Instead of that, I just illustrate the flow chart.

detect poker hands flow chart

detect poker hands flow chart

So I call two functions first, has_flush and has_straight, they are for checking whether a flush or straight exist or not. Combine with that information, I can determine the very highest poker hand each player has. But that’s not enough, cuz there would be chance the result would be a tie. So in that case, I should go for checking second highest poker hands and may continue for next compare and more, think about the situations when two players are all holding high cards and the four highest cards are same. Once finished the competition, return final result and add 1 point to due player win numbers variation. By the way, I have set up a ties variation to record all the ties, but I do not draw it in the chart, you will know the reason.

The challenges of this problem are only two parts, I believe. One is to convert to strings input into something can be calculated, another is to detect the highest poker hands. For them, I created one dictionary and two lists:

suits = {"H":1, "S":2, "D":4, "C":8}
values = ["0","1","2","3","4","5","6","7","8","9","T","J","Q","K","A"]
ranks = ["High Cards", "One Pair", "Two Pair", "Three Of A Kind", "Straight", "Flush", "Full House", "Four Of A Kind", "Straight Flush", "Loyal Flush Straight"]

The dictionary type suits are for bitwise operators to check for same suit cards. The tricks of list type of values and ranks are the index of each item indicate its grade. And meanwhile you can determine the highest poker hand via these storage types.

Ok, now that’s jump to final answer to this problem, are you ready, you’re sure? The final answer is:

The answer of problem 54 of Euler project

The answer of problem 54 of Euler project

Last words. Man, after finished this, you should really consider about making a Texas holdem application online. Of course you still have other works to do, like simple random number generator, but after all, you finished the major part of that game.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s