
Title:
0151 Summary

Description:

Now let's scroll down and look at the group function.

What group does is it takes a list of items

and then returns a list of counts of items and the item itself.

It just goes through the set of items

and returns pairs of the count

and the set and then sorts them in reverse order

so that the highest comes first.

And now this returns a list of pairs,

and we'd like to turn that into a pair of lists,

and the unzip function does that.

It's a little bit tricky.

You have to look up the zip function and apply it

to the destructured list of the pairs,

but rest assured that that just pulls out,

converts a list of pairs into a pair of lists,

and we call that up here in order to get the right answer.

Now, I'm always thinking about refactoring.

I'm also always thinking of when are you done, and when is it time not to refactor anymore?

But I was having so much fun with this, I just wanted to try one more possibility,

and that's to say rather than have a long case statement

of the different results, can we just have them in a lookup table?

And so what I did is I took all 7 of the partitions of the numbers

of the number 5 and put them into a table called "count_rankings"

and then had the corresponding poker ranking,

0 for a high card, 1 for a pair, 2 for two pair, and so on.

And now instead of having a statement here that's a long case statement

for all of the 9 possibilities, I can just say

that the result is the maximum of taking the counts,

looking them up in this table, and that will give you a value

for anything having to do with pairs or 3 or 4 of a kind.

And take the maximum of that, and this expression here,

which says 4 times straight plus 5 times flush.

Now, that might seem a little confusing, but what that's doing is

it's using automatic conversion of booleans into integers

and saying if straight is true, that corresponds to 1,

and if false, that corresponds to 0.

So if straight alone is true, that will give you a 4,

and that's the right ranking for straight.

If 5 alone is true, that will give you a 5, and that's the right ranking for flush.

And if straight and flush are true, that will give you a 9.

And what we had to do here, in the previous results

straight flush was supposed to be 8, but it's okay if it's 9.

All we have to do is take 5 of a kind and bump that up to 10,

and now we get the right rankings by taking the maximum of that.

We still have to return the ranks to break the ties,

but now in 1 line we did what took 9 lines before,

and you can decide is this what you like?

It's nice and concise.

Or do you like the previous approach because it was more explicit?

And that's a question of taste. It's up to you which one you prefer.

Now let's look at our lessons learned.

We always start by understanding the problem.

Look at the specification. See if it makes sense.

If it doesn't make sense, talk to people.

Try to figure it out. Consider different ways of trying to make sense of it.

Then define the pieces of the problem.

For our problem we had cards and hands and ranks and suits and so on.

Figure out everything there is to represent in the problem.

As much as you can, try to reuse the pieces that you have.

We used the max function.

We used the random shuffle function.

Those were sort of standard pieces that we already knew about.

Make sure you write tests.

You don't know what you've done.

You don't know what your program does unless you can specify it

both in terms of the pieces that you're defining

and in terms of the test that tells you what is it that this program does?

And then explore the design space.

We have many dimensions for the design space.

We have to deal with correctness, with efficiency,

with elegance, and with features.

Decide where in the design space you want to be.

Keep moving in the right direction, and use good taste to know when to stop.

I hope we've learned something from this.

Now let's take a look at some homework problems to really

reinforce these lessons learned.