-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman_numerals.py
More file actions
55 lines (44 loc) · 1.24 KB
/
roman_numerals.py
File metadata and controls
55 lines (44 loc) · 1.24 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
"""
roman numerals
"""
import math
symbol1 = ["I", "X", "C", "M"]
symbol5 = ["V", "L", "D"]
def get_digit(number, index):
"""
get a digit from a number
"""
return number // 10**index % 10
def roman(number):
"""
convert Hindu–Arabic numeral system to roman
this version aims to be versatile, simply by adding
the ordered simbols to the list
while substracting values from
['M','CM',D','CD','C','XC','L','XL','X','IX','V','IV', 'I']
would also be a good alternative
"""
romano = ""
# get the number of digits
lenght = int(math.log10(number))+1
for index in range(lenght):
digit = get_digit(number, index)
letter = ""
# 4 or 9
if digit % 5 == 4:
if digit % 5 == 4:
letter += symbol1[index]
if digit > 5:
letter += symbol1[index + 1]
else:
letter += symbol5[index]
else:
# adds symbol5
if digit > 4:
letter += symbol5[index]
digit = digit - 5
# and then the remain symbol1
for _ in range(digit):
letter += symbol1[index]
romano = letter + romano
return romano