I am new to python and programming, so not sure if I should weigh in here. Nonetheless . . .
Creating a dictionary seems fair, but I wonder about using pairs rather than single letters. Roman numerals are constructed from strict rules other than the allowable letter set -- here is the relevant wikipedia bit: Rules regarding Roman numerals often state that a symbol representing 10x may not precede any symbol larger than 10x+1. For example, C cannot be preceded by I or V, only by X (or, of course, by a symbol representing a value equal to or larger than C). Thus, one should represent the number "ninety-nine" as XCIX, not as the "shortcut" IC. However, these rules are not universally followed. Although the shortcut may be easier for people, the universal notation may be easier to define programmatically. I am wondering what would be the best way to create the rules - A dictionary will help you look up values, but not rules. It does not retain its order and order is essential. Instead, create a tuple of the roman numerals in ascending order (roman). Create a paired tuple with the base 10 value (baseten). Now get an element from the string you want to test -- use the index value in roman to test the rules -- you will know what is higher, lower, and the same Then look for things like the following: - does the following element have a lower index in roman? if yes, you know the value (match indexes from the tuples) -- step forward - what if the follower is the same? then check the one after, if it is the same (and the following digit is lower) then you have a value. -- step over the last matching digit - if the following value is higher, it must be by only one unit, if so you have a value, but also a new rule: the following digit must be lower than the last digit of the pair. and so on. not pseudocode I know, and I am certain there are better ways to do this, but maybe something here helps. -c On 3/13/07, Bob Gailer <[EMAIL PROTECTED]> 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 > > -- > Bob Gailer > 510-978-4454 > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- - - - - - - - Clay S. Wiedemann voice: 718.362.0375 aim: khlav wii: 3905 4571 6175 2469 twitter: seastokes _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor