The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Message from discussion Roman Numerals (#22)

From:
To:
Cc:
Followup To:
Subject:
 Validation: For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon.

More options Mar 6 2005, 1:43 pm
Newsgroups: comp.lang.ruby
From: "Timothy Byrd" <byrd.timo...@gmail.com>
Date: 6 Mar 2005 10:43:09 -0800
Local: Sun, Mar 6 2005 1:43 pm
Subject: Re: Roman Numerals (#22)
Since I'm new to Ruby, I may be more enthusiastic than skillful, but
here is my solution.

##########

class RomanNumerals

Roman_array = [
[ 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'  ]
]

def self.to_roman(val)
if val < 0 or val > 5000
"out of range '#{val}'"
else
s = ""
Roman_array.each { |pair|
while val >= pair[0]
s << pair[1]
val -= pair[0]
end
}
s
end
end

def self.from_roman(str)
value = 0
s = str.upcase

while s =~ /^(M|CM|D|CD|C|XC|L|XL|X|IX|V|IV|I)/
value += Roman_array.find() { |pair| pair[1] == \$1 }[0]
s = \$'  #postmatch
end

if !s.empty?
value = value.to_s + " with invalid characters '#{s}'"
end

value
end

def self.translate(s)
if s.to_i > 0
to_roman(s.to_i)
else
from_roman(s)
end
end
end

\$<.each_line { |line| puts RomanNumerals.translate(line.chomp) }

##########

-- Timothy