From 5de6f728f139e7edc267a3cd6f0ffe5146bb41b7 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 11:44:48 +0800 Subject: [PATCH 01/21] New feature: Support for converting numbers in a larger range. To extend the current solution to support even larger ranges of numbers beyond 1,000,000, we can enhance the Roman numeral system to use additional notation for larger values. In Roman numerals, there is no native representation for numbers greater than 1,000,000. --- conversions/roman_numerals.py | 73 ++++++++++++++++------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 75af2ac72882..422c4ead6fe7 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,61 +1,56 @@ ROMAN = [ - (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: """ - 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. - https://en.wikipedia.org/wiki/Roman_numerals - >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} + 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. + ​    https://en.wikipedia.org/wiki/Roman_numerals + ​    >>> all(roman_to_int(key) == value for key, value in tests.items()) + >>> 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 = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} - total = 0 - place = 0 - while place < len(roman): - if (place + 1 < len(roman)) and (vals[roman[place]] < vals[roman[place + 1]]): - total += vals[roman[place + 1]] - vals[roman[place]] - place += 2 + vals = { + "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, + "I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000, "D_": 500000, "M_": 1000000 + } + i, total = 0, 0 + while i < len(roman): + if i + 1 < len(roman) and (roman[i:i+2] in vals): # 处理 `_` 记法 + total += vals[roman[i:i+2]] + i += 2 else: - total += vals[roman[place]] - place += 1 + total += vals[roman[i]] + i += 1 return total - - -def int_to_roman(number: int) -> str: + def int_to_roman(number: int) -> str: """ - Given a integer, convert it to an roman numeral. - https://en.wikipedia.org/wiki/Roman_numerals - >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} + Convert an integer to a Roman numeral, supporting Vinculum notation (underscore _ represents 1000 times). +  Given a integer, convert it to an roman numeral. + ​    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(int_to_roman(value) == key for key, value in tests.items()) True """ + if not isinstance(number, int) or number < 1: + raise ValueError("Input must be a positive integer greater than 0") + result = [] for arabic, roman in ROMAN: - (factor, number) = divmod(number, arabic) + factor, number = divmod(number, arabic) result.append(roman * factor) if number == 0: - break + reak return "".join(result) - if __name__ == "__main__": import doctest - doctest.testmod() From 1e48591d72b3e872f8b2c5e697ee9e94785a6e2c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 03:49:20 +0000 Subject: [PATCH 02/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 422c4ead6fe7..7aacae52cc06 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -9,10 +9,10 @@ def roman_to_int(roman: str) -> int: """ 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. - ​    https://en.wikipedia.org/wiki/Roman_numerals + 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. + ​    https://en.wikipedia.org/wiki/Roman_numerals ​    >>> all(roman_to_int(key) == value for key, value in tests.items()) >>> 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()) @@ -34,7 +34,7 @@ def roman_to_int(roman: str) -> int: def int_to_roman(number: int) -> str: """ Convert an integer to a Roman numeral, supporting Vinculum notation (underscore _ represents 1000 times). -  Given a integer, convert it to an roman numeral. +  Given a integer, convert it to an roman numeral. ​    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(int_to_roman(value) == key for key, value in tests.items()) From f5cdd52aa0c8bf1e77892af0912c1f03632786c8 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 11:51:49 +0800 Subject: [PATCH 03/21] Update roman_numerals.py --- conversions/roman_numerals.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 7aacae52cc06..2d7a10c6cb8f 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -8,8 +8,8 @@ ] def roman_to_int(roman: str) -> int: """ - Convert a Roman numeral to an integer, supporting Vinculum notation (underscore _ represents 1000 times). - LeetCode No. 13 Roman to Integer + 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. ​    https://en.wikipedia.org/wiki/Roman_numerals @@ -31,7 +31,7 @@ def roman_to_int(roman: str) -> int: total += vals[roman[i]] i += 1 return total - def int_to_roman(number: int) -> str: + def int_to_roman(number: int) -> str: """ Convert an integer to a Roman numeral, supporting Vinculum notation (underscore _ represents 1000 times).  Given a integer, convert it to an roman numeral. @@ -48,7 +48,7 @@ def int_to_roman(number: int) -> str: factor, number = divmod(number, arabic) result.append(roman * factor) if number == 0: - reak + break return "".join(result) if __name__ == "__main__": From eb8598f2396858b78a47ecd518488237ff9dc347 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:18:58 +0800 Subject: [PATCH 04/21] Update roman_numerals.py --- conversions/roman_numerals.py | 47 +++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 2d7a10c6cb8f..4752b37e4c1f 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -8,35 +8,46 @@ ] def roman_to_int(roman: str) -> int: """ - 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. - ​    https://en.wikipedia.org/wiki/Roman_numerals - ​    >>> all(roman_to_int(key) == value for key, value in tests.items()) - >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000} + 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 = { - "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, - "I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000, "D_": 500000, "M_": 1000000 + "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, + "I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000, + "D_": 500000, "M_": 1000000 } + i, total = 0, 0 while i < len(roman): - if i + 1 < len(roman) and (roman[i:i+2] in vals): # 处理 `_` 记法 + 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: int) -> str: """ - Convert an integer to a Roman numeral, supporting Vinculum notation (underscore _ represents 1000 times). -  Given a integer, convert it to an roman numeral. - ​    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} + 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 = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, + ... "MMMCMXCIX": 3999, "I_V_": 4000, "X_": 10000, "M_": 1000000} >>> all(int_to_roman(value) == key for key, value in tests.items()) True """ @@ -48,9 +59,9 @@ def int_to_roman(number: int) -> str: factor, number = divmod(number, arabic) result.append(roman * factor) if number == 0: - break + break return "".join(result) - -if __name__ == "__main__": + + if __name__ == "__main__": import doctest doctest.testmod() From ad28671b3b85014a1722ba800aa36ea4755442f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 08:19:12 +0000 Subject: [PATCH 05/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 4752b37e4c1f..61c3eb0bc32d 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -8,7 +8,7 @@ ] def roman_to_int(roman: str) -> int: """ - Convert a Roman numeral to an integer, supporting Vinculum notation + Convert a Roman numeral to an integer, supporting Vinculum notation (underscore _ represents 1000 times). LeetCode No. 13 Roman to Integer: @@ -26,7 +26,7 @@ def roman_to_int(roman: str) -> int: "I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000, "D_": 500000, "M_": 1000000 } - + i, total = 0, 0 while i < len(roman): if i + 1 < len(roman) and roman[i:i+2] in vals: @@ -36,11 +36,11 @@ def roman_to_int(roman: str) -> int: total += vals[roman[i]] i += 1 return total - + def int_to_roman(number: int) -> str: """ - Convert an integer to a Roman numeral, supporting Vinculum notation + Convert an integer to a Roman numeral, supporting Vinculum notation (underscore _ represents 1000 times). Given an integer, convert it to a Roman numeral. @@ -61,7 +61,7 @@ def int_to_roman(number: int) -> str: if number == 0: break return "".join(result) - + if __name__ == "__main__": import doctest doctest.testmod() From 8499e59ae56a52fab514fb4a7c34bcf2fcf3810e Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:23:43 +0800 Subject: [PATCH 06/21] Update roman_numerals.py --- conversions/roman_numerals.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 61c3eb0bc32d..02d184a05a28 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -63,5 +63,5 @@ def int_to_roman(number: int) -> str: return "".join(result) if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + doctest.testmod() From 99a081bc3cddd6d8cc26991b7dff04ea71ddc8cd Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:29:23 +0800 Subject: [PATCH 07/21] Update roman_numerals.py --- conversions/roman_numerals.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 02d184a05a28..61c3eb0bc32d 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -63,5 +63,5 @@ def int_to_roman(number: int) -> str: return "".join(result) if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + doctest.testmod() From bcfd98cbe4a9a9347a88d0adc9356f15c4e7533e Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:33:21 +0800 Subject: [PATCH 08/21] Update roman_numerals.py --- conversions/roman_numerals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 61c3eb0bc32d..54ac8603a779 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -62,6 +62,6 @@ def int_to_roman(number: int) -> str: break return "".join(result) - if __name__ == "__main__": +if __name__ == "__main__": import doctest doctest.testmod() From 1735aed47490b007645190ea283112b78a6d2da2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 08:33:43 +0000 Subject: [PATCH 09/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 56 ++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 54ac8603a779..d82eca718699 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,11 +1,32 @@ 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: """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -22,15 +43,26 @@ def roman_to_int(roman: str) -> int: True """ vals = { - "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, - "I_": 1000, "V_": 5000, "X_": 10000, "L_": 50000, "C_": 100000, - "D_": 500000, "M_": 1000000 + "I": 1, + "V": 5, + "X": 10, + "L": 50, + "C": 100, + "D": 500, + "M": 1000, + "I_": 1000, + "V_": 5000, + "X_": 10000, + "L_": 50000, + "C_": 100000, + "D_": 500000, + "M_": 1000000, } i, total = 0, 0 while i < len(roman): - if i + 1 < len(roman) and roman[i:i+2] in vals: - total += vals[roman[i:i+2]] + 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]] @@ -62,6 +94,8 @@ def int_to_roman(number: int) -> str: break return "".join(result) + if __name__ == "__main__": import doctest + doctest.testmod() From 6bff12142ea69fb6c3acefde8596feeca8e6666f Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 17:25:30 +0800 Subject: [PATCH 10/21] Update roman_numerals.py --- conversions/roman_numerals.py | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index d82eca718699..da32198c4b0e 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -25,8 +25,6 @@ (4, "IV"), (1, "I"), ] - - def roman_to_int(roman: str) -> int: """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -42,34 +40,18 @@ def roman_to_int(roman: str) -> int: >>> all(roman_to_int(key) == value for key, value in tests.items()) True """ - vals = { - "I": 1, - "V": 5, - "X": 10, - "L": 50, - "C": 100, - "D": 500, - "M": 1000, - "I_": 1000, - "V_": 5000, - "X_": 10000, - "L_": 50000, - "C_": 100000, - "D_": 500000, - "M_": 1000000, - } + vals = dict(ROMAN) # Convert the list of tuples to a dictionary i, total = 0, 0 while i < len(roman): - if i + 1 < len(roman) and roman[i : i + 2] in vals: - total += vals[roman[i : i + 2]] + # 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]] i += 2 else: total += vals[roman[i]] i += 1 return total - - def int_to_roman(number: int) -> str: """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -77,9 +59,8 @@ def int_to_roman(number: int) -> str: Given an integer, convert it to a Roman numeral. - 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} + 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 """ @@ -93,9 +74,7 @@ def int_to_roman(number: int) -> str: if number == 0: break return "".join(result) - - + if __name__ == "__main__": import doctest - doctest.testmod() From dd13fe23b057b0df71c996fa5b466c3ecaae4bfb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 09:25:53 +0000 Subject: [PATCH 11/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index da32198c4b0e..548620c7a14e 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -25,6 +25,8 @@ (4, "IV"), (1, "I"), ] + + def roman_to_int(roman: str) -> int: """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -45,13 +47,15 @@ def roman_to_int(roman: str) -> int: 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]] + 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: """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -74,7 +78,9 @@ def int_to_roman(number: int) -> str: if number == 0: break return "".join(result) - + + if __name__ == "__main__": import doctest + doctest.testmod() From 8b10293947937f8e94edd83e1608b360430268f4 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 17:38:11 +0800 Subject: [PATCH 12/21] Update roman_numerals.py --- conversions/roman_numerals.py | 43 ++++++++--------------------------- 1 file changed, 9 insertions(+), 34 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 548620c7a14e..d29055e368ca 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,32 +1,11 @@ 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: """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -47,15 +26,13 @@ def roman_to_int(roman: str) -> int: 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]] + 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: """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -78,9 +55,7 @@ def int_to_roman(number: int) -> str: if number == 0: break return "".join(result) - - + if __name__ == "__main__": import doctest - doctest.testmod() From 5e41453c5e1ae0baedd1a31de224bbf362499388 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 09:39:01 +0000 Subject: [PATCH 13/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 43 +++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index d29055e368ca..548620c7a14e 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,11 +1,32 @@ 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: """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -26,13 +47,15 @@ def roman_to_int(roman: str) -> int: 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]] + 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: """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -55,7 +78,9 @@ def int_to_roman(number: int) -> str: if number == 0: break return "".join(result) - + + if __name__ == "__main__": import doctest + doctest.testmod() From 657073d14036311436381f171f076244bf5190e7 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 17:48:43 +0800 Subject: [PATCH 14/21] Update roman_numerals.py --- conversions/roman_numerals.py | 52 ++++++++++------------------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 548620c7a14e..68f8782d3851 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -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). @@ -42,21 +21,20 @@ def roman_to_int(roman: str) -> int: >>> 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_) + 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). @@ -78,9 +56,7 @@ def int_to_roman(number: int) -> str: if number == 0: break return "".join(result) - - + if __name__ == "__main__": import doctest - doctest.testmod() From da299d48fe7ceffc478952fb9899a5a5f7d96c8e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 09:49:05 +0000 Subject: [PATCH 15/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 43 +++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 68f8782d3851..eefab001fd19 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,11 +1,32 @@ 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): """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -27,13 +48,15 @@ def roman_to_int(roman): i, total = 0, 0 while i < len(roman): # 先匹配 2 个字符的罗马数字(如 I_、X_) - if i + 1 < len(roman) and roman[i:i+2] in vals: - total += vals[roman[i:i+2]] + 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): """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -56,7 +79,9 @@ def int_to_roman(number): if number == 0: break return "".join(result) - + + if __name__ == "__main__": import doctest + doctest.testmod() From ba434208970fec89843a0b694ce1d3be49cd036c Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 17:53:35 +0800 Subject: [PATCH 16/21] Update roman_numerals.py --- conversions/roman_numerals.py | 44 +++++++---------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index eefab001fd19..eba367142e60 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,32 +1,11 @@ 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): """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -47,16 +26,13 @@ def roman_to_int(roman): i, total = 0, 0 while i < len(roman): - # 先匹配 2 个字符的罗马数字(如 I_、X_) - if i + 1 < len(roman) and roman[i : i + 2] in vals: - total += vals[roman[i : i + 2]] + 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): """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -79,9 +55,7 @@ def int_to_roman(number): if number == 0: break return "".join(result) - - + if __name__ == "__main__": import doctest - doctest.testmod() From e6ea0ba222ac2fba1a7a83c53caf43da33ff1d63 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 09:53:57 +0000 Subject: [PATCH 17/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 43 +++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index eba367142e60..24183ea4d2a3 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,11 +1,32 @@ 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): """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -26,13 +47,15 @@ def roman_to_int(roman): i, total = 0, 0 while i < len(roman): - if i + 1 < len(roman) and roman[i:i+2] in vals: - total += vals[roman[i:i+2]] + 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): """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -55,7 +78,9 @@ def int_to_roman(number): if number == 0: break return "".join(result) - + + if __name__ == "__main__": import doctest + doctest.testmod() From 07721a89e2319315dcf4f7b6d482adf406727e01 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 18:12:37 +0800 Subject: [PATCH 18/21] Update roman_numerals.py --- conversions/roman_numerals.py | 56 +++++++++-------------------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 24183ea4d2a3..a4eaa16b538b 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,32 +1,11 @@ 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): """ Convert a Roman numeral to an integer, supporting Vinculum notation @@ -43,19 +22,14 @@ def roman_to_int(roman): True """ vals = {roman: arabic for arabic, roman in ROMAN} - # Convert the list of tuples to a dictionary - i, total = 0, 0 while i < len(roman): - 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 - - + if i + 1 < len(roman) and roman[i + 1] == "_": + total += vals[roman[i] + "_"] + i += 2 + else: + total += vals[roman[i]] + i += 1 def int_to_roman(number): """ Convert an integer to a Roman numeral, supporting Vinculum notation @@ -68,7 +42,7 @@ def int_to_roman(number): >>> all(int_to_roman(value) == key for key, value in tests.items()) True """ - if not isinstance(number, int) or number < 1: + if not isinstance(number, int) or number <= 0: raise ValueError("Input must be a positive integer greater than 0") result = [] @@ -78,9 +52,7 @@ def int_to_roman(number): if number == 0: break return "".join(result) - - + if __name__ == "__main__": import doctest - doctest.testmod() From 668b78a6d4c2434ceb13af5793725602ac82052b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 10:13:49 +0000 Subject: [PATCH 19/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index a4eaa16b538b..0c6e3edca6ba 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -24,7 +24,7 @@ def roman_to_int(roman): 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] == "_": + if i + 1 < len(roman) and roman[i + 1] == "_": total += vals[roman[i] + "_"] i += 2 else: @@ -52,7 +52,7 @@ def int_to_roman(number): if number == 0: break return "".join(result) - + if __name__ == "__main__": import doctest doctest.testmod() From f4682f186f1f35d753f4955242717557307580cf Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Mon, 10 Feb 2025 18:26:56 +0800 Subject: [PATCH 20/21] Update roman_numerals.py --- conversions/roman_numerals.py | 42 ++++++++--------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 0c6e3edca6ba..305ef88087fc 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,5 +1,5 @@ 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"), @@ -7,41 +7,17 @@ (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") @@ -52,7 +28,7 @@ def int_to_roman(number): if number == 0: break return "".join(result) - + if __name__ == "__main__": import doctest doctest.testmod() From 30a49b3046f95c2b89769f69bda7703d3fee4804 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Feb 2025 10:27:19 +0000 Subject: [PATCH 21/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/roman_numerals.py | 39 ++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 305ef88087fc..2e1d5f7026a5 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,11 +1,32 @@ 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): vals = {roman: arabic for arabic, roman in ROMAN} i, total = 0, 0 @@ -17,6 +38,8 @@ def roman_to_int(roman): total += vals[roman[i]] i += 1 return total + + def int_to_roman(number): if not isinstance(number, int) or number <= 0: raise ValueError("Input must be a positive integer greater than 0") @@ -28,7 +51,9 @@ def int_to_roman(number): if number == 0: break return "".join(result) - + + if __name__ == "__main__": import doctest + doctest.testmod()