mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Update roman_numerals.py
Reduced lines of code, time complexity , reduced error matrix. Increased options for the user
This commit is contained in:
parent
72f52df420
commit
0e5eb46ddd
@ -1,61 +1,28 @@
|
|||||||
ROMAN = [
|
ROMAN_NUMERALS = {
|
||||||
(1000, "M"),
|
1000: "M", 900: "CM", 500: "D", 400: "CD", 100: "C",
|
||||||
(900, "CM"),
|
90: "XC", 50: "L", 40: "XL", 10: "X", 9: "IX", 5: "V", 4: "IV", 1: "I"
|
||||||
(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: str) -> int:
|
result = 0
|
||||||
"""
|
i = 0
|
||||||
LeetCode No. 13 Roman to Integer
|
while i < len(roman):
|
||||||
Given a roman numeral, convert it to an integer.
|
if i + 1 < len(roman) and roman[i:i+2] in ROMAN_NUMERALS:
|
||||||
Input is guaranteed to be within the range from 1 to 3999.
|
result += ROMAN_NUMERALS[roman[i:i+2]]
|
||||||
https://en.wikipedia.org/wiki/Roman_numerals
|
i += 2
|
||||||
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999}
|
|
||||||
>>> all(roman_to_int(key) == value for key, value in tests.items())
|
|
||||||
True
|
|
||||||
"""
|
|
||||||
vals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
|
|
||||||
total = 0
|
|
||||||
place = 0
|
|
||||||
while place < len(roman):
|
|
||||||
if (place + 1 < len(roman)) and (vals[roman[place]] < vals[roman[place + 1]]):
|
|
||||||
total += vals[roman[place + 1]] - vals[roman[place]]
|
|
||||||
place += 2
|
|
||||||
else:
|
else:
|
||||||
total += vals[roman[place]]
|
result += ROMAN_NUMERALS[roman[i]]
|
||||||
place += 1
|
i += 1
|
||||||
return total
|
return result
|
||||||
|
|
||||||
|
|
||||||
def int_to_roman(number: int) -> str:
|
|
||||||
"""
|
|
||||||
Given a integer, convert it to an roman numeral.
|
|
||||||
https://en.wikipedia.org/wiki/Roman_numerals
|
|
||||||
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999}
|
|
||||||
>>> all(int_to_roman(value) == key for key, value in tests.items())
|
|
||||||
True
|
|
||||||
"""
|
|
||||||
result = []
|
|
||||||
for arabic, roman in ROMAN:
|
|
||||||
(factor, number) = divmod(number, arabic)
|
|
||||||
result.append(roman * factor)
|
|
||||||
if number == 0:
|
|
||||||
break
|
|
||||||
return "".join(result)
|
|
||||||
|
|
||||||
|
def int_to_roman(number):
|
||||||
|
result = ""
|
||||||
|
for value, numeral in ROMAN_NUMERALS.items():
|
||||||
|
while number >= value:
|
||||||
|
result += numeral
|
||||||
|
number -= value
|
||||||
|
return result
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user