I’ve been working my way through Chris Pine’s excellent book on ruby which sets you various challenges as you go. I got a greater sense of accomplishment from tackling these exercises than anything I did on Codecademy, in large part because I was creating files from scratch with a code editor, and then running them from the command line. The sort of virtual environment that Codecademy provides is great for giving instant feedback, but lacks a certain authenticity….
Anyway, an early task involves writing a little program that converts a number to its Roman numeral equivalent. Initially, old style Roman numerals, which are purely additive, so 4 is IIII, and 9 is VIIII. I found that task fairly straightforward, although if you look at my code you can see that I haven’t yet managed to grok modulo division. Like, at all.
The follow up task was to convert to ‘new’ Roman numerals, where 4 is IV and 9 is IX, and all that subtractive nonsense. This had me stumped for while. I worked on it without much success for the best part of a day. I found that I was still thinking about it over night, and when I woke in the morning I had some additional ideas about how to proceed, which was pretty gratifying!
I’ll present it here in all its Rube Goldbergian glory! Some features of note:
- The number gets converted to a string, because at this point I know how to split strings, but not integers.
- The inclusion of a ‘prep_array’ full of zeros, because if I’m working with 4 digit numbers, how else would I handle less digits?!
- A comment about the .drop array method, which doesn’t actually get used in the finished program. (And which I haven’t yet found the answer for, but trial and error during testing suggests ‘yes’.)
So yeah - I’m sure there are MUCH more elegant ways of solving this task, and I clearly still don’t grok modulo division, but I was very proud of myself for putting together something that worked, based on my 2 weeks or so of ruby study!
Also, this Entombed album cover suddenly makes a lot more sense:
666 = DCLXVI