What’s it all about?

Week 3 is all about… even more Ruby! Week 3 builds on week 2: more Ruby challenges and scoring even more points on Codewars.

What did I have to do?

This week I had to:

1. Complete chapters 9 to 14 of Chris Pine’s “Learn to Program” (“LTP”): Chris Pine’s LTP is a highly regarded intro to programming and Ruby.  As mentioned in this post, the Makers Academy interview prep requires that you complete at least chapters 1 – 8 of LTP, which can be found online here in its earlier incarnation.  Note: Chris Pine’s LTP has been subsequently expanded so buy the book if you want the latest version!

2. Score at least 70 – 80 points on Codewars: same as last week, whereby we were required to score at least 40 – 50 points to begin with.

3. Sign-up to Stackoverlow: as mentioned in previous posts, Stackoverflow is a fantastic resource of targeted questions and answers relating to code.  We were reminded to sign-up and to begin exploring the features and, if brave enough, to start using it to ask our own questions and, if possible, answer other people’s questions!

A word of warning: Stackoverflow users can be BRUTAL.  Luckily Makers Academy have pointed us to this excellent little summary of how to ask good questions on Stackoverflow.   Well worth a read before venturing out into Stackoverflow with a question!

64537256.jpg

We were also recommended to get reading The Well Grounded Rubyist (“WGR“) by David Black and Practical Object-Oriented Design (“POODR“) in Ruby by Sandi Metz.

I’d already begun reading WGR, and it’s awesome! Really concise explanations of why and how Ruby syntax and concepts work in practice.  Highly recommended if you want to really understand how and why Ruby works the way it does.

What did I achieve this week?

The Ruby I learnt

I honed my understanding of the key syntax from the week before and started exploring the following concepts in more detail through the Chris Pine LTP exercises:

  1. Classes, including creation, instance methods and instance variables
  2. Methods
  3. Return values
  4. Arrays and iterating arrays
  5. Recursion… more of that below
  6. Sorting and shuffling
  7. YAML (not Ruby, but useful nonetheless)
  8. Hashes… totally unrelated to drugs… {key => value}
  9. Ranges
  10. Procs procalot = Proc.new {|x| x+1}, Blocks and Lambdas

My highlights

Highlights included understanding and application of some basic programming knowledge to solve real world problems (see the Roman Numerals and Sort challenges from Chris Pine’s LTP below) and grasping more complex programming concepts, such as recursion.

Roman Numerals

This is a fiendishly clever little challenge from Chris Pine’s LTP that had three flavours:

  1. Translate a modern number into old-school Roman numerals, e.g.19 into XVIIII.  To the uninitiated, Roman numerals originally didn’t bother with stuff like “IX” and “IV”, instead they went in for “VIIII” and “IIII”.
  2. Translate a modern number into modern Roman numerals, i.e. the form most people are familiar with on clocks etc where you subtract the number on the left from the number on the right, e.g. 19 into IX.
  3. Translate a Roman numeral number, e.g. mcmxcix, into a modern number, e.g. 1999.

As with most coding challenges, there’s more than one way to do it, which I discovered has it’s own acronym: TMTOWTDI (pronounced “Tim Toady”).

My way of doing it tied together the following learning from this week and last:

  1. Creating a two-dimensional array (an array or arrays) called romans to pair Roman numerals to their integer values.
  2. Array iteration using a block to iterate through my romans array using the integer number being passed in for translation, which was essentially broken down into its constituent units of 1000s 500s, 400s, 100s, 90s, 50s, 40s, 10s, 9s, 5s, 4s and 1s and rewritten as a string of corresponding roman numerals.
  3. Parallel assignment (as part of the above) to assign return values to multiple variables in line.  For instance, one of the parallel assignments updated a counter that determined how many times the Roman numeral corresponding to a unit should be included in the string, e.g. if the number being translated was 1234 there would be 1 x M (for 1000), 2 x C (for 200), 3 x X (for 30) and 1 x IV (for 4).

65488698.jpg

To see my annotated solutions to these challenges see the below links:

  1. modern numbers to old school Roman numerals, e.g. 19 to XVIIII
  2. modern numbers to modern Roman numerals, e.g. 19 to IX
  3. modern Roman numerals to modern numbers, e.g. mcmxcix to 1999

Sorting

Dictionary sorting an array of strings… which means sorting a list of words into alphabetical order.

