forked from davehunt00/UW_python_class_demo.code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman.py
More file actions
33 lines (28 loc) · 1.18 KB
/
Copy pathroman.py
File metadata and controls
33 lines (28 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Roman numeral to integer converter, based on Janos Juhasz in
http://mail.python.org/pipermail/tutor/2007-5March.txt
Turn Juhasz' script into a function, streamline, revise names and comments
"""
# lookup table from single Roman numeral to integer
numerals = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
def roman_to_int(roman):
"""
Convert string of roman numerals to an integer
Numerals may be upper or lowercase, other chars raise KeyError
"""
# convert string of roman numerals to list of integers
digits = [numerals[ch] for ch in roman.upper()]
result = 0
while digits:
digit = digits.pop(0) # take leftmost digit
# if a larger digit remains, subtract this one from result
if digits and digit < max(digits): digit = -digit
result += digit
return result
# just to demo comprehension, define this function at module level
def digits(roman): return [numerals[ch] for ch in roman.upper()]
if __name__ == '__main__':
while True:
roman = raw_input('Enter a roman number: ')
if not roman: break # loop until empty input
print roman, '=', roman_to_int(roman)