From: Gennady Bystritsky Date: 2009-11-12T05:16:36+09:00 Subject: Re: Roman Numerals (Arrgh!) On Nov 11, 2009, at 11:59 AM, Benoit Daloze wrote: > 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.