Chris Pine’s LTP includes a couple of challenges that implement sorting techniques.  The first is the aforementioned dictionary sort.

Unsure of the best way to approach this I did some Googling and stumbled upon something called the quick sort algorithm.  Intrigued I figured out how to apply this using the Ruby I’d learnt to solve this challenge.  This utilised the following:

  1. Array manipulation using partition and shift methods to respectively partition the array and remove elements from the array.
  2. Parallel assignment, which is fast becoming a favourite friend.
  3. Recursion to recursively sort the words.

Ok, so back to the quick sort algorithm.  What is it?  Well the quick sort algorithm is a highly efficient sorting algorithm based on partitioning an array of data into smaller arrays.

Briefly, it works as follows:

  1. Take a large array of values (e.g. integers).
  2. Pick a number in that array and use that value (the “pivot“) to partition the large array into two smaller arrays.
  3. One of the smaller arrays should hold values smaller than the pivot, while the other smaller array should hold values greater than the pivot.
  4. Next is where the recursion kicks in: the quick sort algorithm then repeats this process on each of the smaller arrays, i.e. picking a pivot, partitioning and creating even smaller arrays… and so on.

Make sense? Probably not.  To be honest, I didn’t fully understand how it worked until I watched this video of Hungarian folk dancers using the medium of dance to explain… trust me – watch this video and it will make sense!

 

Other notable Chris Pine challenges

Chris Pine’s LTP has plenty of other awesome challenges for the beginner Rubyist.   Further noteworthy mentions include writing a program that simulates a baby pet dragon that you can feed, tickle, put to bed and potty train and a virtual orange tree you can grow and harvest.

Check out my solutions to those challenges below:

  1. Baby pet dragon
  2. Orange tree

Recursion and M.C. Escher

Chris Pine’s LTP introduced a very important coding (and mathematical) concept called recursion.  At its most basic, recursion is a simple concept: something that recurs and references itself.

Real world examples include mirrors that reflect mirrors (e.g. in fancy hotel elevators), a lot of M.C. Escher’s famous artwork (such as the hands drawing hands drawing below) and Russian Dolls.  But when you start thinking about recursion it can feel a bit like thinking about infinity… mind boggling!

So how does this fit in with programming? Well recursion can be used to write methods that call themselves.

This is useful when performing repetitive tasks that reference themselves.   A classic coding and mathematical example being the generation of factorials.

A factorial is simply the product of all numbers 1 to n, e.g. factorial of 4 is 1 x 2 x 3 x 4 = 24.  In Ruby the factorial of 4 can be generated with the following code:

Screen Shot 2016-05-30 at 21.26.14.png

Line by line:

Line 1: I’ve created a simple method called ‘factorial’ that takes one argument, n, which will be an integer number.  In the above, I called the method on the number 4 initially in Line 6.

Line 2: This is the base case, which is essential to stop the method calling itself forever and ever and breaking the code.  In the above, once n is equal to zero the program stops and returns 1.

Line 3: This is where the recursion magic happens – the method factorial is called on itself.

Another way of visualising it is set out below (n! is mathematical shorthand for the factorial of a given number n). In the below you can get a better sense of how the recursion nests in a similiar way to how Russian dolls next references to themselves inside one another.

recursion-computer-algorithms-5-638

Indeed, the above is also how my text editor displays the successive return values, which are represented in the comments (the grey writing prefixed by # => alongside line 3 in the above screenshot), e.g. factorial of 1 = 1, factorial of 2 = 2, factorial of 3 = 6 and factorial of 4 = 24.

If that still doesn’t make sense, check out this excellent video by Joshua Cheek from his Ruby Kickstart series.

Anyway, this blog post has gotten way longer than I expected as I’ve been nerding into some of the learning from this week in too much detail! I guess that’s because it’s so damn interesting.

How did I feel during and after week 3?

Week 3 felt pretty good.  Unlike week 2 I felt week 3 consolidated a lot of my knowledge from week 2 and expanded upon certain areas in more detail, particularly the combination of different Ruby syntax and programming concepts to perform more complicated tasks.

Other positive outcomes were that:

  • I’m getting much better at Codewars through increasing familiarity with Ruby syntax and its uses.
  • I’m starting to think more systematically when breaking down a coding problem and figuring out how to solve it.

Bring on week 4!

Advertisements