-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroman_converter.py
61 lines (49 loc) · 1.87 KB
/
roman_converter.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import sys
"""
A Python code to convert a Roman numeral to a number and vice versa.
"""
def main():
input_string = sys.argv[1]
if input_string.isdecimal():
print(Arabic_to_Roman(int(input_string)))
else:
print(Roman_to_Arabic(input_string))
def Roman_to_Arabic(numeral_string):
"""
Input: string containing Roman symbols
Ouput: numeric decimal value, or ValueError if the string is invalid
Sum all the values that appear in the string, from right to left.
Slice the symbols off from the end of the string,
and add the corresponding values to the result.
If the string is empty, the loop can be stopped.
The string is not a valid Roman numeral if
a) at some step the Roman symbol isn't larger than the intermediate result;
b) the loop ended and there are still characters in the string left.
"""
symbols = ('I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M')
values = (1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000)
s = numeral_string
result = 0
for symb, val in zip(symbols, values):
if result >= val:
raise ValueError(f'{numeral_string} is not a valid input')
if s == '':
break
while s.endswith(symb):
s, _, _ = s.rpartition(symb)
result += val
else:
if s != '':
raise ValueError(f'{numeral_string} is not a valid input')
return result
def Arabic_to_Roman(num):
symbols = ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
values = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
numeral_string = ''
for symb, val in zip(symbols, values):
k, num = divmod(num, val)
numeral_string += symb*k
return numeral_string
if __name__ == "__main__":
main()