The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Message from discussion [SOLUTION]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 7 2005, 3:27 am
Newsgroups: comp.lang.ruby
From: Vance A Heron <he...@jpl.nasa.gov>
Date: Mon, 7 Mar 2005 17:27:20 +0900
Local: Mon, Mar 7 2005 3:27 am
Subject: Re: [QUIZ] [SOLUTION]Roman Numerals (#22)
I think the additional stipulation for "subtractors"
(other than the order of magnitude one) is that the
"subtractors" must be a power of 10 (I, X or C) *not*
V, L, or D.

That's what I coded.

Puzzled over the subtractor vs the repeats, until
I figured out one need to do both.

Here's my solution - now I'll be able to look
at everyone elses :)

Vance
--- roman.rb ---
#! /usr/bin/env ruby

def is_arabic?(n)
return n =~ /^[0-9]*\$/
end

def to_roman(n)
r = ''
rd = %w{M D C L X V I}
val = [1000, 500, 100, 50, 10, 5, 1]
val.each_with_index { |v, i|
c = (n - (n % v))/v
if c < 4 && c > 0
c.times { r << rd[i] }
n -= (c * v)
end
s = (i % 2 == 0) ? i+2 : i+1
if (s < 7) && (n >= (val[i] - val[s]))
r << rd[s] << rd[i]
n -= (val[i] - val[s])
end
}
return r
end

def to_arabic(n)
val = { 'I' => 1,   'V' => 5,   'X' => 10, 'L' => 50,
'C' => 100, 'D' => 500, 'M' => 1000 }
sum = 0
lastval = 1000
n.each_byte {|c|
sum += val[c.chr]
sum -= (lastval * 2) if val[c.chr] > lastval
lastval = val[c.chr]
}
return sum

end

f = File.open(ARGV[0])
while line = f.gets
line.chomp!
val = is_arabic?(line) ? to_roman(line.to_i) : to_arabic(line)
puts "#{line} -> #{val}"
end
----