From 7e8d3a71539073374e5965ccc2ea2d095998d9bd Mon Sep 17 00:00:00 2001 From: Cal Barkman Date: Sun, 19 Oct 2025 22:25:20 -0700 Subject: [PATCH] Solve an "easy" interview question I had in the past and I wanted to make sure I am confident with the answer in. --- problem_997/solution.py | 51 ++++++++++++++++++++++++++++++++++++ problem_997/test_inputs.json | 29 ++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 problem_997/solution.py create mode 100644 problem_997/test_inputs.json diff --git a/problem_997/solution.py b/problem_997/solution.py new file mode 100644 index 0000000..3b092f5 --- /dev/null +++ b/problem_997/solution.py @@ -0,0 +1,51 @@ +# 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 + +# Imports for the solution +from typing import List + +# This is a problem not found in the normal LeetCode problem list. +# The problem is to find the first and last occurance of () in a string. If less than 2 are found, return -1. +class Solution: + def firstAndLastParenthesis(self, s: str) -> List[int]: + done = False + length = len(s) + iter = 0 + foundList = [] + while(not done): + if(s[iter:iter+2] == '()'): + foundList.append(iter) + + iter += 1 + # How close to the end are we? + if(iter > length - 1): + done = True + + # Now we have a list of all the found copies, get the first and last one + if(len(foundList) < 2): + return -1 + + last = max(foundList) + first = foundList[0] + + return [first, last] + + + +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_997/test_inputs.json b/problem_997/test_inputs.json new file mode 100644 index 0000000..d5a6a3c --- /dev/null +++ b/problem_997/test_inputs.json @@ -0,0 +1,29 @@ +{ + "method": "firstAndLastParenthesis", + "tests": [ + { + "Input": "()()", + "Output": "[0, 2]" + }, + { + "Input": "()()()(())((()((()))))", + "Output": "[0, 16]" + }, + { + "Input": "()", + "Output": "-1" + }, + { + "Input": "(())", + "Output": "-1" + }, + { + "Input": "(", + "Output": "-1" + }, + { + "Input": "", + "Output": "-1" + } + ] +} \ No newline at end of file