From: trans Date: 2009-09-21T00:12:49+09:00 Subject: Re: Roman numeral conversion On Sep 18, 1:26 pm, Brian Tickler wrote: > First time poster here... > > I realize that rubyquiz.com is no longer really active, but some of the > code there is still used by people googling for example code (roman > numeral conversion in my case for a class I am taking), and I wanted to > point out that many of the solutions listed for quiz #22 are evaluating > roman to arabic numerals incorrectly. > > If you run many of the solutions, especially the ones that use this > table: > >         [ 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'  ] >         ] > > ...and then you try to evaluate the string value XD (for example), it > will convert this string to 510.  This should not happen; XD is not a > legal roman numeral, DX would be 510, and CDXC would be 490.  The > problem seems to be that in roman to arabic numeral conversion, the > solutions just add the raw values together.  This means that converters > using this kind of conversion will accept any combination of roman > numeral values from the table above...regardless of order. > > A crazier example: > > If you evaluate XIV, you get 14, by finding X and then IV in the table > above and then adding their corresponding values together. > > If you put in IXIVI, it will *also* evaluate to 14 by adding IX + IV + I > and accept it as a valid roman numeral. > > I don't have a solution yet, but I thought I would at least point this > out in case someone is adding a to_roman method to Integer or something > and wants it to be 100% robust. english/roman.rb checks out. T.