8000 New feature: Support for converting numbers in a larger range. by lighting9999 · Pull Request #12576 · TheAlgorithms/Python · GitHub
[go: up one dir, main page]

Skip to content

New feature: Support for converting numbers in a larger range. #12576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5de6f72
New feature: Support for converting numbers in a larger range.
lighting9999 Feb 10, 2025
1e48591
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
f5cdd52
Update roman_numerals.py
lighting9999 Feb 10, 2025
eb8598f
Update roman_numerals.py
lighting9999 Feb 10, 2025
ad28671
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
8499e59
Update roman_numerals.py
lighting9999 Feb 10, 2025
99a081b
Update roman_numerals.py
lighting9999 Feb 10, 2025
bcfd98c
Update roman_numerals.py
lighting9999 Feb 10, 2025
1735aed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
6bff121
Update roman_numerals.py
lighting9999 Feb 10, 2025
dd13fe2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
8b10293
Update roman_numerals.py
lighting9999 Feb 10, 2025
5e41453
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
657073d
Update roman_numerals.py
lighting9999 Feb 10, 2025
da299d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
ba43420
Update roman_numerals.py
lighting9999 Feb 10, 2025
e6ea0ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
07721a8
Update roman_numerals.py
lighting9999 Feb 10, 2025
668b78a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
f4682f1
Update roman_numerals.py
lighting9999 Feb 10, 2025
30a49b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 10, 2025
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
Prev Previous commit
Next Next commit
Update roman_numerals.py
  • Loading branch information
lighting9999 authored Feb 10, 2025
commit f4682f186f1f35d753f4955242717557307580cf
42 changes: 9 additions & 33 deletions conversions/roman_numerals.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,23 @@
ROMAN = [
(1000000, "M_"), (900000, "C_M_"), (500000, "D_"), (400000, "C_D_"),
(1000000, "M_"), (900000, "C_M_"), (500000, "D_"), (400000, "C_D_"),
(100000, "C_"), (90000, "X_C_"), (50000, "L_"), (40000, "X_L_"),
(10000, "X_"), (9000, "I_X_"), (5000, "V_"), (4000, "I_V_"),
(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):
"""
Convert a Roman numeral to an integer, supporting Vinculum notation
(underscore _ represents 1000 times).

LeetCode No. 13 Roman to Integer:
Given a Roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.

Reference: https://en.wikipedia.org/wiki/Roman_numerals
>>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500,
... "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000}
>>> all(roman_to_int(key) == value for key, value in tests.items())
True
"""
vals = {roman: arabic for arabic, roman in ROMAN}
i, total = 0, 0
while i < len(roman):
if i + 1 < len(roman) and roman[i + 1] == "_":
total += vals[roman[i] + "_"]
i += 2
else:
total += vals[roman[i]]
i += 1
if i + 1 < len(roman) and roman[i + 1] == "_":
total += vals[roman[i] + "_"]
i += 2
else:
total += vals[roman[i]]
i += 1
return total
def int_to_roman(number):
"""
Convert an integer to a Roman numeral, supporting Vinculum notation
(underscore _ represents 1000 times).

Given an integer, convert it to a Roman numeral.

Reference: https://en.wikipedia.org/wiki/Roman_numerals
>>> tests = {3: "III", 154: "CLIV", 1009: "MIX", 2500: "MMD", 3999: "MMMCMXCIX"}
>>> all(int_to_roman(value) == key for key, value in tests.items())
True
"""
if not isinstance(number, int) or number <= 0:
raise ValueError("Input must be a positive integer greater than 0")

Expand All @@ -52,7 +28,7 @@
if number == 0:
break
return "".join(result)

Check failure on line 31 in conversions/roman_numerals.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

conversions/roman_numerals.py:31:1: W293 Blank line contains whitespace
if __name__ == "__main__":
import doctest
doctest.testmod()
Loading
0