Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion conversions/roman_numerals.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,35 @@ def roman_to_int(roman: str) -> int:

def int_to_roman(number: int) -> str:
"""
Given a integer, convert it to an roman numeral.
Given an integer, convert it to a Roman numeral.
Input must be an integer in the range 1 to 3999.
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
>>> int_to_roman(0)
Traceback (most recent call last):
...
ValueError: int_to_roman only accepts integers in the range 1 to 3999, got 0
>>> int_to_roman(-1)
Traceback (most recent call last):
...
ValueError: int_to_roman only accepts integers in the range 1 to 3999, got -1
>>> int_to_roman(4000)
Traceback (most recent call last):
...
ValueError: int_to_roman only accepts integers in the range 1 to 3999, got 4000
>>> int_to_roman(True)
Traceback (most recent call last):
...
TypeError: int_to_roman only accepts integers, got bool
"""
if not isinstance(number, int) or isinstance(number, bool):
msg = f"int_to_roman only accepts integers, got {type(number).__name__}"
raise TypeError(msg)
if not 1 <= number <= 3999:
msg = f"int_to_roman only accepts integers in the range 1 to 3999, got {number}"
raise ValueError(msg)
result = []
for arabic, roman in ROMAN:
(factor, number) = divmod(number, arabic)
Expand Down