This is kind of a random post, but it relates back to a previous post about Academia.

As I am finishing my PhD in July, I’m thinking about the future. I will have a position in my current lab as a Research Associate Specialist III (I think that’s what it’s called?) after I graduate while I look for a job – thanks go to my PI. This way she can pay me more than a normal post-doc, since Academia feels it necessary to limit post-doc positions to near poverty level. Anyway, I’m looking at possibly switching paths a little into coding – probably should have done this all along, I’ve been a computer nerd since I was a kid. Maybe helping out the ELN world!

So the last couple weeks, in my spare time, I have been learning Python. A friend of mine gave me an assignment he had in one have his CS classes (actually for coding in Java). It was basically a program to determine the least number of cards you would have to add to a Rummy hand in order to get all the cards into a run or set of 3 or more. The assignment involved writing three different methods. One to finish the hand getting all the cards into sets, one to finish the hand getting all the cards into runs, and one where either is permitted. The third method nobody in the class was able to really do. I thought, ‘hey I’m a math/problem solving geek, let’s do this 3rd method!’

It was a great way to really learn how to use Python - because I was trying to figure out the best algorithm to solve the problem, but then I would have to get Python to actually do what I was thinking. I became a little enthralled in the problem all Saturday. It was a little difficult with two kids jumping all over me and taking breaks to play with them, I felt like I kept losing my BIG ideas about the best way to solve it :). Eventually, at least, I felt like

**I was beating Python into submission and getting it to bend to my will**.

I won’t put my code up here yet, because it’s probably embarassing – I’m new to it so it’s written with ‘horrible style and no class.’ I probably should have started slow and got classes down and how to use them (as well as generators, looking back now). But I was more interested in just solving the problem. Eventually I actually naturally started getting a cleaner style. (I have to actually read more examples of code to get a better feel for this). Maybe I’ll make it into a game and put it up later.

So, I SOLVED IT. Kind of poorly at first, as I -brute force- would have the method add one card from each of the remaining cards in the deck and then check if it ended up completing the hand. Then try every combination of 2 cards, then 3… until it worked

The algorithm to check whether the hand was complete or not was a little involved but works quite well. I would have it check for all the possible runs or sets, then check if any run or set overlapped – if it did then I would ‘give’ the overlapping card to only either the sets or runs, see which situation was better,…

Everything worked great, but a little slow – especially when it got 5,6 cards deep in permutations, then it was hours(days-months probably if it had to go to 8+ cards). So I narrowed the cards in the deck that it would be checking – they had to be cards that would complete a set or a card 1 or 2 away in the same suit (for the runs). That sped things up alot. But then, along the same thought process, I thought I only really need to go three deep at a time and then look for which combo of 3 (or less) cards would eliminate (make those most cards be in a meld) the most cards – then add the combo, and start again. The permutation numbers go down so significantly that the program can solve any hand within a minute (usually a couple seconds).

So –

**IN YOUR FACE RUMMY!**I can now sleep again.

for example – the hand in the picture above requires at least 5 cards (below is the first solution it came up with)

2 Clubs, 2 Diamonds, Jack Clubs, Jack Hearts, Jack Spades

That’s too easy though, how about this hand:

2 Spades, 8 Spades, 9 Hearts, 4 Clubs, 6 Clubs, King Clubs, 8 Diamonds, 9 Diamonds, 10 Diamonds, King Diamonds

it takes at least eight (here’s the first solution it came up with)

5 Clubs, 8 Clubs, 9 Clubs, King Hearts, 8 Hearts, 4 Spades, 9 Spades, 3 Spades

2 Spades, 8 Spades, 9 Hearts, 4 Clubs, 6 Clubs, King Clubs, 8 Diamonds, 9 Diamonds, 10 Diamonds, King Diamonds

it takes at least eight (here’s the first solution it came up with)

5 Clubs, 8 Clubs, 9 Clubs, King Hearts, 8 Hearts, 4 Spades, 9 Spades, 3 Spades