29 Aug 2014, 01:18
Photo on 2012-11-16 at 22.13_pragsmall

Eddie Muñoz (2 posts)

I’ve been going about this book with the process of translating the written code into spoken word. That way it’s easier to see something starts and ends. However, now that I’m at the roman numerals exercises, I can’t for the life of me understand how to read, interpret, and create my own. Specifically this code. Please pardon the lack of formatting. I’ve tried to correct it but it just goes back to this manner of display.

roman = ‘’

roman = roman + ‘M’ * (num / 1000) roman = roman + ‘D’ * (num % 1000 / 500) roman = roman + ‘C’ * (num % 500 / 100) roman = roman + ‘L’ * (num % 100 / 50) roman = roman + ‘X’ * (num % 50 / 10) roman = roman + ‘V’ * (num % 10 / 5) roman = roman + ‘I’ * (num % 5 / 1) roman

Can anyone help me break it down please?

07 Sep 2014, 04:25
Med__2008.08.19.09.44.43_pragsmall

Chris Pine (62 posts)

First, sorry about the formatting issues. Use ~~~ on the line above and below the code snippet that you want to be formatted.

Second, I hope you are writing your own solutions rather than just focusing on mine! No amount of reading code can make up for a lack of writing code. :-)

So, to answer your question:

roman = ''

roman = roman + 'M' * (self        / 1000)
roman = roman + 'D' * (self % 1000 /  500)
roman = roman + 'C' * (self %  500 /  100)
roman = roman + 'L' * (self %  100 /   50)
roman = roman + 'X' * (self %   50 /   10)
roman = roman + 'V' * (self %   10 /    5)
roman = roman + 'I' * (self %    5 /    1)

roman

The entire method is building up a single string, stored in roman. So it first starts as an empty string. Then, on each line, we reset roman to point to the string containing all of the stuff we’ve built so far, plus whatever new letters we need (which we’ll talk more about in a bit). Finally, we return the string roman.

With me so far? I just want to make sure you get the large-scale feel of what this method does before delving into the specifics.

OK. For each of the middle lines, we add all of the appropriate letter. You can see that the first line is for adding all of the 'M's, the second line then adds all of the 'D's, and so on. (When I say “all of the 'M's”, that might be none if there are no 'M's in this number.) We do this by multiplying the string of the letter we are considering on that line by the number of times we should include it.

So far so good?

All that’s left is to understand why the expressions in parentheses give us the numbers that we want. Remember that / is integer division (since these are all integers), which means that we throw away the remainder. And % is the modulus operator, which is like division except that it throws away the answer and gives us only the remainder.

Let’s work through a few examples. Let’s first try 3:

  • 3 / 1000 -> 0, so no M’s.
  • 3 % 1000 / 500 -> 3 / 500 -> 0, so no D’s.
  • 3 % 500 / 100 -> 3 / 100 -> 0, so no C’s.
  • 3 % 100 / 50 -> 3 / 50 -> 0, so no L’s.
  • 3 % 50 / 10 -> 3 / 10 -> 0, so no X’s.
  • 3 % 10 / 5 -> 3 / 5 -> 0, so no V’s.
  • 3 % 5 / 1 -> 3 / 1 -> 3, so we finally get three I’s!

So you see how the small value of 3 passed all the way down to the bottom line. Let’s try again, with 2000:

  • 2000 / 1000 -> 2, so we start with two M’s.
  • 2000 % 1000 / 500 -> 0 / 500 -> 0, so no D’s.
  • 2000 % 500 / 100 -> 0 / 100 -> 0, so no C’s.
  • 2000 % 100 / 50 -> 0 / 50 -> 0, so no L’s.
  • 2000 % 50 / 10 -> 0 / 10 -> 0, so no X’s.
  • 2000 % 10 / 5 -> 0 / 5 -> 0, so no V’s.
  • 2000 % 5 / 1 -> 0 / 1 -> 0, so no I’s.

So the M’s were pulled off right at the beginning. Finally, let’s look at 2014:

  • 2014 / 1000 -> 2, so we start with two M’s.
  • 2014 % 1000 / 500 -> 14 / 500 -> 0, so no D’s.
  • 2014 % 500 / 100 -> 14 / 100 -> 0, so no C’s.
  • 2014 % 100 / 50 -> 14 / 50 -> 0, so no L’s.
  • 2014 % 50 / 10 -> 14 / 10 -> 1, so one X.
  • 2014 % 10 / 5 -> 4 / 5 -> 0, so no V’s.
  • 2014 % 5 / 1 -> 4 / 1 -> 4, so four I’s.

Whew! Hope that made sense. :-)

Chris

06 Sep 2014, 07:22
Photo on 2012-11-16 at 22.13_pragsmall

Eddie Muñoz (2 posts)

Chris, Thank you so much for your reply. And sorry for my delay. So you said / stands for multiplication? I’m Thinking you meant to say division?

I hate to say it but I’m still a bit confused. So for your example on 2014. I’m reading on the first line where we divide 2014/ 1000

roman = 2014 + m * (2014/1000)

Is that right? or is this where I’m interpreting and going wrong?

Again, thank you for your reply,

Eddie M.

07 Sep 2014, 04:30
Med__2008.08.19.09.44.43_pragsmall

Chris Pine (62 posts)

Yes, I meant division. :) Fixed now.

For the first line, remember that roman is the string being built, not the integer. Thus, each of the additions are string additions. So the first line is like this:

  roman = '' + 'M' * (2014 / 1000)

In other words, we set roman to be an empty string plus a string of some-number-of-M’s. How many M’s? (2014 / 1000), so 2.

Does that help?

  You must be logged in to comment