Google Groups Home
Help | Sign in
Message from discussion Roman Numerals (#22)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Timothy Byrd  
View profile
 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google