Skip to content
Closed
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion conversions/excel_title_to_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@
28
>>> excel_title_to_column("Z")
26
>>> excel_title_to_column("a")
1
>>> excel_title_to_column("ab")
28
>>> excel_title_to_column("")
Traceback (most recent call last):
...
ValueError: Column title must be a non-empty string containing only alphabetic characters.

Check failure on line 22 in conversions/excel_title_to_column.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E501)

conversions/excel_title_to_column.py:22:89: E501 Line too long (94 > 88)
>>> excel_title_to_column("A1")
Traceback (most recent call last):
...
ValueError: Column title must be a non-empty string containing only alphabetic characters.

Check failure on line 26 in conversions/excel_title_to_column.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E501)

conversions/excel_title_to_column.py:26:89: E501 Line too long (94 > 88)
"""
assert column_title.isupper()
if not column_title or not column_title.isalpha():
raise ValueError(
"Column title must be a non-empty string containing only alphabetic characters."

Check failure on line 30 in conversions/excel_title_to_column.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E501)

conversions/excel_title_to_column.py:30:89: E501 Line too long (92 > 88)
)

column_title = column_title.upper()
answer = 0
index = len(column_title) - 1
power = 0
Expand Down
38 changes: 37 additions & 1 deletion conversions/roman_numerals.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,25 @@
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999}
>>> all(roman_to_int(key) == value for key, value in tests.items())
True
>>> roman_to_int("iii")
3
>>> roman_to_int("")
Traceback (most recent call last):
...
ValueError: Input cannot be an empty string
>>> roman_to_int("MIX-abc")
Traceback (most recent call last):
...
ValueError: Invalid Roman numeral character: -
"""
vals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
roman = roman.upper()
if not roman:
raise ValueError("Input cannot be an empty string")
for char in roman:
if char not in vals:
raise ValueError(f"Invalid Roman numeral character: {char}")

Check failure on line 44 in conversions/roman_numerals.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (EM102)

conversions/roman_numerals.py:44:30: EM102 Exception must not use an f-string literal, assign to variable first help: Assign to variable; remove f-string literal

total = 0
place = 0
while place < len(roman):
Expand All @@ -40,12 +57,31 @@

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.
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: Input must be an integer between 1 and 3999
>>> int_to_roman(-5)
Traceback (most recent call last):
...
ValueError: Input must be an integer between 1 and 3999
>>> int_to_roman(4000)
Traceback (most recent call last):
...
ValueError: Input must be an integer between 1 and 3999
>>> int_to_roman(1.5) # type: ignore[arg-type]
Traceback (most recent call last):
...
ValueError: Input must be an integer between 1 and 3999
"""
if not isinstance(number, int) or not (0 < number < 4000):
raise ValueError("Input must be an integer between 1 and 3999")

result = []
for arabic, roman in ROMAN:
(factor, number) = divmod(number, arabic)
Expand Down
Loading