[Tutor] Roman to digital (pseudocode)
Bob Gailer
bgailer at alum.rpi.edu
Tue Mar 13 22:08:14 CET 2007
Deep breath .... and big step back.
The problem is not just how to code this in Python, but how to parse a
language (in this case Roman Numbers).
I have studied formal language theory but am not an expert. So here's my
take on an algorithm that would save a lot of stress.
Create a dictionary with
keys for each roman letter and each "pair" (iv, ix, xl, xc, etc)
values are the corresponding decimal values.
Starting at the left of the roman_input and repeating until the end of
the roman_input
Take the next pair of roman_input.
If it is in the dictionary
add the decimal value and step to the next letter.
Else
take the leftmost letter of that pair
If it is in the dictionary
add the decimal value and step to the next letter.
Else
complain about bad letter
--
Bob Gailer
510-978-4454
More information about the Tutor
mailing list