From 63c993c548c1dbc2f84a3c6e1088ded6c0311aaf Mon Sep 17 00:00:00 2001 From: Cal Barkman Date: Sun, 19 Oct 2025 22:10:48 -0700 Subject: [PATCH] Problem 028 in an okay answer --- problem_028/solution.py | 39 ++++++++++++++++++++++++++++++++++++ problem_028/test_inputs.json | 26 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 problem_028/solution.py create mode 100644 problem_028/test_inputs.json diff --git a/problem_028/solution.py b/problem_028/solution.py new file mode 100644 index 0000000..7f507c8 --- /dev/null +++ b/problem_028/solution.py @@ -0,0 +1,39 @@ +# 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 strStr(self, haystack: str, needle: str) -> int: + retval = 0 + max_length = len(haystack) + needle_length = len(needle) + max_start_position = max_length - needle_length + while(retval <= max_start_position): + if(haystack[retval] == needle[0]): + # This is very hacky... + chop = haystack[retval:retval+needle_length] + if(chop == needle): + return retval + retval += 1 + # If we didn't find it, + if(retval >= max_start_position): + retval = -1 + 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_028/test_inputs.json b/problem_028/test_inputs.json new file mode 100644 index 0000000..6e3a5da --- /dev/null +++ b/problem_028/test_inputs.json @@ -0,0 +1,26 @@ +{ + "method": "strStr", + "tests": [ + { + "Input": { + "haystack": "sadbutsad", + "needle": "sad" + }, + "Output": "0" + }, + { + "Input": { + "haystack": "leetcode", + "needle": "leeto" + }, + "Output": "-1" + }, + { + "Input": { + "haystack": "a", + "needle": "a" + }, + "Output": "0" + } + ] +} \ No newline at end of file