Velocity Reviews > Ruby > [QUIZ] Roman Numerals (#22)

# [QUIZ] Roman Numerals (#22)

Ruby Quiz
Guest
Posts: n/a

 03-04-2005
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.

Bill Guindon
Guest
Posts: n/a

 03-04-2005
On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <(E-Mail Removed)> wrote:

> VII is 8

VIII is 8

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

--
Bill Guindon (aka aGorilla)

James Edward Gray II
Guest
Posts: n/a

 03-04-2005
On Mar 4, 2005, at 8:10 AM, Bill Guindon wrote:

> On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz
> <(E-Mail Removed)> wrote:
>
>> VII is 8

>
> VIII is 8

Oops. Thank you.

James Edward Gray II

Brian Schröder
Guest
Posts: n/a

 03-04-2005
On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <(E-Mail Removed)> 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/

Nikolai Weibull
Guest
Posts: n/a

 03-04-2005
* 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);}

James Edward Gray II
Guest
Posts: n/a

 03-04-2005
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

Bill Guindon
Guest
Posts: n/a

 03-04-2005
On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
<(E-Mail Removed)> 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)

Brian Schröder
Guest
Posts: n/a

 03-04-2005
On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
<(E-Mail Removed)> 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/

James Edward Gray II
Guest
Posts: n/a

 03-04-2005
On Mar 4, 2005, at 9:42 AM, Bill Guindon wrote:

> On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
> <(E-Mail Removed)> 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

Karl von Laudermann
Guest
Posts: n/a

 03-05-2005
In article
<20050304135259.ZEZS2476.lakermmtao12.cox.net@loca lhost.localdomain>,
Ruby Quiz <(E-Mail Removed)> 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""}