From a7cb1ccdb5489882b95e4115986dc0c160ef8f21 Mon Sep 17 00:00:00 2001 From: Cal Barkman Date: Fri, 17 Oct 2025 21:45:00 -0700 Subject: [PATCH 1/2] Problem 012 first pass --- problem_012/solution.py | 103 +++++++++++++++++++++++++++++++++++ problem_012/test_inputs.json | 25 +++++++++ 2 files changed, 128 insertions(+) create mode 100644 problem_012/solution.py create mode 100644 problem_012/test_inputs.json diff --git a/problem_012/solution.py b/problem_012/solution.py new file mode 100644 index 0000000..eb95f8c --- /dev/null +++ b/problem_012/solution.py @@ -0,0 +1,103 @@ +# Extra header to ensure that tests can run individually and as a suite +# ------------------------------------------------------------------- +import sys +from pathlib import Path +# Add the project root folder to the Python path +sys.path.append(str(Path(__file__).resolve().parents[1])) +# ------------------------------------------------------------------- + +# Now you can import your wrapper +from test_runner.wrapper import run_tests + +class Solution: + def intToRoman(self, num: int) -> str: + int_str = str(num) + int_order = len(int_str) + retval = "" + for char in int_str: + if(retval and char == "0"): + # the next character is 0 so... don't do anything? + print("a") + else: + retval += self.convert_character(char, 10**(int_order-1)) + int_order = int_order - 1 + return retval + + def convert_character(self, char: str, multiple: int): + intermediate = "" + retval = "" + match char: + case '1': + intermediate = "I" + case '2': + intermediate = "II" + case '3': + intermediate = "III" + case '4': + intermediate = "IV" + case '5': + intermediate = "V" + case '6': + intermediate = "VI" + case '7': + intermediate = "VII" + case '8': + intermediate = "VIII" + case '9': + intermediate = "IX" + case '0': + intermediate = "X" + # Skip returning anything if we are handling the lowest power (aka handle when we have a 0 in the last digit) + if(multiple == 1): + return "" + case _: + print("error, a character that is not 0 to 9 was processed somehow...") + # Now convert the number to the proper scale + for char in intermediate: + match char: + case "I": + match multiple: + case 1: + char = "I" + case 10: + char = "X" + case 100: + char = "C" + case 1000: + char = "M" + case _: + print("multiple too high case 1") + case "V": + match multiple: + case 1: + char = "V" + case 10: + char = "L" + case 100: + char = "D" + case _: + print("multiple too high case 2") + case "X": + match multiple: + case 1: + char = "X" + case 10: + char = "C" + case 100: + char = "M" + case _: + print("error case 2") + retval += char + + return retval + + +if __name__ == "__main__": + # Get the directory where this solution.py script lives + script_dir = Path(__file__).parent + + # Join the script's directory with the JSON filename to create a full path + # Make sure your file is actually named "test_inputs.json"! + test_file_path = script_dir / "test_inputs.json" + + run_tests(Solution, test_file_path) \ No newline at end of file diff --git a/problem_012/test_inputs.json b/problem_012/test_inputs.json new file mode 100644 index 0000000..e597d9e --- /dev/null +++ b/problem_012/test_inputs.json @@ -0,0 +1,25 @@ +{ + "method": "intToRoman", + "tests": [ + { + "Input": "3749", + "Output": "MMMDCCXLIX" + }, + { + "Input": "58", + "Output": "LVIII" + }, + { + "Input": "1994", + "Output": "MCMXCIV" + }, + { + "Input": "10", + "Output": "X" + }, + { + "Input": "100", + "Output": "C" + } + ] +} \ No newline at end of file From 8af0353295ac1e21bd5756281ac2d1220efcdf1e Mon Sep 17 00:00:00 2001 From: Cal Barkman Date: Fri, 17 Oct 2025 21:47:51 -0700 Subject: [PATCH 2/2] Tiny improvements --- problem_012/solution.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/problem_012/solution.py b/problem_012/solution.py index eb95f8c..1fa2ee1 100644 --- a/problem_012/solution.py +++ b/problem_012/solution.py @@ -15,11 +15,9 @@ def intToRoman(self, num: int) -> str: int_order = len(int_str) retval = "" for char in int_str: - if(retval and char == "0"): - # the next character is 0 so... don't do anything? - print("a") - else: + if(char != "0"): retval += self.convert_character(char, 10**(int_order-1)) + # else the next character is 0 so don't do anything int_order = int_order - 1 return retval @@ -45,11 +43,11 @@ def convert_character(self, char: str, multiple: int): intermediate = "VIII" case '9': intermediate = "IX" - case '0': - intermediate = "X" - # Skip returning anything if we are handling the lowest power (aka handle when we have a 0 in the last digit) - if(multiple == 1): - return "" + # case '0': + # intermediate = "X" + # # Skip returning anything if we are handling the lowest power (aka handle when we have a 0 in the last digit) + # if(multiple == 1): + # return "" case _: print("error, a character that is not 0 to 9 was processed somehow...") # Now convert the number to the proper scale