[Tutor] Roman to digital (pseudocode)

Adam Bark adam.jtm30 at gmail.com
Thu Mar 8 19:40:11 CET 2007


On 08/03/07, Alan Gilfoy <agilfoy at frontiernet.net> wrote:
>
> This, I heard, is more difficult than digital-to-Roman, since you have
> to "read" the subtractive cases, with a smaller numeral placed before
> a larger numeral, without simply adding all the numerals' values up
>
> I'm going to use a raw_input prompt to ask the user which Roman
> numeral he/she wants to convert. How do I "screen" for inputs that
> have characters besides "I", "V", "X", "L", "C", "D", or "M"?
>
> Second Python question:
>
> I know there's a way to "find out" the name of the first item in a list
> (ListName[0]), but is there a way to find out the first character of a
> string?
>
> Also, is there a way to "ask" Python what characters are before and
> after the character in the string that you're asking about?
>
> For example, usign the sample string "MCMXVII" (1917):
>
> How would you ask Python:
> "What's the 3rd character in this string?" (returns "M")
> "What's before that character?" (returns "C")
>
> Pseudocode time:
>
> If character Y is "M":
> and the character before character Y is "C",
> add 900 to digital_result
> and remove that "C" and that "M" from the string.
> #How would you do *that*?
>
> and the character before character Y is another "M", or if character Y
> is the first character in the string,
> add 1000 to digital_result
> and remove that "M" from the string.


you can index a string just like a list:
>>> roman_numeral = "MCMXVII"
>>> roman_numeral[2]
'M'
>>> roman_numeral[2-1]
'C'

HTH
Adam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070308/583c1dea/attachment.html 


More information about the Tutor mailing list