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 = {
|
ROMAN_NUMERALS = {
|
||||||
1000: "M", 900: "CM", 500: "D", 400: "CD", 100: "C",
|
1000: "M",
|
||||||
90: "XC", 50: "L", 40: "XL", 10: "X", 9: "IX", 5: "V", 4: "IV", 1: "I"
|
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):
|
||||||
result = 0
|
result = 0
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(roman):
|
while i < len(roman):
|
||||||
if i + 1 < len(roman) and roman[i:i+2] in ROMAN_NUMERALS:
|
if i + 1 < len(roman) and roman[i : i + 2] in ROMAN_NUMERALS:
|
||||||
result += ROMAN_NUMERALS[roman[i:i+2]]
|
result += ROMAN_NUMERALS[roman[i : i + 2]]
|
||||||
i += 2
|
i += 2
|
||||||
else:
|
else:
|
||||||
result += ROMAN_NUMERALS[roman[i]]
|
result += ROMAN_NUMERALS[roman[i]]
|
||||||
i += 1
|
i += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def int_to_roman(number):
|
def int_to_roman(number):
|
||||||
result = ""
|
result = ""
|
||||||
for value, numeral in ROMAN_NUMERALS.items():
|
for value, numeral in ROMAN_NUMERALS.items():
|
||||||
@ -23,6 +36,8 @@ def int_to_roman(number):
|
|||||||
number -= value
|
number -= value
|
||||||
return result
|
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