[QUIZ] Roman Numerals (#22)

Discussion in 'Ruby' started by Ruby Quiz, Mar 4, 2005.

  1. Ruby Quiz

    Ruby Quiz Guest

    The three rules of Ruby Quiz:

    1. Please do not post any solutions or spoiler discussion for this quiz until
    48 hours have passed from the time on this message.

    2. Support Ruby Quiz by submitting ideas as often as you can:

    http://www.rubyquiz.com/

    3. Enjoy!

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    This week's quiz is to write a converter to and from Roman numerals.

    The script should be a standard Unix filter, reading from files specified on the
    command-line or STDIN and writing to STDOUT. Each line of input will contain
    one integer (between 1 and 3999) expressed as an Arabic or Roman numeral. There
    should be one line of output for each line of input, containing the original
    number in the opposite format.

    For example, given the following input:

    III
    29
    38
    CCXCI
    1999

    The correct output is:

    3
    XXIX
    XXXVIII
    291
    MCMXCIX

    If you're not familiar with or need a refresher on Roman numerals, the rules are
    simple. First, there are seven letters associated with seven values:

    I = 1
    V = 5
    X = 10
    L = 50
    C = 100
    D = 500
    M = 1000

    You can combine letters to add values, by listing them largest to smallest from
    left to right:

    II is 2
    VII is 8
    XXXI is 31

    However, you may only list three consecutive identical letters. That requires a
    special rule to express numbers like 4 and 900. That rule is that a single
    lower value may proceed a larger value, to indicate subtraction. This rule is
    only used to build values not reachable by the previous rules:

    IV is 4
    CM is 900

    But 15 is XV, not XVX.
     
    Ruby Quiz, Mar 4, 2005
    #1
    1. Advertising

  2. Ruby Quiz

    Bill Guindon Guest

    On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <> wrote:

    > VII is 8


    VIII is 8

    All I Ever Needed To Know I Learned from "History of the World"

    --
    Bill Guindon (aka aGorilla)
     
    Bill Guindon, Mar 4, 2005
    #2
    1. Advertising

  3. On Mar 4, 2005, at 8:10 AM, Bill Guindon wrote:

    > On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz
    > <> wrote:
    >
    >> VII is 8

    >
    > VIII is 8


    Oops. Thank you.

    James Edward Gray II
     
    James Edward Gray II, Mar 4, 2005
    #3
  4. On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <> wrote:
    > The three rules of Ruby Quiz:
    >
    > 1. Please do not post any solutions or spoiler discussion for this quiz until
    > 48 hours have passed from the time on this message.
    >
    > 2. Support Ruby Quiz by submitting ideas as often as you can:
    >
    > http://www.rubyquiz.com/
    >
    > 3. Enjoy!
    >
    > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    >
    > This week's quiz is to write a converter to and from Roman numerals.
    >
    > The script should be a standard Unix filter, reading from files specified on the
    > command-line or STDIN and writing to STDOUT. Each line of input will contain
    > one integer (between 1 and 3999) expressed as an Arabic or Roman numeral. There
    > should be one line of output for each line of input, containing the original
    > number in the opposite format.
    >
    > For example, given the following input:
    >
    > III
    > 29
    > 38
    > CCXCI
    > 1999
    >
    > The correct output is:
    >
    > 3
    > XXIX
    > XXXVIII
    > 291
    > MCMXCIX
    >
    > If you're not familiar with or need a refresher on Roman numerals, the rules are
    > simple. First, there are seven letters associated with seven values:
    >
    > I = 1
    > V = 5
    > X = 10
    > L = 50
    > C = 100
    > D = 500
    > M = 1000
    >
    > You can combine letters to add values, by listing them largest to smallest from
    > left to right:
    >
    > II is 2
    > VII is 8
    > XXXI is 31
    >
    > However, you may only list three consecutive identical letters. That requires a
    > special rule to express numbers like 4 and 900. That rule is that a single
    > lower value may proceed a larger value, to indicate subtraction. This rule is
    > only used to build values not reachable by the previous rules:
    >
    > IV is 4
    > CM is 900
    >
    > But 15 is XV, not XVX.
    >
    >


    Hello James,

    I know that in reality roman numbers didn't always follow the
    conventions given here, so everything is a little bit more complex,
    but this are more or less clear rules. Maybe one should add the
    additional rule that shorter numbers are preferred over longer ones to
    disambiguate a bit more. But the question I'm after:

    When I follow your rules, I calculate MIM for 1999, why do you propose
    the slightly less readable: MCMXCIX for this purpose? Also this does
    not seem to be consistent with XXIX for 29.

    best regards,

    Brian


    --
    Brian Schröder
    http://ruby.brian-schroeder.de/
     
    Brian Schröder, Mar 4, 2005
    #4
  5. * Ruby Quiz (Mar 04, 2005 15:00):
    > However, you may only list three consecutive identical letters. That
    > requires a special rule to express numbers like 4 and 900. That rule
    > is that a single lower value may proceed a larger value, to indicate
    > subtraction.


    These are the "new" roman numerals, the old ones were not expressed in
    this manner, rather repeating four identical "letters" (the L, C, and M
    are actually derived from older non-letter symbols but became
    letter-shaped in the end) if necessary,
    nikolai

    --
    ::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
    ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
    ::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
    main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
     
    Nikolai Weibull, Mar 4, 2005
    #5
  6. On Mar 4, 2005, at 9:13 AM, Brian Schröder wrote:

    > When I follow your rules, I calculate MIM for 1999, why do you propose
    > the slightly less readable: MCMXCIX for this purpose? Also this does
    > not seem to be consistent with XXIX for 29.


    This is just me tripping myself up it seems. I'm not trying to be
    clever. This is the standard Roman Numerals challenge.

    I was trying to make the rules as simple as possible in plain language.
    I tried to cover myself here with:

    "This rule is only used to build values not reachable by the previous
    rules"

    That probably wasn't very clear though. Let me try again

    IV is 4
    IX is 9
    XL is 40
    XC is 90
    CD is 4000
    CM is 900

    Those are the only cases where a lower value proceeds a bigger value.
    Hopefully that clears up my intentions. Sorry for the confusion.

    James Edward Gray II
     
    James Edward Gray II, Mar 4, 2005
    #6
  7. Ruby Quiz

    Bill Guindon Guest

    On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
    <> wrote:

    > IV is 4
    > IX is 9
    > XL is 40
    > XC is 90
    > CD is 4000
    > CM is 900


    CD is 400

    and I'm still waiting for History of the World Part II

    --
    Bill Guindon (aka aGorilla)
     
    Bill Guindon, Mar 4, 2005
    #7
  8. On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
    <> wrote:
    > On Mar 4, 2005, at 9:13 AM, Brian Schröder wrote:
    >
    > > When I follow your rules, I calculate MIM for 1999, why do you propose
    > > the slightly less readable: MCMXCIX for this purpose? Also this does
    > > not seem to be consistent with XXIX for 29.

    >
    > This is just me tripping myself up it seems. I'm not trying to be
    > clever. This is the standard Roman Numerals challenge.
    >
    > I was trying to make the rules as simple as possible in plain language.
    > I tried to cover myself here with:
    >
    > "This rule is only used to build values not reachable by the previous
    > rules"
    >
    > That probably wasn't very clear though. Let me try again
    >
    > IV is 4
    > IX is 9
    > XL is 40
    > XC is 90
    > CD is 4000


    400 I'd say ;)

    > CM is 900
    >
    > Those are the only cases where a lower value proceeds a bigger value.
    > Hopefully that clears up my intentions. Sorry for the confusion.
    >
    > James Edward Gray II
    >
    >



    --
    Brian Schröder
    http://ruby.brian-schroeder.de/
     
    Brian Schröder, Mar 4, 2005
    #8
  9. On Mar 4, 2005, at 9:42 AM, Bill Guindon wrote:

    > On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
    > <> wrote:
    >
    >> IV is 4
    >> IX is 9
    >> XL is 40
    >> XC is 90
    >> CD is 4000
    >> CM is 900

    >
    > CD is 400


    Egad. Good thing I have all of you to look after me!

    Thanks again.

    James Edward Gray II
     
    James Edward Gray II, Mar 4, 2005
    #9
  10. In article
    <>,
    Ruby Quiz <> wrote:

    > This week's quiz is to write a converter to and from Roman numerals.
    >
    > The script should be a standard Unix filter, reading from files specified on
    > the
    > command-line or STDIN and writing to STDOUT. Each line of input will contain
    > one integer (between 1 and 3999) expressed as an Arabic or Roman numeral.
    > There
    > should be one line of output for each line of input, containing the original
    > number in the opposite format.


    Hey, isn't the solution to this quiz already in the Pickaxe II book? :)

    --
    Karl von Laudermann - karlvonl(a)rcn.com - http://www.geocities.com/~karlvonl
    #!/usr/bin/env ruby
    c=" .,:;i|+=ahHME8";def l(a,b,c)x=b-a;y=c-a;Math.sqrt(x*x+y*y)end;25.times{|y|
    50.times{|x|print(l(12,x/2,y)<=12?((c[l(8,x/2,y).to_i]||36).chr):" ")};puts""}
     
    Karl von Laudermann, Mar 5, 2005
    #10
  11. Ruby Quiz

    Mark Hubbart Guest

    On Sat, 5 Mar 2005 00:13:56 +0900, Brian Schröder <> wrote:
    > On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <> wrote:
    > > The three rules of Ruby Quiz:
    > >
    > > 1. Please do not post any solutions or spoiler discussion for this quiz until
    > > 48 hours have passed from the time on this message.
    > >
    > > 2. Support Ruby Quiz by submitting ideas as often as you can:
    > >
    > > http://www.rubyquiz.com/
    > >
    > > 3. Enjoy!
    > >
    > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    > >
    > > This week's quiz is to write a converter to and from Roman numerals.
    > >
    > > The script should be a standard Unix filter, reading from files specified on the
    > > command-line or STDIN and writing to STDOUT. Each line of input will contain
    > > one integer (between 1 and 3999) expressed as an Arabic or Roman numeral. There
    > > should be one line of output for each line of input, containing the original
    > > number in the opposite format.
    > >
    > > For example, given the following input:
    > >
    > > III
    > > 29
    > > 38
    > > CCXCI
    > > 1999
    > >
    > > The correct output is:
    > >
    > > 3
    > > XXIX
    > > XXXVIII
    > > 291
    > > MCMXCIX
    > >
    > > If you're not familiar with or need a refresher on Roman numerals, the rules are
    > > simple. First, there are seven letters associated with seven values:
    > >
    > > I = 1
    > > V = 5
    > > X = 10
    > > L = 50
    > > C = 100
    > > D = 500
    > > M = 1000
    > >
    > > You can combine letters to add values, by listing them largest to smallest from
    > > left to right:
    > >
    > > II is 2
    > > VII is 8
    > > XXXI is 31
    > >
    > > However, you may only list three consecutive identical letters. That requires a
    > > special rule to express numbers like 4 and 900. That rule is that a single
    > > lower value may proceed a larger value, to indicate subtraction. This rule is
    > > only used to build values not reachable by the previous rules:
    > >
    > > IV is 4
    > > CM is 900
    > >
    > > But 15 is XV, not XVX.
    > >
    > >

    >
    > Hello James,
    >
    > I know that in reality roman numbers didn't always follow the
    > conventions given here, so everything is a little bit more complex,
    > but this are more or less clear rules. Maybe one should add the
    > additional rule that shorter numbers are preferred over longer ones to
    > disambiguate a bit more. But the question I'm after:
    >
    > When I follow your rules, I calculate MIM for 1999, why do you propose
    > the slightly less readable: MCMXCIX for this purpose? Also this does
    > not seem to be consistent with XXIX for 29.


    The rule of thumb as I remember it is that you can't prefix a symbol
    that is greater than one order of magnitude (base ten). So, IX is
    okay, where IC and IM are not.

    HTH,
    Mark
     
    Mark Hubbart, Mar 5, 2005
    #11
  12. Ruby Quiz

    Timothy Byrd Guest

    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
     
    Timothy Byrd, Mar 6, 2005
    #12
  13. Ruby Quiz

    Jason Bailey Guest

    Re: [SOLUTION][QUIZ] Roman Numerals (#22)

    --------------020503000008070704030306
    Content-Type: text/plain; charset=ISO-8859-1; format=flowed
    Content-Transfer-Encoding: 7bit

    I'm new to ruby so this was quite a fun little thing to help me get up
    to speed on what you can do with the language.

    since this quite small and simple I've attached it.

    thanks
    jason

    --------------020503000008070704030306
    Content-Type: text/plain;
    name="convert.rb"
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline;
    filename="convert.rb"

    #!/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 toRoman(num)
    reply = ""
    for key, value in @data
    count, num = num.divmod(value)
    reply << (key * count)
    end
    reply
    end

    def toArabic(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 toArabic(line)
    when @arabic
    puts toRoman(line.to_i)
    end
    end


    --------------020503000008070704030306--
     
    Jason Bailey, Mar 6, 2005
    #13
  14. Re: [SOLUTION][QUIZ] Roman Numerals (#22)

    On Mar 6, 2005, at 3:17 PM, Jason Bailey wrote:

    > I'm new to ruby so this was quite a fun little thing to help me get up
    > to speed on what you can do with the language.


    Well, if you're new (heck, even if you're not), I'm impressed. Nice
    solution!

    James Edward Gray II
     
    James Edward Gray II, Mar 7, 2005
    #14
  15. Re: [SOLUTION][QUIZ] Roman Numerals (#22)

    * Jason Bailey (Mar 06, 2005 22:20):
    > I'm new to ruby so this was quite a fun little thing to help me get up
    > to speed on what you can do with the language.
    >
    > since this quite small and simple I've attached it.


    Very nice.

    Time for a shameless plug:

    For the arabic-to-roman case, one could have done

    require 'lisp/format';$stdin.each{|l|Lisp.format("~@R",l.to_i)}

    But the Lisp module isn't part of the standard library...,
    nikolai

    --
    ::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
    ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
    ::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
    main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
     
    Nikolai Weibull, Mar 7, 2005
    #15
  16. Re: [SOLUTION][QUIZ] Roman Numerals (#22)

    On Mon, 7 Mar 2005 10:24:27 +0900, Nikolai Weibull
    <> wrote:
    > * Jason Bailey (Mar 06, 2005 22:20):
    > > I'm new to ruby so this was quite a fun little thing to help me get up
    > > to speed on what you can do with the language.
    > >
    > > since this quite small and simple I've attached it.

    >
    > Very nice.
    >
    > Time for a shameless plug:
    >
    > For the arabic-to-roman case, one could have done
    >
    > require 'lisp/format';$stdin.each{|l|Lisp.format("~@R",l.to_i)}
    >
    > But the Lisp module isn't part of the standard library...,
    > nikolai
    >
    > --
    > ::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
    > ::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
    > ::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
    > main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
    >
    >


    Cheater. ;)
     
    Logan Capaldo, Mar 7, 2005
    #16
  17. Ruby Quiz

    Bill Guindon Guest

    Well, it's an odd approach, but I had fun with it:

    require 'optparse'

    TITLE = "\nRoman Nvmeral Converter Version: I.IV\n"
    HELP = "
    Roman Nvmerals shall be rendered vnto Decimal.
    Decimal Nvmerals shall be rendered vnto Caesar.
    "

    ARGV.options do |opts|
    opts.banner = "Usage: ruby #{__FILE__} [options] [input files]"
    opts.on('Options:')
    opts.on("--help", "-h", "This text") { puts TITLE, '', opts, HELP; exit 0 }
    opts.parse!
    end

    DEC_MAP = %w( 0 A AA AAA AB B BA BAA BAAA AC )

    DEC_DIGIT = []
    DEC_DIGIT << {'A' => 'I', 'B' => 'V', 'C' => 'X'}
    DEC_DIGIT << {'A' => 'X', 'B' => 'L', 'C' => 'C'}
    DEC_DIGIT << {'A' => 'C', 'B' => 'D', 'C' => 'M'}
    DEC_DIGIT << {'A' => 'M', 'B' => '?', 'C' => '?'}

    ROMAN={ 'I' => 1 , 'V' => 5 , 'X' => 10 , 'L' => 50 , 'C' => 100 , 'D'
    => 500 , 'M' => 1000 }

    results = []

    ARGF.each { |number|
    number = number.upcase.chop
    roman = number =~ /^[IVXLCDM]*$/
    decimal = number =~ /^[0-9]*$/
    case

    when roman
    skip = false
    number = number.split('')
    total = 0
    number.each_with_index do |char, idx|
    nextchar = number[idx + 1]
    if skip
    skip = false
    else
    if nextchar && (ROMAN[nextchar] > ROMAN[char])
    total += ROMAN[nextchar] - ROMAN[char]
    skip = true
    else
    total += ROMAN[char]
    end
    end
    end
    results << total

    when decimal
    number = number.split('').reverse
    number.each_with_index do |place, idx|
    number[idx] = DEC_MAP[place.to_i].split('')
    number[idx].collect! do |char|
    char = DEC_DIGIT[idx][char]
    end
    number[idx].join
    end
    number = number.reverse.join('')
    results << number

    else
    results << "Nothing"
    end
    }

    results.each {|result| puts result}


    --
    Bill Guindon (aka aGorilla)
     
    Bill Guindon, Mar 7, 2005
    #17
  18. Ruby Quiz

    Dave Burt Guest

    Re: [SOLUTION][QUIZ] Roman Numerals (#22)

    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

    # 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|

    class String
    # Considers string a roman numeral numeral,
    # and converts it to the corresponding integer.
    def to_i_roman

    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
     
    Dave Burt, Mar 7, 2005
    #18
  19. Ruby Quiz

    Dave Burt Guest

    "Bill Guindon" <> rubied:
    > DEC_MAP = %w( 0 A AA AAA AB B BA BAA BAAA AC )
    >
    > DEC_DIGIT = []
    > DEC_DIGIT << {'A' => 'I', 'B' => 'V', 'C' => 'X'}
    > DEC_DIGIT << {'A' => 'X', 'B' => 'L', 'C' => 'C'}
    > DEC_DIGIT << {'A' => 'C', 'B' => 'D', 'C' => 'M'}
    > DEC_DIGIT << {'A' => 'M', 'B' => '?', 'C' => '?'}


    Very cool, Bill!
     
    Dave Burt, Mar 7, 2005
    #19
  20. Re: [Solution] [QUIZ] Roman Numerals (#22)

    On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <> wrote:
    > [Snipped Quiz Description]


    Hello Group,

    I attach my solution here, but as always the full color version can be found at:

    http://ruby.brian-schroeder.de/quiz/roman/

    The solution:
    --------------

    I chose to change the Integer class for this.

    class Integer

    First I set up two constants. The first is needed for conversion from
    integer to roman, the second for conversion from roman to integer.

    @@roman_values_assoc = %w(I IV V IX X XL L XC C CD D CM
    M).zip([1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]).reverse
    @@roman_values = @@roman_values_assoc.inject({}) { |h, (r,a)|
    h[r] = a; h }

    Then we will come to the core. I implemented integer to roman as a
    recursive process, where in each iteration the biggest possible part
    of the number is matched by a roman number, then the rest is done the
    same way. Negative roman numbers did not exist, but why not simply
    prefix a roman number with a minus sign?

    def roman
    return "-#{(-self).roman}" if self < 0
    return "" if self == 0
    @@roman_values_assoc.each do | (i, v) |
    return(i+(self-v).roman) if v <= self end
    end

    Converting romans to integer is done by reading the roman number from
    right to left, detecting if we are in the subtraction or the addition
    case and summing up the results. Nothing complicated.

    def Integer.roman(roman)
    last = roman[-1,1]
    roman.reverse.split('').inject(0) { | result, c |
    if @@roman_values[c] < @@roman_values[last]
    result -= @@roman_values[c]
    else
    last = c
    result += @@roman_values[c]
    end
    }
    end

    Thats all there is to roman numbers.

    end

    cheers,

    Brian

    --
    Brian Schröder
    http://ruby.brian-schroeder.de/
     
    Brian Schröder, Mar 7, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher Benson-Manica

    Roman numerals to ints

    Christopher Benson-Manica, Sep 12, 2003, in forum: C Programming
    Replies:
    13
    Views:
    752
    MikeyD
    Sep 17, 2003
  2. ARMAS

    Decimal to Roman Numerals

    ARMAS, Jan 24, 2007, in forum: C Programming
    Replies:
    31
    Views:
    1,768
    Dave Thompson
    Feb 6, 2007
  3. Roman Numerals

    , Aug 17, 2007, in forum: Java
    Replies:
    10
    Views:
    974
    Roedy Green
    Aug 18, 2007
  4. Replies:
    26
    Views:
    1,677
    CBFalconer
    Jan 28, 2008
  5. Ruby Quiz

    [SUMMARY] Roman Numerals (#22)

    Ruby Quiz, Mar 10, 2005, in forum: Ruby
    Replies:
    0
    Views:
    189
    Ruby Quiz
    Mar 10, 2005
Loading...

Share This Page