Roman numerals

Translate Roman numbers into Arabic number (https://en.wikipedia.org/wiki/Roman_numerals)

IN

CCLXXXIII

OUT

283

IN

MMMCMXCIX

OUT

3999

IN

I

OUT

1

IN

III

OUT

3

IN

VIII

OUT

8

IN

XL

OUT

40

IN

LX

OUT

60

IN

LXII

OUT

62

IN

CC

OUT

200

IN

CD

OUT

400

IN

MCMLXXXVIII

OUT

1988
Login to submit solution


196 symbols python2 failed test 10 Wrong answer
600
instead of
400
Sat Aug 30 02:20:01 2014 PFischbeck
s=raw_input()
a={'M':1000,'D':500,'CM':900,'CD':400,'C':100,'L':50,'XC':90,'XL':40,'X':10,'V':5,'IX':9,'IV':4,'I':1}
i=r=0
while i<len(s):
 for k in a:
	if s.find(k,i)==i:r+=a[k];i+=len(k)
print r
90 symbols perl failed test 2 Wrong answer
4221
instead of
3999
Thu Sep 4 06:03:42 2014 gabrielmad
#!/usr/bin/perl -paF
$x+=($d={qw/M 1000 D 500 C 100 L 50 X 10 V 5 I 1/}->{$_})-2*$l*($l<($l=$d))for@F;$_=$x
142 symbols ruby2.0 failed test 1 Code error

./code:3:in `<main>': undefined local variable or method `str' for main:Object (NameError)

Sat Oct 4 17:09:21 2014 fl00r
R='IVXLCDM'
r=->(a){b=R.index(a);5**(b%2)*10**(b/2)}
str.scan(/((.)\2*)/).map{|a,b|r.call(b)*a.size}.reverse.inject(0){|s,c|c>s ? s+=c : s-=c}
148 symbols ruby2.0 failed test 1 Wrong answer

                      
instead of
283
Sat Oct 4 17:10:19 2014 fl00r
R='IVXLCDM'
r=->(a){b=R.index(a);5**(b%2)*10**(b/2)}
ARGF.read.scan(/((.)\2*)/).map{|a,b|r.call(b)*a.size}.reverse.inject(0){|s,c|c>s ? s+=c : s-=c}
105 symbols ruby2.0 failed test 1 Wrong answer
61
instead of
283
Sat Oct 4 17:36:10 2014 fl00r
p ARGF.read.scan(/\S/).map{|a|c='IVXLCDM'.index(a);5**(c%2)*10**(c/2)}.reverse.inject{|s,c|s+=c<s&&-c||c}
90 symbols ruby2.0 failed test 1 Code error

./code:2: warning: ARGF#chars is deprecated; use #each_char instead ./code:2:in `block in <main>': undefined method `%' for nil:NilClass (NoMethodError) from ./code:2:in `each_char' from ./code:2:in `chars' from ./code:2:in `<main>'

Sat Oct 4 21:41:14 2014 fl00r
x=l=0
ARGF.chars{|a|c=5**(c%2)*10**(c/2);x+=c;c>l&&x-=l*2;l=c if c='IVXLCDM'.index(a)}
p x
93 symbols ruby2.0 failed test 1 Wrong answer
283
CCLXXXIII
instead of
283
Sun Oct 5 04:16:49 2014 fl00r
#!/usr/bin/ruby1.9.1 -p
x=l=0
$_.chars{|a|c='IVXLCDM'.index(a)||next;c=5**(c%2)*10**(c/2);x+=c;c>l&&x-=l*2;l=c}
p x
85 symbols perl failed test 1 Code error

Scalar found where operator expected at ./code line 2, near "$<" (Missing semicolon on previous line?) Can't modify constant item in scalar assignment at ./code line 2, near "$<" syntax error at ./code line 2, near "$<" syntax error at ./code line 2, near "c}" Execution of ./code aborted due to compilation errors.

Mon Oct 6 14:44:41 2014 fl00r
x=l=0
$<.chars{|a|c='IVXLCDM'.index(a);c=5**(c%2)*10**(c/2);x+=c;c>l&&x-=l*2;l=c}
p x
172 symbols php failed test 1 Wrong answer
$a=0;$r=array('I'=>1,'V'=>5,'X'=>10,'L'=>50,'C'=>100,'D'=>500,'M'=>1000);foreach(str_split($t)as$i){$a=!empty($p)&&$r[$p]<$r[$i]?$a-$r[$p]+($r[$i]-$r[$p]):$a+$r[$i];$p=$i;}
instead of
283
Sat Sep 12 12:18:55 2015 fourtwentyseven
$a=0;$r=array('I'=>1,'V'=>5,'X'=>10,'L'=>50,'C'=>100,'D'=>500,'M'=>1000);foreach(str_split($t)as$i){$a=!empty($p)&&$r[$p]<$r[$i]?$a-$r[$p]+($r[$i]-$r[$p]):$a+$r[$i];$p=$i;}
87 symbols python3 failed test 1 Code error

Traceback (most recent call last): File "./code", line 2, in <module> for c in input()[::-1]:i='IVXLCDM'.find(c);r+=i%2*5*10**i//2*((r/i<5)*2-1) ZeroDivisionError: division by zero

Fri Oct 9 16:48:34 2015 BogdanShevchenko
r=0
for c in input()[::-1]:i='IVXLCDM'.find(c);r+=i%2*5*10**i//2*((r/i<5)*2-1)
print(r)
87 symbols python3 failed test 1 Wrong answer
2500
instead of
283
Fri Oct 9 16:50:45 2015 BogdanShevchenko
r=0
for c in input()[::-1]:i='IVXLCDM'.find(c);r+=i%2*5*10**i//2*((r<5*i)*2-1)
print(r)
91 symbols python3 failed test 1 Wrong answer
-2450
instead of
283
Fri Oct 9 17:03:55 2015 BogdanShevchenko
r=0
for c in input()[::-1]:i='IVXLCDM'.find(c);r+=(i%2*4+1)*10**i//2*((r<5*i)*2-1)
print(r)
92 symbols python3 failed test 1 Wrong answer
-1125
instead of
283
Fri Oct 9 17:07:20 2015 BogdanShevchenko
r=0
for c in input()[::-1]:i='IVXLCDM'.find(c);r+=2**i//2*5**(i+1)//2*((r<5*i)*2-1)
print(r)
89 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 2, in <module> for i in map("IVXLCDM".find,raw_input()):n=5**(-~i/2)<<i/2;x+=n-p*2*(i>p);p=i NameError: name 'p' is not defined

Mon Sep 12 17:25:21 2016 Sait2000
x=0
for i in map("IVXLCDM".find,raw_input()):n=5**(-~i/2)<<i/2;x+=n-p*2*(i>p);p=i
print x

View all solutions