Saturday, February 24, 2007

Roman Numeral Converter

One more codegolf challenge:-

In this challenge, you will be given a number in roman numeral form and must print out its integer value.

( from codegolf)


My latest solution at 129 bytes:-

r,l,s={'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1},1000,0
for j in raw_input():s+=r[j]-(0,l*2)[r[j]>l];l=r[j]
print s

Any other ideas ?

Update:

After manzo's comment, I searched a little and came up with this. Right now its at 119 bytes

r,l,s=dict(M=1000,D=500,C=100,L=50,X=10,V=5,I=1),1000,0
for j in raw_input():s+=r[j]-(0,l*2)[r[j]>l];l=r[j]
print s

1 comment:

manzo said...

instead of

s={'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1}

this is shorter:

s=dict(zip('IVXLCDM',(1,5,10,50,100,500,1000)))

;)