Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions problem_014/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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)
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__":
# 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)
29 changes: 29 additions & 0 deletions problem_014/test_inputs.json
Original file line number Diff line number Diff line change
@@ -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": ""
}
]
}