Project Euler number 54. Given 1000 random hands of 5 playing cards dealt to two players, how many hands does player 1 win? We have the following notation for the cards. When I was learning python I spent some time solving the project euler problems. This is the code for all of the problems I made it through. Some of them may be pretty ugly, I was just learning.
Ok. I'll admit that part of me wishes that Husam did not post this submission, but only a small part of me thinks that way. Why? Because I am a great admirer of the Project Euler problems. They afford a great sense of satisfaction in the solving, knowing that you have used your skills in MATLAB to solve these problems. Most problems require some insight, some understanding of a mathematical concept, as well as not insignificant programming skill to solve in your chosen language. As such they are a tremendous vehicle to help you to learn to use a tool like MATLAB. I have always maintained that the best way to learn a language is to have a project that requires solving.
So I hope that most Euler solvers will choose to solve the problems on their own, first, only then look to the occasional published solutions they will find on the net.
On the other hand, this is one of the moderately easy PE problems. if this particular solution causes others to find an interest of their own in these same problems, then it is worth having posted it. As I said, my opinion is mixed here. While I am quite happy to see some advertising for others to begin the Project Euler adventure, I hope not to see solutions to each problem appear here on the FEX.
As for the code itself, I won't offer a rating because of those mixed feelings. I do very much like that the code is written in a modular fashion.
There are useful comments in the main block of code - a good start there, although the comments get sparser towards the end. The individual modules are entirely without comments though, and this is a place where those comments would have been most valuable. How for example, did the author choose to test if a hand is a flush? 3 of a kind? A tutorial code should explain itself.
Is there anything else that I might have hoped to see? Yes. Very often solving a problem like this requires the programmer to implement an encoding for the data. In this case, is there an efficient way to encode the hands for evaluation of the potential of those hands? I'd have liked to see some exposition on the data structure employed, as I can think of at least two or three ways to store them. Careful advance thought about the data structures employed is often critical to an efficient solution of the problem down the line.
Again, my point is to use this tool as a means to teach how to use matlab to solve a problem. Help the person reading your code to understand how you solved the problem.
Project Euler's problem 54 was great practice for me in writing and calling functions in Python. Here's a link to the problem. If you're a relative but not absolute beginner, like me, this problem is a good challenge.
The problem centers around poker hands. The stated goal is to find how many games player 1 wins in their given dataset. More generally, the goal is to develop a function that will tell you the better poker hand when given two hands.
To solve the problem, I wrote a function best_hand(hand1, hand2)
that itself called other functions like flush(cards)
and of_a_kind(cards, num)
. In solving the problem, I got valuable practice in making sure that my functions accepted and returned the proper types of values. All in all, my solution to the problem used 18 different functions!
My code is far from perfect, but I'm happy to share it with anyone here via PM if you send me proof that you have solved the problem already (because you're not supposed to share code). I'd also love to see your solutions, if you've already solved it (here's proof that I solved it).