Forum: Ruby Calculating roman numerals

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
32164e4d1f85ce9b5b756a0ed6c2461b?d=identicon&s=25 Shiloh Madsen (Guest)
on 2007-01-02 22:36
(Received via mailing list)
Ok, I'm having trouble with another exercise in this book. It has
asked me to write a program that will calculate the old school roman
numeral value for a given modern number (up to the thousands). To
clarify, old school roman numerals didn't do the subtraction thing, so
4 is IIII, nine is VIIII, etc. I've played around with a few options
using division, loops and modulus, but can't seem to get my brain
around the problem...any suggestions?

Shiloh
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 Carlos (Guest)
on 2007-01-02 22:44
(Received via mailing list)
[Shiloh Madsen <shiloh.madsen@gmail.com>, 2007-01-02 22.35 CET]
> Ok, I'm having trouble with another exercise in this book. It has
> asked me to write a program that will calculate the old school roman
> numeral value for a given modern number (up to the thousands). To
> clarify, old school roman numerals didn't do the subtraction thing, so
> 4 is IIII, nine is VIIII, etc. I've played around with a few options
> using division, loops and modulus, but can't seem to get my brain
> around the problem...any suggestions?

A hint: Try loops and subtractions.

Good luck.
32164e4d1f85ce9b5b756a0ed6c2461b?d=identicon&s=25 Shiloh Madsen (Guest)
on 2007-01-02 22:58
(Received via mailing list)
Thank you. That gave me an entirely new way to look at this. I am
working on one solution now, but I will try that next. If they both
work I'll post both as I am curious which will be more efficient.
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-01-02 23:30
(Received via mailing list)
Shiloh Madsen wrote:
> --
> It is better to rule in hell than serve in heaven.

class Integer
  def to_roman
    "I =1       V =5      X = 10       L = 50
    C = 100    D = 500   M = 1000".
    scan( / ([A-Z]) \s *= \s* (\d+) /x ).
    map{|letter,val|  [ letter, val.to_i ] }.
    sort_by{|a| -a.last}.
    inject( [ "", self ] ){|roman, pair|
      [ roman.first + pair.first * (roman.last / pair.last),
        roman.last % pair.last ] }.
    first
  end
end
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2007-01-03 13:46
(Received via mailing list)
On Wed, Jan 03, 2007 at 06:35:02AM +0900, Shiloh Madsen wrote:
} Ok, I'm having trouble with another exercise in this book. It has
} asked me to write a program that will calculate the old school roman
} numeral value for a given modern number (up to the thousands). To
} clarify, old school roman numerals didn't do the subtraction thing, so
} 4 is IIII, nine is VIIII, etc. I've played around with a few options
} using division, loops and modulus, but can't seem to get my brain
} around the problem...any suggestions?

We recently had a golfing competition at work on going the other
direction
(i.e. roman numeral to arabic). Our best solution:

def x s
  p=t=0
  s.scan(/./){|r|
    i=' IVXLCDM'.index r
      c=10**(i/2)/(2-i%2)
      t+= p<c ?-p:p
      p=c
  }
  t+p
end

} Shiloh
--Greg
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-01-04 13:36
(Received via mailing list)
Gregory Seidman wrote:
> (i.e. roman numeral to arabic). Our best solution:
> end
>
> } Shiloh
> --Greg

Another way:

require 'enumerator'
def y s
  t=0
  (s.split("").map{|c|
    n=' IVXLCDM'.index c;10**(n/2)/(2-n%2)}<<0).
  each_cons(2){|a,b| t+= a<b ?-a:a}
  t
end
63529e6fbb35dde336d471b569b84d87?d=identicon&s=25 Rodrigo Bermejo (rbermejo)
on 2007-01-05 00:54
There was a related ruby quiz:

 http://rubyquiz.com/quiz22.html

-rb.
302da8d9b2db88c7f08d35c4a59b2a01?d=identicon&s=25 Jason Bornhoft (jmvbxx)
on 2007-01-05 03:58
Shiloh Madsen wrote:
> Ok, I'm having trouble with another exercise in this book. It has


Which book?

JB
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jan Svitok (Guest)
on 2007-01-06 19:30
(Received via mailing list)
On 1/2/07, Shiloh Madsen <shiloh.madsen@gmail.com> wrote:
> Ok, I'm having trouble with another exercise in this book. It has
> asked me to write a program that will calculate the old school roman
> numeral value for a given modern number (up to the thousands). To
> clarify, old school roman numerals didn't do the subtraction thing, so
> 4 is IIII, nine is VIIII, etc. I've played around with a few options
> using division, loops and modulus, but can't seem to get my brain
> around the problem...any suggestions?
>
> Shiloh

Look for older thread "Need help with a program".
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2007-01-06 19:31
(Received via mailing list)
On 1/2/07, Shiloh Madsen <shiloh.madsen@gmail.com> wrote:
> Ok, I'm having trouble with another exercise in this book. It has
> asked me to write a program that will calculate the old school roman
> numeral value for a given modern number (up to the thousands). To
> clarify, old school roman numerals didn't do the subtraction thing, so
> 4 is IIII, nine is VIIII, etc. I've played around with a few options
> using division, loops and modulus, but can't seem to get my brain
> around the problem...any suggestions?
>

Lest we all forget about the code snippets on RubyForge ...

http://rubyforge.org/snippet/detail.php?type=snipp...

Should do all you need (and more)

Blessings,
TwP
47e2d5513caa1db234f3481d2d528838?d=identicon&s=25 Gerald Ebberink (Guest)
on 2007-01-08 08:25
(Received via mailing list)
On Wed, 03 Jan 2007 06:35:02 +0900, Shiloh Madsen wrote:

> Ok, I'm having trouble with another exercise in this book. It has
> asked me to write a program that will calculate the old school roman
> numeral value for a given modern number (up to the thousands). To
> clarify, old school roman numerals didn't do the subtraction thing, so
> 4 is IIII, nine is VIIII, etc. I've played around with a few options
> using division, loops and modulus, but can't seem to get my brain
> around the problem...any suggestions?
>
> Shiloh

Many people have answered, but since I'm a beginner I would like to have
some comments on the following. Which I think is pretty ruby-esque

ans=0
roman="XVIII"
{"X"=>10,"V"=>5,"I"=>1}.each_pair do |letter,
number|ans+=roman.count(letter)*number end;ans


Kind regards,
Gerald
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-01-08 08:45
(Received via mailing list)
Gerald Ebberink wrote:
> > Shiloh
> Gerald
The original poster asked for a way to translate from modern numerals
to roman numerals.  You're doing the opposite.

Your method will convert old-school roman numerals, but it
won't work for "XIV".
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-01-19 16:30
(Received via mailing list)
On 1/4/07, Tim Pease <tim.pease@gmail.com> wrote:

> Lest we all forget about the code snippets on RubyForge ...

The code snippets thing is super cool, if only it was better
organized.  Not sure if we can do much with it, since it's mostly up
to GForge...

I just wish it was easier to navigate, search, etc.
This topic is locked and can not be replied to.