From 9e0e47c76ed5b0ee39db375d53d7d2e90877918e Mon Sep 17 00:00:00 2001 From: Cal Barkman Date: Fri, 17 Oct 2025 23:40:48 -0700 Subject: [PATCH 1/2] Problem 014 - not finished --- problem_014/solution.py | 35 +++++++++++++++++++++++++++++++++++ problem_014/test_inputs.json | 29 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 problem_014/solution.py create mode 100644 problem_014/test_inputs.json diff --git a/problem_014/solution.py b/problem_014/solution.py new file mode 100644 index 0000000..8e01e7d --- /dev/null +++ b/problem_014/solution.py @@ -0,0 +1,35 @@ +# 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 +from typing import List + +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + prefix = strs[0] + for word in strs: + word_len = len(word) + try: + for iter in range(word_len): + if(prefix[iter] != word[iter]): + break + prefix = word[0:iter] + except Exception as e: + print(f"Exception e = {e}") + return prefix + +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_014/test_inputs.json b/problem_014/test_inputs.json new file mode 100644 index 0000000..d22c884 --- /dev/null +++ b/problem_014/test_inputs.json @@ -0,0 +1,29 @@ +{ + "method": "longestCommonPrefix", + "tests": [ + { + "Input": { + "strs": ["flower","flow","flight"] + }, + "Output": "fl" + }, + { + "Input": { + "strs": ["dog","racecar","car"] + }, + "Output": "" + }, + { + "Input": { + "strs": ["ab", "a"] + }, + "Output": "a" + }, + { + "Input": { + "strs": ["reflower","flow","flight"] + }, + "Output": "" + } + ] +} \ No newline at end of file From 4465253fd00f78ae51d178a634d88312b5ea8691 Mon Sep 17 00:00:00 2001 From: Cal Barkman Date: Fri, 17 Oct 2025 23:47:01 -0700 Subject: [PATCH 2/2] Fixed logic error in problem 014 --- problem_014/solution.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/problem_014/solution.py b/problem_014/solution.py index 8e01e7d..5ffa96f 100644 --- a/problem_014/solution.py +++ b/problem_014/solution.py @@ -15,13 +15,14 @@ def longestCommonPrefix(self, strs: List[str]) -> str: prefix = strs[0] for word in strs: word_len = len(word) - try: - for iter in range(word_len): - if(prefix[iter] != word[iter]): - break - prefix = word[0:iter] - except Exception as e: - print(f"Exception e = {e}") + prefix_len = len(prefix) + iter = 0 + while(iter < word_len and iter < prefix_len): + if(prefix[iter] != word[iter]): + break + else: + iter = iter + 1 + prefix = prefix[0:iter] return prefix if __name__ == "__main__":