-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest5-Decimal_number_to_Roman.py
More file actions
98 lines (90 loc) · 1.96 KB
/
test5-Decimal_number_to_Roman.py
File metadata and controls
98 lines (90 loc) · 1.96 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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import re
base = {
1: 'I',
5: 'V',
10: 'X',
50: 'L',
100: 'C',
500: 'D',
1000: 'M',
4: 'IV',
9: 'IX',
40: 'XL',
90: 'XC',
400: 'CD',
900: 'CM'
}
while True:
year = input()
try:
if year and (0 < int(year) < 4000):
break
else:
print('Range input error, repeat please')
except ValueError:
print('Range input error, repeat please')
continue
except AttributeError:
print('Range input error, repeat please')
continue
p = re.compile('.')
field = p.findall(year.zfill(4), 0)
print(field)
result = ''
for c in range(4):
s = 0
# thousands
i = field[c]
if i != '0' and c == 0:
for j in range(int(i)):
result += base[1000]
continue
# hundreds
if i != '0' and c == 1:
if i == '4':
result += base[400]
continue
elif i == '9':
result += base[900]
continue
if 5 <= int(i) <= 8:
s = 5
result += base[500]
if i == '5':
continue
for j in range(s, int(i)):
result += base[100]
continue
# decimals
if i != '0' and c == 2:
if i == '4':
result += base[40]
continue
elif i == '9':
result += base[90]
continue
if 5 <= int(i) <= 8:
s = 5
result += base[50]
if i == '5':
continue
for j in range(s, int(i)):
result += base[10]
continue
# units
if i != '0' and c == 3:
if i == '4':
result += base[4]
continue
elif i == '9':
result += base[9]
continue
if 5 <= int(i) <= 8:
s = 5
result += base[5]
if i == '5':
continue
for j in range(s, int(i)):
result += base[1]
continue
print(result)