What a week! I definitely stayed up a bit too late on Thursday and was a bit blunted to say the least on Friday. Fortunately it was a lot of review and recap, as well as some really cool Git stuff with githug. I finally feel like I have at least a basic grasp on rebasing and in particular interactive rebasing to squash commits (did I use that term correctly?!).
One aspect of class that can be frustrating at times but will no doubt be incredibly useful down the road is the focus on TDD. I still find it tricky to write good tests from a logical standpoint: ones that actually help me write code and foster creative and elegant thinking about the task at hand. Alex is a TDD-whiz though so I'm hoping some of it will rub off.
Next week we're starting with HTML/CSS/JS stuff and then moving onward towards rails. The pace of the class is dizzying but we're learning so much, even if some days it feels like a struggle to summon it to memory, let alone to bring it beyond that to something approaching comprehension.
One of my favorite projects from this week was to code a simple method to score Yahtzee rolls. Even though I have not actually ever played the game, I'm fond of exclaiming "Yahtzee!" whenever I figure something out. This was couched as a pair-programming exercise, and here's what Mike and I came up with.
I won't belabor the whole solution, but two cool things worth noting are the roll_count function, which allows us to easily check for two pairs, three- and four-of-a-kind. From an array of five dice rolls, it looks at @rolls.uniq to determine how many different numbers we have, then adds their score if there are more than (value) of them in the original array. Thus roll_count(4) for an @rolls of [5,5,5,5,1] would iterate over [5,1] and add 20 to the score, since only the 5 appears four or more times in the original @rolls array.
The other cool thing is how we check for a full house. The code is:
return score if @rolls.uniq.length != 2 || @rolls.permutation.to_a.uniq.length == 5
This appears near the end of a case statement asking for the user's scoring method (if you're unfamiliar with Yahtzee, players roll five dice and then choose how they are scored: pair, two-pair, etc.). We first check to ensure that @rolls.uniq.length == 2 and simply return score (which is set to 0 at the beginning of each scoring loop) if this is not the case. Obviously, for a full house there must be exactly 2 unique numbers in the rolls array or its impossible to have three of one number and two of another. Thus this first condition narrows things down to full houses or four-of-a-kind.
The other part of our OR statement is:
@rolls.permutation.to_a.uniq.length == 5
I thought this was pretty slick! We use Ruby's Array#permutation to generate an array of all possible permutations for the initial @rolls array. We then get the unique combinations thereof because #permutation doesn't account for identical indexes, and if the resultant array of arrays has a length of 5, we know that instead of a full house there are actually 4 of a kind, so in that case we just return score (=0) as well.
Bonus question: how many unique permutations are there for a full house roll? I'm all over this one since I taught the GRE for Kaplan and that stuff tends to be featured in the quant section! :]