From 9313f382ad92039cb2c30b3cd0e9c57f6193bf47 Mon Sep 17 00:00:00 2001 From: NA-V10 Date: Fri, 21 Nov 2025 16:57:08 +0530 Subject: [PATCH 1/4] feat: add integer_to_roman algorithm with doctests --- strings/integer_to_roman.py | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 strings/integer_to_roman.py diff --git a/strings/integer_to_roman.py b/strings/integer_to_roman.py new file mode 100644 index 000000000000..023cc6608742 --- /dev/null +++ b/strings/integer_to_roman.py @@ -0,0 +1,51 @@ +def integer_to_roman(n: int) -> str: + """ + Convert an integer to a Roman numeral. + + Examples: + >>> integer_to_roman(1) + 'I' + >>> integer_to_roman(4) + 'IV' + >>> integer_to_roman(9) + 'IX' + >>> integer_to_roman(58) + 'LVIII' + >>> integer_to_roman(1994) + 'MCMXCIV' + >>> integer_to_roman(0) + Traceback (most recent call last): + ... + ValueError: number must be between 1 and 3999 + """ + if not (1 <= n <= 3999): + raise ValueError("number must be between 1 and 3999") + + symbols = [ + (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"), + ] + + result = [] + for value, numeral in symbols: + while n >= value: + result.append(numeral) + n -= value + + return "".join(result) + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 33bfb8354bcb69164f176924c9a71f1f2ed4734d Mon Sep 17 00:00:00 2001 From: NA-V10 Date: Fri, 21 Nov 2025 16:58:33 +0530 Subject: [PATCH 2/4] feat: add integer_to_roman algorithm with doctests --- strings/integer_to_roman.py | 1 + 1 file changed, 1 insertion(+) diff --git a/strings/integer_to_roman.py b/strings/integer_to_roman.py index 023cc6608742..19a3a45a75b5 100644 --- a/strings/integer_to_roman.py +++ b/strings/integer_to_roman.py @@ -49,3 +49,4 @@ def integer_to_roman(n: int) -> str: if __name__ == "__main__": import doctest doctest.testmod() + \ No newline at end of file From e8c6e8f26a06273246abd4d2d41fa53eab5b28a1 Mon Sep 17 00:00:00 2001 From: NA-V10 Date: Fri, 21 Nov 2025 17:25:02 +0530 Subject: [PATCH 3/4] style: rename parameter n to descriptive name 'number' --- strings/integer_to_roman.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/strings/integer_to_roman.py b/strings/integer_to_roman.py index 19a3a45a75b5..db5f1363aac5 100644 --- a/strings/integer_to_roman.py +++ b/strings/integer_to_roman.py @@ -1,4 +1,4 @@ -def integer_to_roman(n: int) -> str: +def integer_to_roman(number: int) -> str: """ Convert an integer to a Roman numeral. @@ -18,7 +18,7 @@ def integer_to_roman(n: int) -> str: ... ValueError: number must be between 1 and 3999 """ - if not (1 <= n <= 3999): + if not (1 <= number <= 3999): raise ValueError("number must be between 1 and 3999") symbols = [ @@ -39,9 +39,9 @@ def integer_to_roman(n: int) -> str: result = [] for value, numeral in symbols: - while n >= value: + while number >= value: result.append(numeral) - n -= value + number -= value return "".join(result) @@ -49,4 +49,3 @@ def integer_to_roman(n: int) -> str: if __name__ == "__main__": import doctest doctest.testmod() - \ No newline at end of file From 8aa3682c6c1c50f50d4ce92fa8ac09835345eb4a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 21 Nov 2025 12:09:19 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/integer_to_roman.py | 1 + 1 file changed, 1 insertion(+) diff --git a/strings/integer_to_roman.py b/strings/integer_to_roman.py index db5f1363aac5..91efbe6dbda2 100644 --- a/strings/integer_to_roman.py +++ b/strings/integer_to_roman.py @@ -48,4 +48,5 @@ def integer_to_roman(number: int) -> str: if __name__ == "__main__": import doctest + doctest.testmod()