mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
0e5eb46ddd
commit
588ab7bee4
@ -1,20 +1,33 @@
|
||||
ROMAN_NUMERALS = {
|
||||
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"
|
||||
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):
|
||||
result = 0
|
||||
i = 0
|
||||
while i < len(roman):
|
||||
if i + 1 < len(roman) and roman[i:i+2] in ROMAN_NUMERALS:
|
||||
result += ROMAN_NUMERALS[roman[i:i+2]]
|
||||
if i + 1 < len(roman) and roman[i : i + 2] in ROMAN_NUMERALS:
|
||||
result += ROMAN_NUMERALS[roman[i : i + 2]]
|
||||
i += 2
|
||||
else:
|
||||
result += ROMAN_NUMERALS[roman[i]]
|
||||
i += 1
|
||||
return result
|
||||
|
||||
|
||||
def int_to_roman(number):
|
||||
result = ""
|
||||
for value, numeral in ROMAN_NUMERALS.items():
|
||||
@ -23,6 +36,8 @@ def int_to_roman(number):
|
||||
number -= value
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
Loading…
Reference in New Issue
Block a user