Saturday, August 29, 2015

When in Rome ...

- What roman numeral can climb the fence?

Hello, everyone!

  I have been working my way through Chris Pine's book, and I am done with Chapter 9. The two challenges that I have been working on the last couple days are programs that would convert the modern number into a roman numerals.

 The first one was a little bit more straightforward. It would convert the number into ancient roman numeral. So a 9 would be turned into a VIIII. It is just straightforward addition, from larger to smaller. This is the code I wrote: Ancient Roman Numerals Challenge.

  The second challenge was to rewrite the code so that it converts the number into a modern roman numeral, in which in order to avoid repeating a symbol 4 times (like IIII in VIIII), you are supposed to subtract from the higher number. So a 9 would be IX. At first I thought it would be just a minor tweak to the code, and I really struggled to make it work. I had to redo pretty much the entire code from scratch. This is the final version of that program: Modern Roman Numeral Challenge

  As as a beginner, you can really get hung up on the smallest things. After I finished the above challenge, part of the code would not behave properly for some reason. It took me about an hour to realize that
     x = 2
     if x == (1 || 2 || 3)
     puts 'x is equal 1 or 2 or 3'

is not the same as

  x = 2
  if (x == 1) || (x == 2) || (x == 3)
   puts 'x is equal 1 or 2 or 3'

  As soon as I changed my code, everything worked. I understand the difference now, but at first to me both of the codes above were the same thing.

  I started Chapter 10, and I am trying to wrap my head around recursions, which is going a lot slower than I hoped, I must say...

  Thanks for tuning in,
  til next time,


No comments:

Post a Comment