[Tutor] Roman to digital (pseudocode)

Kent Johnson kent37 at tds.net
Wed Mar 14 00:53:35 CET 2007


Bob Gailer wrote:
> 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

Or make a list of all the pairs and values, followed by all the single 
letters and values, and just find the first match from the list against 
the start of the test string.

Neither of these is really a parser, though; they allow badly formed 
numbers like IICCXM.

Kent


More information about the Tutor mailing list