Stuart’s MATLAB Videos

Watch and Learn

Cody challenges added: Poker Series 7

Posted by Doug Hull,

Cody is a great way to learn MATLAB. Recently, a new series of problems have been added. This series of problems will be excellent for people that are new to MATLAB to solve. There are 11 problems, that taken together will walk you down a path such that at the end, you will have some code that can compare two sets of playing cards and determine which can make the better poker hand.

The math behind this will give you a chance to practice lots of indexing, comparisons, pattern matching, flow control, cell arrays, structures, sub-functions, etc. The test suites have been designed to catch as many edge cases as possible so that to successfully finish each function, you will need to really get it right. You then will get a chance to reuse the code from each challenge to the next.

You will also be able to see how other people solve these problems. For instance, I have put my solution in there to be viewed. My code is usually very long, but easy to understand. You will also see other people using more advanced techniques that use less lines of code. This gives you a chance to see code of known functionality that uses (possibly) unfamiliar techniques.

I recommend you start with the code IsStraightFlush.

Can you make code that can tell that this matrix:

[0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 1 1 1 1 1 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 0];
contains five cards in the same row and adjacent columns, while
[0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 1 1 1 0 1 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 1 0];
does not? What about the tricky case where the Ace (column 1) is considered high?
[0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 0;
 1 0 0 0 0 0 0 0 0 1 1 1 1;
 0 0 0 0 0 0 0 0 0 0 0 0 0];

7 CommentsOldest to Newest

David Young replied on : 1 of 7

Thanks! I’ll have a go at some of these. Cody has turned out to be fun – perhaps too much fun as I’ve spent more time than I should with it. I’ve learnt a few things though.

I’m glad you emphasise the test suites. I think the weakest part of Cody so far has been the test suites, both in some of the Cody team problems and some of the community problems. This has resulted in a few solvers writing dull solutions that are just lookup tables. Sometimes solvers produce an obvious “solution” that happens to satisfy all the test cases but which doesn’t generally solve the problem, and it’s important to reject these so that people learn to program for the general case.

So: lots of hard test cases, lots of edge cases, please!

doug replied on : 2 of 7


I agree. In making my own reference solutions, I made all these edge cases for myself. It looks like there is very little ‘gaming’ of these problems because the ‘hardcoded’ solutions would me longer than the optimized solutions people are giving. I consider that a success.

What I found amusing was I thought there was a hole in one of the solvers, so I modified the test suite to ‘catch them’. Turns out, my solutions was flawed and I had to modify it to pass my own test suite. That is why we do testing! (His solution was right, BTW)


Aurélien Queffurust replied on : 3 of 7

Hi Doug!

My feedback:

1) I have never played POKER

2) Instructions to solve Poker problems are too long TO READ compared to the other CODY problem

3) TMW is more and more fun for customers: Trendy , Cody ,…

4) I will pay you 2 beers if I manage to solve any poker issue;)

My stats:
Cody : 12th player ( it is my new drug)

Trendy :”Plot for How many MATLAB jobs in South of France ?”
is number seven

MATLAB Answers : currently 32th

Frenchy : the ONLY ONE!!!


Aurélien replied on : 4 of 7

First of all , sorry for my last post.

In fact we retrieve the 3 same paragraphs at each beginning of the exercice … so in fact the instructions are not long at all to read …sorry!

I have just solved the 2 first POKER series and it request no POKER knowledge (ouf!)

They are really challenging compared to oher CODY problems : the best solutions in term of size for these 2 fisrt problems are greater than 50 …

Again sorry for my last post , and playing POKER with MATLAB is a very good idea !

Albert Yam replied on : 5 of 7

Hi Doug,
I’ve been slowly making my way through the problems. (Quite refreshing finding myself one of less than ten that complete a Problem. Even though my code is twice as long as everyone else’s.) But I have found that I am not learning as much programming techniques, as I feel as I think I could.

Which is fine, since I am doing the problems as mental exercises. I have found that, I am stuck in the way that I am thinking and laying out my code (lots of sums and finds). This may in part be due to the way the problems are progressing. Also because other solutions aren’t well commented (but still better than other Cody problems).

Having fun. Wasting time.
Thanks for posting the problems.


Doug replied on : 6 of 7


Glad you are solving the problems. You can have those promised beers delivered to MathWorks headquarters in Massachusetts. I prefer Trappist Ales.


This is an issue with mechanically scored code, the most readable (best???) code scores poorly, while the winning code tends to be terse and hard to read.

I look at the short code for clever tricks, but I look at the long code for better style. Notice that my code is very often the ‘worst’ code by scoring. It is the nature of the game.


Add A Comment

Your email address will not be published. Required fields are marked *

Preview: hide