[Tutor] Digital-to- Roman (pseudocode)

Bob Gailer bgailer at alum.rpi.edu
Thu Mar 8 18:10:42 CET 2007


Alan Gilfoy wrote:
> Quoting Bob Gailer <bgailer at alum.rpi.edu>:
>
>   
>> Make sure each step
>> is one simple operation. Walk thru the steps to verify that you have
>> them correct.
>>     
>
> Bob, your email did inspire me on how I would express the process in  
> computer-processing terms.
>
> Here's how I'd break down the steps, using "pseudocode".
> I might also be askign how to convert each bit of pseudocode into actual code.
>
> (For both, I'm going to program in "exceptions" for numbers less than  
> 0 or greater than 3999, which is MMMCMXCIX in Roman numerals. That is  
> the highest number that can be expressed in Roman numerals using  
> strings of no morer than 3 of the same letter (standard rule for Roman  
> numerals), and using only the symbols I, V, X, L, C, D and M.)
> I know there are ways to express a Roman numeral for 5,000 and higher,  
> but I'm goign to pgram those in later.
>
> Digital to Roman pseudocode:
>
> 1. if digital_input is greater than 1000:
> subtract 1000 from it and add "M" to string roman_result
> # How do you do that, add one character to the end of an existing string?
>   
Start with an empty string:

roman_result = ""

To add a character at the end:

roman_result += "M" # Python shorthand for roman_result = roman_result + "M"


> # also, how do I modify the digital_input variable (it's an integer)  
>   
digital_input -= 1000
> several times through the conversion process?
>   
You will be processing the input in a loop (while or for).
> if digital_input is less than 1000:
> is it greater than 900? If so, subtract 900 from digital_input and add  
> "CM" to string roman_reuslt
>
> is it less than 900?
> If it's less than 900, but greater than 500, subtract 500, and add "D"  
> to the string.
>
> If it's less than 900, and less than 500, is it greater than 400?
> If so, subtract 400 from input and add "CD" to the string.
> If it isn't greater than 400, but greater than 100, subtract 100 from  
> the input and add "C" to the result string.
>
> is it less than 100?
> If it is, but it's greater than 90, subtract 90 and add "XC" to the string.
> if it is less than 90, but greater than 50, subtract 50, and add "L"  
> to the string.
>
> (and so on, down from 50 to 1)
>   
That looks good.

As you gain familiarity with Python you will develop ways to separate 
data from logic. I might say more about this later, but right now I'm 
about to drive north a bit.

-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list