Hello mailing list. This has already been posted on Usenet, sorry for the
duplication. (The gateway seems to be one-way at the moment.)
Dave
-----
Hi,
My solution is available here:
http://www.dave.burt.id.au/ruby/roman_numerals.rb
It can be used as a library (it pollutes String and Integer :) or as
required from the command line, filtering integers to roman numeral strings
and vice-versa.
An outline:
# Contains methods to convert integers to roman numeral strings and
vice-versa.
module RomanNumerals
# Maps roman numeral digits to their integer values
DIGITS = {
# The largest integer representable as a roman numerable by this module
MAX = 3999
# Stolen from O'Reilly's Perl Cookbook 6.23. Regular Expression Grabbag
REGEXP = /^M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$/i
# Maps some integers to their roman numeral values
@@digits_lookup = DIGITS.inject({
# Converts +int+ to a roman numeral
def self.from_integer(int)
# main loop:
@@digits_lookup.keys.sort.reverse.each do |digit_value|
while remainder >= digit_value
# Converts +roman_string+, a roman numeral, to an integer
def self.to_integer(roman_string)
# main loop:
roman_string.to_s.upcase.split(//).reverse.inject(0) do |memo, digit|
# Returns true iif +string+ is a roman numeral.
def self.is_roman_numeral?(string)
class String
# Considers string a roman numeral numeral,
# and converts it to the corresponding integer.
def to_i_roman
# Returns true iif the subject is a roman numeral.
def is_roman_numeral?
class Integer
# Converts this integer to a roman numeral.
def to_s_roman
# Processes ARGF as per Quiz requirement - converts numbers to roman
numerals and vice versa
if __FILE__ == $0
Cheers,
Dave