mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-23 01:28:26 +00:00
Update roman_numerals.py
This commit is contained in:
parent
1735aed474
commit
6bff12142e
|
@ -25,8 +25,6 @@ ROMAN = [
|
||||||
(4, "IV"),
|
(4, "IV"),
|
||||||
(1, "I"),
|
(1, "I"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def roman_to_int(roman: str) -> int:
|
def roman_to_int(roman: str) -> int:
|
||||||
"""
|
"""
|
||||||
Convert a Roman numeral to an integer, supporting Vinculum notation
|
Convert a Roman numeral to an integer, supporting Vinculum notation
|
||||||
|
@ -42,34 +40,18 @@ def roman_to_int(roman: str) -> int:
|
||||||
>>> all(roman_to_int(key) == value for key, value in tests.items())
|
>>> all(roman_to_int(key) == value for key, value in tests.items())
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
vals = {
|
vals = dict(ROMAN) # Convert the list of tuples to a dictionary
|
||||||
"I": 1,
|
|
||||||
"V": 5,
|
|
||||||
"X": 10,
|
|
||||||
"L": 50,
|
|
||||||
"C": 100,
|
|
||||||
"D": 500,
|
|
||||||
"M": 1000,
|
|
||||||
"I_": 1000,
|
|
||||||
"V_": 5000,
|
|
||||||
"X_": 10000,
|
|
||||||
"L_": 50000,
|
|
||||||
"C_": 100000,
|
|
||||||
"D_": 500000,
|
|
||||||
"M_": 1000000,
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
# Check for 2-character symbols first (like I_ or X_)
|
||||||
total += vals[roman[i : i + 2]]
|
if i + 1 < len(roman) and roman[i:i+2] in vals:
|
||||||
|
total += vals[roman[i:i+2]]
|
||||||
i += 2
|
i += 2
|
||||||
else:
|
else:
|
||||||
total += vals[roman[i]]
|
total += vals[roman[i]]
|
||||||
i += 1
|
i += 1
|
||||||
return total
|
return total
|
||||||
|
|
||||||
|
|
||||||
def int_to_roman(number: int) -> str:
|
def int_to_roman(number: int) -> str:
|
||||||
"""
|
"""
|
||||||
Convert an integer to a Roman numeral, supporting Vinculum notation
|
Convert an integer to a Roman numeral, supporting Vinculum notation
|
||||||
|
@ -77,9 +59,8 @@ def int_to_roman(number: int) -> str:
|
||||||
|
|
||||||
Given an integer, convert it to a Roman numeral.
|
Given an integer, convert it to a Roman numeral.
|
||||||
|
|
||||||
Reference:https://en.wikipedia.org/wiki/Roman_numerals
|
Reference: https://en.wikipedia.org/wiki/Roman_numerals
|
||||||
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500,
|
>>> tests = {3: "III", 154: "CLIV", 1009: "MIX", 2500: "MMD", 3999: "MMMCMXCIX"}
|
||||||
... "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
|
|
||||||
>>> 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
|
||||||
"""
|
"""
|
||||||
|
@ -93,9 +74,7 @@ def int_to_roman(number: int) -> str:
|
||||||
if number == 0:
|
if number == 0:
|
||||||
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