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 657073d14036311436381f171f076244bf5190e7
52 changes: 14 additions & 38 deletions conversions/roman_numerals.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
ROMAN = [
(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"),
(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: str) -> int:
def roman_to_int(roman):
"""
Convert a Roman numeral to an integer, supporting Vinculum notation
(underscore _ represents 1000 times).
Expand All @@ -42,21 +21,20 @@
>>> all(roman_to_int(key) == value for key, value in tests.items())
True
"""
vals = dict(ROMAN) # Convert the list of tuples to a dictionary
vals = {roman: arabic for arabic, roman in ROMAN}
# Convert the list of tuples to a dictionary

i, total = 0, 0
while i < len(roman):
# Check for 2-character symbols first (like I_ or X_)
if i + 1 < len(roman) and roman[i : i + 2] in vals:
total += vals[roman[i : i + 2]]
# 先匹配 2 个字符的罗马数字(如 I_、X_)

Check failure on line 29 in conversions/roman_numerals.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF003)

conversions/roman_numerals.py:29:25: RUF003 Comment contains ambiguous `(` (FULLWIDTH LEFT PARENTHESIS). Did you mean `(` (LEFT PARENTHESIS)?

Check failure on line 29 in conversions/roman_numerals.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF003)

conversions/roman_numerals.py:29:33: RUF003 Comment contains ambiguous `)` (FULLWIDTH RIGHT PARENTHESIS). Did you mean `)` (RIGHT PARENTHESIS)?
if i + 1 < len(roman) and roman[i:i+2] in vals:
total += vals[roman[i:i+2]]
i += 2
else:
total += vals[roman[i]]
i += 1
return total


def int_to_roman(number: int) -> str:
def int_to_roman(number):
"""
Convert an integer to a Roman numeral, supporting Vinculum notation
(underscore _ represents 1000 times).
Expand All @@ -78,9 +56,7 @@
if number == 0:
break
return "".join(result)



Check failure on line 59 in conversions/roman_numerals.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

conversions/roman_numerals.py:59:1: W293 Blank line contains whitespace
if __name__ == "__main__":
import doctest

doctest.testmod()
Loading
0