[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Roman Numerals (#22

James Gray

3/4/2005 1:53:00 PM

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.rub...

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.


27 Answers

Bill Guindon

3/4/2005 2:10:00 PM

0

On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <james@grayproductions.net> 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 Gray

3/4/2005 2:21:00 PM

0

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

> On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz
> <james@grayproductions.net> wrote:
>
>> VII is 8
>
> VIII is 8

Oops. Thank you.

James Edward Gray II



Brian Schröder

3/4/2005 3:14:00 PM

0

On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <james@grayproductions.net> 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.rub...
>
> 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-sch...



Nikolai Weibull

3/4/2005 3:14:00 PM

0

* 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 Gray

3/4/2005 3:33:00 PM

0

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

3/4/2005 3:43:00 PM

0

On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
<james@grayproductions.net> 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

3/4/2005 3:44:00 PM

0

On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
<james@grayproductions.net> 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-sch...



James Gray

3/4/2005 3:52:00 PM

0

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

> On Sat, 5 Mar 2005 00:33:11 +0900, James Edward Gray II
> <james@grayproductions.net> 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

3/5/2005 5:03:00 PM

0

In article
<20050304135259.ZEZS2476.lakermmtao12.cox.net@localhost.localdomain>,
Ruby Quiz <james@grayproductions.net> 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...
#!/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""}

Mark Hubbart

3/5/2005 5:39:00 PM

0

On Sat, 5 Mar 2005 00:13:56 +0900, Brian Schröder <ruby.brian@gmail.com> wrote:
> On Fri, 4 Mar 2005 22:53:20 +0900, Ruby Quiz <james@grayproductions.net> 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.rub...
> >
> > 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