Can this Roman Number to Integer converter code be shorter?
Asked Answered
T

4

6

95 bytes currently in python

I,V,X,L,C,D,M,R,r=1,5,10,50,100,500,1000,vars(),lambda x:reduce(lambda T,x:T+R[x]-T%R[x]*2,x,0)

Here is the few test results, it should work for 1 to 3999 (assume input is valid char only)

>>> r("I")
1
>>> r("MCXI")
1111
>>> r("MMCCXXII")
2222
>>> r("MMMCCCXXXIII")
3333
>>> r("MMMDCCCLXXXVIII")
3888
>>> r("MMMCMXCIX")
3999

And this is not duplicate with this, this is reversed one.

So, is it possible to make that shorter in Python, or Other languages like ruby could be done shorter than that?

Tankersley answered 3/12, 2009 at 10:35 Comment(4)
You can leverage the reversed solution like this. for x in range(1,4000):if intToRoman(x)==input:break ;)Boyla
Vote to close on the fact that you're trying to get a better code-golf solution i bet ;) not really...Ranice
Imm, I like to see various implementations and in various languages, thats all. Its my own implemenation. I believe there is many many geeks out there who can do a lot better then that. Thats why I post this.Tankersley
I'm voting to close this question as off-topic because this is not code golf.Periphrastic
B
7

Shortest solutions from codegolf.com

There was a "Roman to decimal" competition over at Code Golf some time ago. (Well, actually it's still running because they never end.) A Perl golfer by the name of eyepopslikeamosquito decided to win all four languages (Perl, PHP, Python, and Ruby), and so he did. He wrote a fascinating four-part series "The golf course looks great, my swing feels good, I like my chances" (part II, part III, part IV) describing his approaches over at Perl Monks.

Here are his solutions:

Ruby, 53 strokes

n=1;$.+=n/2-n%n=10**(494254%C/9)%4999while C=getc;p$.

Perl, 58 strokes

$\+=$z-2*$z%($z=10**(19&654115/ord)%1645)for<>=~/./g;print

He also has a 53-stroke solution, but it probably doesn't work right now: (it uses the $^T variable during a few second period in 2011!)

$\+=$z-2*$z%($z=10**(7&$^T/ord)%1999)for<>=~/./g;print

PHP, 70 strokes

<?while(A<$c=fgetc(STDIN))$t+=$n-2*$n%$n=md5(o²Ûö¬Ñ.$c)%1858+1?><?=$t;

The six weird characters in the md5(..) are chr(111).chr(178).chr(219).chr(246).chr(172).chr(209) in Perl notation.

Python, 78 strokes

t=p=0
for r in raw_input():n=10**(205558%ord(r)%7)%9995;t+=n-2*p%n;p=n
print t
Bourque answered 3/12, 2009 at 22:55 Comment(2)
Oh thanks, I couldn't find that before, So I better accept this.Tankersley
Yeah, unfortunately because the codegolf.com competition never ends, you can't see what other people are doing. It just so happens that eyepopslikeamosquito revealed his solutions elsewhere ...Bourque
B
3

Python - 94 chars

cheap shot :)

I,V,X,L,C,D=1,5,10,50,100,500
M,R,r=D+D,vars(),lambda x:reduce(lambda T,x:T+R[x]-T%R[x]*2,x,0)
Boyla answered 3/12, 2009 at 11:37 Comment(0)
B
1

Actually defining my own fromJust is smaller, a total of 98

r=foldl(\t c->t+y c-t`mod`y c*2)0 --34
y x=f$lookup x$zip"IVXLCDM"[1,5,10,50,100,500,1000] --52
f(Just x)=x --12
  -- assumes correct input

Haskell gets close.

import Data.Maybe --18
r=foldl(\t c->t+y c-t`mod`y c*2)0 --34
y x=fromJust$lookup x$zip"IVXLCDM"[1,5,10,50,100,500,1000] --59

total bytes = 111

Would be 93 if i didn't need the import for fromJust

Byerly answered 3/12, 2009 at 11:36 Comment(0)
F
0

Adopting a response from Jon Skeet to a previously asked similar question:

In my custom programming language "CPL1839079", it's 3 bytes:

r=f
Foresaid answered 3/12, 2009 at 10:49 Comment(3)
yeah, if there is built-in functions, its always shorter. btw, minus are not meTankersley
Your custom programming language has to be Turing-complete, or it doesn't count. Fork over the code, and I'll un-downvote you :)Numbskull
Joey: Actually, CPL1839079 is Python with gnibbler's program (replacing r with f) included in the standard libs. Not much of a deal, isn't it?Foresaid

© 2022 - 2024 — McMap. All rights reserved.