From 539301e293e237bae0a0bc2b27ca8bc879568ec2 Mon Sep 17 00:00:00 2001 From: Stefano Agbodan Date: Thu, 5 Feb 2026 22:25:17 +0100 Subject: [PATCH 1/3] feat: solve longest common prefix (naive algorithm) --- .../0014-longest-common-prefix/solution.py | 68 +++++++++++++++++++ .../test_solution.py | 37 ++++++++++ 2 files changed, 105 insertions(+) create mode 100644 py/src/arrays/0014-longest-common-prefix/solution.py create mode 100644 py/src/arrays/0014-longest-common-prefix/test_solution.py diff --git a/py/src/arrays/0014-longest-common-prefix/solution.py b/py/src/arrays/0014-longest-common-prefix/solution.py new file mode 100644 index 0000000..dec444b --- /dev/null +++ b/py/src/arrays/0014-longest-common-prefix/solution.py @@ -0,0 +1,68 @@ +from typing import List + +class Solution: + def min_len_word(pos_word: int, strs: List[str]) -> int: + # Safety first, check if the list is empty + # If it is, we can return 0 as there are no words to compare + # This prevents any index errors when trying to access elements in an empty list + # For examble, IndexError: list index out of range if we try to access strs[0] when strs is empty + if not strs: + return 0 + + min_len_word = len(strs[0]) + + while pos_word < len(strs) - 1: + if len(strs[pos_word + 1]) < min_len_word: + min_len_word = len(strs[pos_word + 1]) + pos_word += 1 + + return min_len_word + + def longest_common_prefix(self, strs: List[str]) -> str: + # Safety first, check if the list is empty + # If it is, we can return an empty string as there are no words to compare + # This prevents any index errors when trying to access elements in an empty list + # For example, IndexError: list index out of range if we try to access strs[0] when strs is empty + if not strs: + return "" + + first_word = strs[0] + + if len(strs) == 1: + return strs[0] + + prefix_cmn_long: List[str] = [] + + min_len_word = Solution.min_len_word(0, strs) + + # If the minimum length word is 0, it means at least one of the strings is empty + # For example, strs = ["flower", "", "flight"] + # In this case, the longest common prefix is also an empty string + if min_len_word == 0: + return "" + + pos_char = 0 + first_word = strs[0] + # Outer loop: We need to compare characters at the same position across all words + while pos_char < min_len_word: + pos_word = 0 + # Inner Loop: We need to compare the character at pos_char across all words + while pos_word < len(strs) - 1: + + current_word = strs[pos_word] + next_word = strs[pos_word + 1] + + # If mismatch at first char => "", else return what we built + if current_word[pos_char] != next_word[pos_char]: + return "".join(prefix_cmn_long) + pos_word += 1 + + # If we got here, char matches across all words for this position + prefix_cmn_long.append(first_word[pos_char]) + pos_char += 1 + + return "".join(prefix_cmn_long) + + +strs = ["flower","flow","flight"] +print(Solution().longest_common_prefix(strs)) \ No newline at end of file diff --git a/py/src/arrays/0014-longest-common-prefix/test_solution.py b/py/src/arrays/0014-longest-common-prefix/test_solution.py new file mode 100644 index 0000000..36e798e --- /dev/null +++ b/py/src/arrays/0014-longest-common-prefix/test_solution.py @@ -0,0 +1,37 @@ +import pytest +from solution import two_sum + + +def test_returns_indices_of_two_numbers_that_sum_to_target(): + nums = [2, 7, 11, 15] + target = 9 + + result = two_sum(nums, target) + + assert result == [0, 1] + + +def test_works_with_negative_numbers(): + nums = [-3, 4, 3, 90] + target = 0 + + result = two_sum(nums, target) + + assert result == [0, 2] + + +def test_does_not_reuse_the_same_element_twice(): + nums = [3, 3] + target = 6 + + result = two_sum(nums, target) + + assert result == [0, 1] + + +def test_raises_error_when_no_solution_exists(): + nums = [1, 2, 3] + target = 7 + + with pytest.raises(ValueError, match="No solution"): + two_sum(nums, target) From ef3b90a04e71bcfdb059657b72dbe6871777de89 Mon Sep 17 00:00:00 2001 From: Stefano Agbodan Date: Thu, 5 Feb 2026 22:53:35 +0100 Subject: [PATCH 2/3] feat: reset test_solution file --- .../test_solution.py | 37 ------------------- 1 file changed, 37 deletions(-) diff --git a/py/src/arrays/0014-longest-common-prefix/test_solution.py b/py/src/arrays/0014-longest-common-prefix/test_solution.py index 36e798e..e69de29 100644 --- a/py/src/arrays/0014-longest-common-prefix/test_solution.py +++ b/py/src/arrays/0014-longest-common-prefix/test_solution.py @@ -1,37 +0,0 @@ -import pytest -from solution import two_sum - - -def test_returns_indices_of_two_numbers_that_sum_to_target(): - nums = [2, 7, 11, 15] - target = 9 - - result = two_sum(nums, target) - - assert result == [0, 1] - - -def test_works_with_negative_numbers(): - nums = [-3, 4, 3, 90] - target = 0 - - result = two_sum(nums, target) - - assert result == [0, 2] - - -def test_does_not_reuse_the_same_element_twice(): - nums = [3, 3] - target = 6 - - result = two_sum(nums, target) - - assert result == [0, 1] - - -def test_raises_error_when_no_solution_exists(): - nums = [1, 2, 3] - target = 7 - - with pytest.raises(ValueError, match="No solution"): - two_sum(nums, target) From 26dd059722f3e6a6a84d8b0d2ebbf3570eeb3693 Mon Sep 17 00:00:00 2001 From: Stefano Agbodan Date: Thu, 5 Feb 2026 23:20:12 +0100 Subject: [PATCH 3/3] test(py): avoid pytest import collisions by using unique test module names --- py/src/arrays/0001-two-sum/{test_solution.py => test_two_sum.py} | 0 .../{test_solution.py => test_longest_common_prefix.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename py/src/arrays/0001-two-sum/{test_solution.py => test_two_sum.py} (100%) rename py/src/arrays/0014-longest-common-prefix/{test_solution.py => test_longest_common_prefix.py} (100%) diff --git a/py/src/arrays/0001-two-sum/test_solution.py b/py/src/arrays/0001-two-sum/test_two_sum.py similarity index 100% rename from py/src/arrays/0001-two-sum/test_solution.py rename to py/src/arrays/0001-two-sum/test_two_sum.py diff --git a/py/src/arrays/0014-longest-common-prefix/test_solution.py b/py/src/arrays/0014-longest-common-prefix/test_longest_common_prefix.py similarity index 100% rename from py/src/arrays/0014-longest-common-prefix/test_solution.py rename to py/src/arrays/0014-longest-common-prefix/test_longest_common_prefix.py