-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_roman.py
executable file
·55 lines (47 loc) · 1.27 KB
/
to_roman.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
#! /usr/bin/env python3
"""A very simple function for converting an integer to roman numerals.
"""
import sys
int_to_roman = (
(1000, 'M'),
(900, 'CM'),
(500, 'D'),
(400, 'CD'),
(100, 'C'),
(90, 'XC'),
(50, 'L'),
(40, 'XL'),
(10, 'X'),
(9, 'IX'),
(5, 'V'),
(4, 'IV'),
(1, 'I'),
)
def to_roman(number):
"""Given an integer, return a roman number.
Somewhat arbitrarily, we assume that Romans never counted above 3000,
so that's the highest number we support.
"""
if not isinstance(number, int):
raise ValueError('{!r} is not an integer'.format(number))
if number < 1:
raise ValueError('Roman numbers do not allow zero or less, so not {}'.format(number))
if number > 3000:
raise ValueError('We do not support {}, it is more than 3000'.format(number))
result = ''
for integer, part in int_to_roman:
while number >= integer:
result += part
number -= integer
return result
if __name__ == '__main__':
args = sys.argv[1:]
if len(args) != 1:
print(__doc__)
sys.exit(1)
try:
number = int(args[0], 10)
except ValueError as e:
print(e)
sys.exit(1)
print(to_roman(number))