Pre-Course Wk 4: That’s F***ing Teamwork

Code pairing for the first time

Well this week’s main challenge was considerably less of an ordeal than the three previous weeks. In fact, mission already accomplished. Time to take a break and write my Antwerp article.

Presumably they kept things reasonably short and sweet because the main criterion was trying out pairing for the first time, and they didn’t want us falling out with our classmates or crying over the phone to each other before we even get properly acquainted.

I joined forces with co-student Stevie and went through a fairly straightforward FizzBuzz exercise, picking up the basic principles of RSpec testing along the way.

Some points of interest:

  • There seems to be a real art to pairing over Skype. Since we were screensharing and both needed to keep both our screens up to date as well as check loads of documentation etc. I found myself flicking between windows constantly. I may need to invest in another screen.
  • They weren’t kidding—you learn fast when you’re pairing with someone who’s better than you. (Either that or you feel ashamed fast.)
  • Communicating your thought process clearly during logic puzzles is a real challenge of its own, but it helps to structure your thinking.
  • RSpec is nicely thought out, with some clever automated features providing you keep all your files in the right folder structure. The rubytree gem is well worth a look on that note — it gives you a more complete visual of where you are in your folder structure and also has commands for moving stuff around.

It was all over pretty quick so we went and solved the shit out of our first FIVE KYU kata on Codewars.

Added bonus: it was Christmas themed. I’ll walk you through it.

5 KYU Kata: “Guess the Gifts”

You have two arrays of hashes, the presents you’ve been hoping for, called ‘wishlist’, and the mystery presents under the tree — ‘presents’.

Both arrays of hashes have values for the keys :size, :weight and :clatters. Wishlist also has values for :name.

You have to compare the two arrays and output the names of the presents where there’s a full match.

The output also has to have no duplicates.

Here’s the link:

Our Code

This was the first time I had a go at nesting an .each do iteration inside another one, and it worked a charm. One at a time, hashes in the wishlist array are checked off against every hash in the presents array, and if all three criteria are matched, the name value gets added to a result array.

The method .uniq at the end there was a good find by Stevie: it takes an array and passes it back with all duplicates removed.

The Much Better Solution

There’s another new method here, .merge, which combines two hashes into one. The first hash you call the method on, the second one you use as the parameter.

Where duplicates appear, the new version supplants the old — unless you pass a block, which lets you set specific instructions.

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
h1.merge(h2){|key, oldval, newval| newval - oldval}
#=> {"a"=>100, "b"=>54, "c"=>300}
#=> {"a"=>100, "b"=>200}

So in the solution above, he’s saying ‘one by one, merge all hashes in wishlist with all hashes in presents. If the outcome is just the same as the wishlist element (because :clatters, :size and :weight are just duplicates in the new merged hash and get removed) then add the name to the final returned array.’

Show your support

Clapping shows how much you appreciated Lawrence Hunt’s story.