From: William James Date: 2007-01-04T21:35:09+09:00 Subject: Re: Calculating roman numerals Gregory Seidman wrote: > 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 p=c > } > t+p > 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