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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
YOU
  • 120,166
  • 34
  • 186
  • 219
  • You can leverage the reversed solution like this. `for x in range(1,4000):if intToRoman(x)==input:break` ;) – John La Rooy Dec 03 '09 at 11:44
  • Vote to close on the fact that you're trying to get a better code-golf solution i bet ;) not really... – RCIX Dec 03 '09 at 11:51
  • 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. – YOU Dec 03 '09 at 11:59
  • I'm voting to close this question as off-topic because this is not code golf. – n. m. could be an AI Oct 27 '18 at 09:53

4 Answers4

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
A. Rex
  • 31,633
  • 21
  • 89
  • 96
  • Oh thanks, I couldn't find that before, So I better accept this. – YOU Dec 04 '09 at 00:55
  • 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 ... – A. Rex Dec 04 '09 at 01:02
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)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
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

barkmadley
  • 5,207
  • 1
  • 28
  • 31
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
Community
  • 1
  • 1
Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • yeah, if there is built-in functions, its always shorter. btw, minus are not me – YOU Dec 03 '09 at 11:48
  • Your custom programming language has to be Turing-complete, or it doesn't count. Fork over the code, and I'll un-downvote you :) – Joey Adams Dec 03 '09 at 23:05
  • 2
    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? – Erich Kitzmueller Dec 04 '09 at 03:14