### Thread: convert numbers from roman to arabic

1. #### convert numbers from roman to arabic

Hi all,

the first goal is to write a programm in Python that conerts numbers from roman to arabic.
The second goal is that the program should be as short as possible.
I've written a program with a size of 206 byte.
Is it possible to write a shorter one?
2. I wouldn't know of the top of my head, if you attach the program, maybe it can be optamised. Could you also give a brief explination of how the progam works.

Have fun,
Mark.
3. OK ... here it is:

Code:
```import sys;d={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
l=[];r=0;l=map(lambda x:d[x],list(sys.argv[1]))
for n in range(len(l)-1):
if l[n]<l[n+1]:
l[n]=-l[n]
for m in l:
r+=m
print r```
4. #### inline

You'll be hard pressed, that's a very compact pieces of code.

Code:
```import sys;d={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
l=[];r=0;l=map(lambda x:d[x],list(sys.argv[1]))
for n in range(len(l)-1):if l[n]<l[n+1]:l[n]=-l[n]
for m in l:r+=m
print r```
try that, all ive really done is make some of the staments inline rather than using indented blocks. dont think ur gonna get much better. I havnt tested it so it doesnt work let me know. Why was the size so important anyway?

Hope this helps,
Mark.
5. That doesn't work :-(
Code:
```    for n in range(len(l)-1):if l[n]<l[n+1]:l[n]=-l[n]
^
SyntaxError: invalid syntax```
The size was important because it was a compitition on a Linux convention.
I was not there but a friend told me.
I just wanted to see if it is possible to write a shorter programm.
By the way every programming language was allowed.
The winner wrote a progamm that had less then 100 byte (with Haskell). With C++ I wrote a programm with 164 byte.
6. #### This should work.

Ok, sorry, obviously you can't do more than one inline block that way. Oops, im sure there is a way of doing it but anyway, they this

Code:
```import sys;d="I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
l=[];r=0;l=map(lambda x:d[x],list(sys.argv[1]))
for n in range(len(l)-1):
if l[n]<l[n+1]: l[n]=-l[n]
for m in l: r+=m
print r```
If your going for size may i sugest perl, I've seen entire pages of code crushed down into a couple of lines. I don't see you getting this program much smaller. however if you were being clever you could write a module and call that (assuming they dont count modules as part of the programs).

This one should work, as before, let me know if it doesn't.

Hope this helps,
Mark.
Last edited by netytan; July 15th, 2003 at 05:27 PM.
7. OK. It works ... the size is excactly 200 byte.
A friend of mine programmed it in perl.
His program has a size of 104 byte.
I thought you could code it in a smaller size in python but seeming impossible :-(
8. The problem is that python relies on white space to denote blocks. i'm sure there is a way to write inline code but i couldnt tell you how off the top of my head simply because most people us python because of its easy to read style and maintainablity, by writing inline code you make the code less readable. On a size front perl will win, but then as i said i've seen a whole page smashed down into a few lines! it was nearly unreadable but for size.. you can obviously use modules in your code, it maybe be a bit of a cheat but write a module, plug it into your python version, and then call that, could get it under 20 bytes i think if you did it that way. if its against the rules for you to write the module, then ill pack it for u, so u "didnt write" the module lol. just an idea

Anyway have fun,
Mark.