mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-23 09:38:27 +00:00
Update roman_numerals.py
This commit is contained in:
parent
e6ea0ba222
commit
07721a89e2
|
@ -1,32 +1,11 @@
|
||||||
ROMAN = [
|
ROMAN = [
|
||||||
(1000000, "M_"),
|
(1000000, "M_"), (900000, "C_M_"), (500000, "D_"), (400000, "C_D_"),
|
||||||
(900000, "C_M_"),
|
(100000, "C_"), (90000, "X_C_"), (50000, "L_"), (40000, "X_L_"),
|
||||||
(500000, "D_"),
|
(10000, "X_"), (9000, "I_X_"), (5000, "V_"), (4000, "I_V_"),
|
||||||
(400000, "C_D_"),
|
(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"),
|
||||||
(100000, "C_"),
|
(100, "C"), (90, "XC"), (50, "L"), (40, "XL"),
|
||||||
(90000, "X_C_"),
|
(10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")
|
||||||
(50000, "L_"),
|
|
||||||
(40000, "X_L_"),
|
|
||||||
(10000, "X_"),
|
|
||||||
(9000, "I_X_"),
|
|
||||||
(5000, "V_"),
|
|
||||||
(4000, "I_V_"),
|
|
||||||
(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 roman_to_int(roman):
|
def roman_to_int(roman):
|
||||||
"""
|
"""
|
||||||
Convert a Roman numeral to an integer, supporting Vinculum notation
|
Convert a Roman numeral to an integer, supporting Vinculum notation
|
||||||
|
@ -43,19 +22,14 @@ def roman_to_int(roman):
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
vals = {roman: arabic for arabic, roman in ROMAN}
|
vals = {roman: arabic for arabic, roman in ROMAN}
|
||||||
# Convert the list of tuples to a dictionary
|
|
||||||
|
|
||||||
i, total = 0, 0
|
i, total = 0, 0
|
||||||
while i < len(roman):
|
while i < len(roman):
|
||||||
if i + 1 < len(roman) and roman[i : i + 2] in vals:
|
if i + 1 < len(roman) and roman[i + 1] == "_":
|
||||||
total += vals[roman[i : i + 2]]
|
total += vals[roman[i] + "_"]
|
||||||
i += 2
|
i += 2
|
||||||
else:
|
else:
|
||||||
total += vals[roman[i]]
|
total += vals[roman[i]]
|
||||||
i += 1
|
i += 1
|
||||||
return total
|
|
||||||
|
|
||||||
|
|
||||||
def int_to_roman(number):
|
def int_to_roman(number):
|
||||||
"""
|
"""
|
||||||
Convert an integer to a Roman numeral, supporting Vinculum notation
|
Convert an integer to a Roman numeral, supporting Vinculum notation
|
||||||
|
@ -68,7 +42,7 @@ def int_to_roman(number):
|
||||||
>>> all(int_to_roman(value) == key for key, value in tests.items())
|
>>> all(int_to_roman(value) == key for key, value in tests.items())
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
if not isinstance(number, int) or number < 1:
|
if not isinstance(number, int) or number <= 0:
|
||||||
raise ValueError("Input must be a positive integer greater than 0")
|
raise ValueError("Input must be a positive integer greater than 0")
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
|
@ -79,8 +53,6 @@ def int_to_roman(number):
|
||||||
break
|
break
|
||||||
return "".join(result)
|
return "".join(result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user