Roman Numerals (Arrgh!)

B

Benoit Daloze

[Note: parts of this message were removed to make it a legal post.]

I didn't miss it at all, it's quite cool, but very not easy to find directly
and to explain for an homework ...

But I admit it's quite a nice solution, not that hard to understand.

2009/11/11 Gennady Bystritsky said:
Well, being student as well, I'd love to study Ruby instead of this (****)
Java. (Where are you studying?) But that's not the point.

So, here is my piece of advice:

Using $numerals = [
[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"],
[100, "C"], [90, "XC"], [50, "L"], [40, "XL"],
[10, "X"], [9, "IX"], [5, "V"], [4, "IV"],
[1, "I"]
]

,you should iterate on each of them, seeing how much your number have of
this kind (/)
and what is the rest after that, to use it for the next step(%)...
(if you use Array#each, you'll get a new array(let's say a) like a=[dec,
romanString]. You can then take them separetely by a[0] and a[1].
Even if this method is not that beautiful in Ruby, it's the easiest way to
understand how to do it).

That the way used in a very known book of Ruby.

You just need to manage how to save this in your String at each step.
(You can multiply a String by "X" * 3, and concatenate using << (or +=))

Give us your script when you'll got one working ;)

In case you have missed it from yesterday, a slightly modified original
solution was:

def roman_numeral(number)
$numerals.inject(["", number]) { |(result, number), (order, roman)|
[ result + roman * (number / order), number % order ]
}.first
end

It works with shortened numerals for 4, 9, 40, 90, 400, 900 as well.

Gennady.
 
S

Steve Wilhelm

Benoit said:
I didn't miss it at all, it's quite cool, but very not easy to find
directly
and to explain for an homework ...

But I admit it's quite a nice solution, not that hard to understand.

2009/11/11 Gennady Bystritsky <[email protected]>

I'm going to assume the assignment has long been completed, so here is
how I would have written the function. Not as elegant as the inject
version above, but simpler for someone new to Ruby (like myself) to
understand.

def roman_numeral(number)
result, numerals = "", [
[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"],
[100, "C"], [90, "XC"], [50, "L"], [40, "XL"],
[10, "X"], [9, "IX"], [5, "V"], [4, "IV"],
[1, "I"]
]
numerals.each do |order, roman|
result << roman * (number / order)
number %= order
end
result
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Need a bit of help 1
Roman numeral conversion 3
Calculating roman numerals 12
[QUIZ] Roman Numerals (#22) 25
Roman Numerals 10
[SUMMARY] Roman Numerals (#22) 0
Help in fixing this code 8
looking for a string method 4

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top