# Need help on a program

**URL:** <https://www.ruby-forum.com/t/need-help-on-a-program/78853>\
**Category:** Ruby\
**Created:** [December 9, 2006, 12:29pm UTC](https://www.ruby-forum.com/t/need-help-on-a-program/78853 "2006-12-09T12:29:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopherl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b38774/32.png) [@christopherl](https://www.ruby-forum.com/u/christopherl)\
**Post date:** [December 9, 2006, 12:29pm UTC](https://www.ruby-forum.com/t/need-help-on-a-program/78853/1 "2006-12-09T12:29:09Z")

</div>

Stuck on this exercise from a book:  
Old-school Roman numerals. In the early days of Roman numerals,  
the Romans didnâ€™t bother with any of this new-fangled subtraction  
â€œIXâ€ nonsense. No sir, it was straight addition, biggest to littlestâ€”  
so 9 was written â€œVIIII,â€ and so on. Write a method that, when  
passed an integer between 1 and 3000 (or so), returns a string  
containing the proper old-school Roman numeral. In other words,  
old\_roman\_numeral 4 should return ‘IIII’. Make sure to test  
your method on a bunch of different numbers.  
For reference, these are the values of the letters used:  
I =1 V =5 X = 10 L = 50  
C = 100 D = 500 M = 1000

Suggestions on a soloution?

---

<div class="post-metadata">

**Author:** ![christopherl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b38774/32.png) [@christopherl](https://www.ruby-forum.com/u/christopherl)\
**Post date:** [December 9, 2006, 2:03pm UTC](https://www.ruby-forum.com/t/need-help-on-a-program/78853/2 "2006-12-09T14:03:25Z")

</div>

> Suggestions on a soloution?  
> There’s one in the Ruby Cookbook, by O’Reilly. I am sure there would be  
> some on Google, too. I even think I recall a RubyQuiz on this topic.

---

<div class="post-metadata">

**Author:** ![christopherl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b38774/32.png) [@christopherl](https://www.ruby-forum.com/u/christopherl)\
**Post date:** [December 9, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/need-help-on-a-program/78853/3 "2006-12-09T17:51:40Z")

</div>

Christopher L. wrote:

> I =1 V =5 X = 10 L = 50  
> C = 100 D = 500 M = 1000
> 
> Suggestions on a soloution?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

class Integer  
def to\_roman  
“I =1 V =5 X = 10 L = 50  
C = 100 D = 500 M = 1000”.  
scan( / ([A-Z]) \s _= \s_ (\d+) /x ).  
map{|letter,val| [letter, val.to\_i] }.  
sort\_by{|a| -a.last}.  
inject( [“”, self] ){|roman, pair|  
[ roman.first + pair.first \* (roman.last / pair.last),  
roman.last % pair.last ] }.  
first  
end  
end

---

<div class="post-metadata">

**Author:** ![christopherl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b38774/32.png) [@christopherl](https://www.ruby-forum.com/u/christopherl)\
**Post date:** [December 9, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/need-help-on-a-program/78853/4 "2006-12-09T14:05:17Z")

</div>

On 12/9/06, Christopher L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I =1 V =5 X = 10 L = 50  
> C = 100 D = 500 M = 1000
> 
> Suggestions on a soloution?

1. skeleton (I’ll use test/unit, as it’s easier to check the results)

require ‘test/unit’

def to\_roman(int)

# here comes the code

end

class TestToRoman \< Test::Unit::TestCase

# check the base digits

def test\_roman\_digits  
assert\_equal ‘I’, to\_roman(1)  
assert\_equal ‘V’, to\_roman(5)  
assert\_equal ‘X’, to\_roman(10)  
assert\_equal ‘L’, to\_roman(50)  
assert\_equal ‘C’, to\_roman(100)  
assert\_equal ‘D’, to\_roman(500)  
assert\_equal ‘M’, to\_roman(1000)  
end

def test\_roman\_more  
assert\_equal ‘I’, to\_roman(1)  
assert\_equal ‘II’, to\_roman(2)  
assert\_equal ‘III’, to\_roman(3)  
assert\_equal ‘IIII’, to\_roman(4)

```
   assert_equal 'VI', to_roman(6)
   assert_equal 'VII', to_roman(7)
   assert_equal 'VIII', to_roman(8)
   assert_equal 'VIIII', to_roman(9)

   assert_equal 'MDCCCCLXXXVIIII', to_roman(1989)

```

end  
end

1. to the actual solution:  
(you’ll be creating the number left-to-right)

_find largest digit less than the number. subtract its value from the  
number, add the digit to the result_. repeat until the digit is larger  
than the remainder. if there’s anything left, choose the next digit.

in fact, the text between the stars is the simplest/most generic  
algorithm. the following things are just an optimisation.

The actual ruby code I’ll leave to you. First do anything that works  
(use the test above to check, and add your own ones), no matter how  
ugly it is. Then post your code, and somebody (be it me or anybody  
else) will suggest you improvements.

1. a bit of rubyism:

instead of  
def to\_roman(int)  
…  
end

and calling as to\_roman(123) you can extend the integers themselves:

class Integer  
def to\_roman

# use self insted of int parameter

end  
end

and call as 123.to\_roman - it’s nicer.

---

<div class="post-metadata">

**Author:** ![christopherl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b38774/32.png) [@christopherl](https://www.ruby-forum.com/u/christopherl)\
**Post date:** [December 10, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/need-help-on-a-program/78853/5 "2006-12-10T12:54:59Z")

</div>

class Integer  
def to\_roman  
Hash[“M”, 1000, “D”, 500, “C”, 100, “L”, 50, “X”, 10, “V”, 5, “I”,  
1].sort\_by{|a| -a.last}.inject( [“”, self] ){|roman, pair|  
[ (pair.last - roman.last ==1 )? roman.first+“I”+pair.first :  
roman.first + pair.first \* (roman.last / pair.last),  
roman.last % pair.last ] }.  
first  
end  
end

I noticed that your code is partly wrong since it will output IIII for  
4 and so on , I have modified it a little bit , but I have not checked  
the result

Regards  
On 12/9/06, William J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > For reference, these are the values of the letters used:  
> > "I =1 V =5 X = 10 L = 50

–  
Chá»‰ cÃ³ lÃ½ trÃ­ cá»§a báº£n thÃ¢n má»›i soi sÃ¡ng Ä‘Æ°á»£c sá»± tháº­t.“CÃ¡i Ä‘áº¹p Ä‘áº½ nháº¥t  
mÃ chÃºng ta cÃ³ thá»ƒ tráº£i nghiá»‡m Ä‘Æ°á»£c lÃ cÃ¡i bÃ­ áº©n. ÄÃ³ lÃ cáº£m thá»©c ná»n  
táº£ng trong cÃ¡i nÃ´i cá»§a nghá»‡ thuáº­t vÃ khoa há»c chÃ¢n chÃ­nh. Káº» nÃ o khÃ´ng  
biáº¿t Ä‘áº¿n nÃ³, khÃ´ng cÃ²n kháº£ nÄƒng ngáº¡c nhiÃªn hay kinh ngáº¡c, káº» Ä‘Ã³ coi  
nhÆ° Ä‘Ã£ cháº¿t, Ä‘Ã£ táº¯t rá»¥i lá»­a sá»‘ng trong máº¯t mÃ¬nh”“Das SchÃ¶nste, was wir  
erleben kÃ¶nnen, ist das Geheimnisvolle. Es ist das GrundgefÃ¼hl, das an  
der Wiege von wahrer Kunst und Wissenschaft steht. Wer es nicht kennt  
und sich nicht mehr wundern, nicht mehr staunen kann, der ist  
sozusagen tot und sein Auge erloschen.”“The fairest thing we can  
experience is the mysterious. It is the fundamental emotion which  
stands at the cradle of true art and true science. He who knows it not  
and can no longer wonder, no longer feel amazement, is as good as  
dead, his eyelight has been extinct.”

---

<div class="post-metadata">

**Author:** ![christopherl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b38774/32.png) [@christopherl](https://www.ruby-forum.com/u/christopherl)\
**Post date:** [December 10, 2006, 3:17pm UTC](https://www.ruby-forum.com/t/need-help-on-a-program/78853/6 "2006-12-10T15:17:13Z")

</div>

#!/usr/bin/env ruby  
@data = [  
[  
“M” , 1000],  
[“CM” , 900],  
[“D” , 500],  
“CD” , 400],  
[  
[“C” , 100],  
[  
“XC” , 90],  
“L” , 50],  
[  
“XL” , 40],  
[  
[“X” , 10],  
[“IX” , 9],  
“V” , 5],  
[  
[  
“IV” , 4],  
“I” , 1]  
[  
]  
@roman = %r{^[CDILMVX]_$}  
 @arabic = %r{^[0-9]_$}  
def to\_roman(num)  
“”  
reply =  
for key, value in @data  
count, num = num.divmod(value)  
reply \<\< (key \* count)  
end  
reply  
end  
def  
to\_arabic(rom)  
reply = 0  
for key, value in @data  
while rom.index(key) == 0  
reply += value  
rom.slice!(key)  
end  
end  
reply  
end  
$stdin.each do |line|  
case line  
when @roman  
puts to\_arabic(line)  
when @arabic  
puts to\_roman(line.to\_i)  
end  
end

* * *

Another solution from the pragmatic book : best quiz
