Decimal to Roman Numeral Conversion¶
Roman Numerals¶
Roman numerals are a numeral system that originated in ancient Rome and were used throughout the Roman Empire. They consist of a combination of letters from the Latin alphabet, which are used to represent different values. The Roman numeral system has no symbol for zero, and it is not a positional system like the decimal system we use today.
There are seven basic symbols used in the Roman numeral system:
I: represents the value 1
V: represents the value 5
X: represents the value 10
L: represents the value 50
C: represents the value 100
D: represents the value 500
M: represents the value 1000
These symbols can be combined in various ways to represent larger values. For example:
II: represents the value 2
III: represents the value 3
IV: represents the value 4
VI: represents the value 6
IX: represents the value 9
XI: represents the value 11
XIV: represents the value 14
XX: represents the value 20
XL: represents the value 40
LXX: represents the value 70
XC: represents the value 90
CXX: represents the value 120
CD: represents the value 400
DCC: represents the value 700
CM: represents the value 900
Above example shows that some numerals are written using “subtractive notation”, e.g. for 4 (IV) and 9 (IX), where the first symbol (I) is subtracted from the larger one (V, or X), thus avoiding the clumsier IIII and VIIII. Subtractive notation is also used for 40 (XL), 90 (XC), 400 (CD) and 900 (CM). These are the only subtractive forms in standard use.
Roman numerals are often used today in situations where a more traditional or formal style is desired, such as in the numbering of book chapters or movie sequels. They are also used in clock faces, and in the names of monarchs and popes.
For more details see Roman Numerals at Wikipedia.
Task¶
Create a decimal_to_roman function which given an integer number returns its representation as roman numeral.
Implementation Notes¶
Test Cases
1import unittest
2from parameterized import parameterized
3
4class TestConvertFunction(unittest.TestCase):
5
6 @parameterized.expand([
7 ("1", 1, "I"),
8 ("2", 2, "II"),
9 ("3", 3, "III"),
10 ("4", 4, "IV"),
11 ("5", 5, "V"),
12 ("6", 6, "VI"),
13 ("8", 8, "VIII"),
14 ("9", 9, "IX"),
15 ("10", 10, "X"),
16 ("11", 11, "XI"),
17 ("19", 19, "XIX"),
18 ("20", 20, "XX"),
19 ("39", 39, "XXXIX"),
20 ("40", 40, "XL"),
21 ("41", 41, "XLI"),
22 ("49", 49, "XLIX"),
23 ("50", 50, "L"),
24 ("90", 90, "XC"),
25 ("95", 95, "XCV"),
26 ("100", 100, "C"),
27 ("400", 400, "CD"),
28 ("500", 500, "D"),
29 ("900", 900, "CM"),
30 ("1000", 1000, "M"),
31 ("Spanish flu", 1918, "MCMXVIII"),
32 ("2023", 2023, 'MMXXIII')
33 ])
34 def test_convert(self, name, num, expected_result):
35 result = decimal_to_roman(num)
36 self.assertEqual(result, expected_result)
Solution 1
1def decimal_to_roman(num):
2 roman = ''
3 number_map = (
4 (1000, "M"),
5 (900, "CM"),
6 (500, "D"),
7 (400, "CD"),
8 (100, "C"),
9 (90, "XC"),
10 (50, "L"),
11 (40, "XL"),
12 (10, 'X'),
13 (9, 'IX'),
14 (5, 'V'),
15 (4, 'IV'),
16 (1, 'I'),
17 )
18 for arabic_num, roman_num in number_map:
19 while num >= arabic_num:
20 roman += roman_num
21 num -= arabic_num
22 return roman