Skip to content

Commit e6234ce

Browse files
辰言辰言
authored andcommitted
feat(conversions): add input validation and fix bugs in Roman Numerals
1 parent e3b01ec commit e6234ce

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

conversions/roman_numerals.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,26 @@ def roman_to_int(roman: str) -> int:
2424
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999}
2525
>>> all(roman_to_int(key) == value for key, value in tests.items())
2626
True
27+
>>> roman_to_int("iii")
28+
3
29+
>>> roman_to_int("")
30+
Traceback (most recent call last):
31+
...
32+
ValueError: Input cannot be an empty string
33+
>>> roman_to_int("MIX-abc")
34+
Traceback (most recent call last):
35+
...
36+
ValueError: Invalid Roman numeral character: -
2737
"""
2838
vals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
39+
roman = roman.upper()
40+
if not roman:
41+
raise ValueError("Input cannot be an empty string")
42+
for char in roman:
43+
if char not in vals:
44+
msg = f"Invalid Roman numeral character: {char}"
45+
raise ValueError(msg)
46+
2947
total = 0
3048
place = 0
3149
while place < len(roman):
@@ -40,12 +58,31 @@ def roman_to_int(roman: str) -> int:
4058

4159
def int_to_roman(number: int) -> str:
4260
"""
43-
Given a integer, convert it to an roman numeral.
61+
Given an integer, convert it to a roman numeral.
4462
https://en.wikipedia.org/wiki/Roman_numerals
4563
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999}
4664
>>> all(int_to_roman(value) == key for key, value in tests.items())
4765
True
66+
>>> int_to_roman(0)
67+
Traceback (most recent call last):
68+
...
69+
ValueError: Input must be an integer between 1 and 3999
70+
>>> int_to_roman(-5)
71+
Traceback (most recent call last):
72+
...
73+
ValueError: Input must be an integer between 1 and 3999
74+
>>> int_to_roman(4000)
75+
Traceback (most recent call last):
76+
...
77+
ValueError: Input must be an integer between 1 and 3999
78+
>>> int_to_roman(1.5) # type: ignore[arg-type]
79+
Traceback (most recent call last):
80+
...
81+
ValueError: Input must be an integer between 1 and 3999
4882
"""
83+
if not isinstance(number, int) or not (0 < number < 4000):
84+
raise ValueError("Input must be an integer between 1 and 3999")
85+
4986
result = []
5087
for arabic, roman in ROMAN:
5188
(factor, number) = divmod(number, arabic)

0 commit comments

Comments
 (0)