From 2ae6d52737e18808035ab6ecad9557e88b025047 Mon Sep 17 00:00:00 2001 From: Mi-K Date: Fri, 22 Aug 2025 19:03:01 +0200 Subject: [PATCH 1/3] Updating sys.path. --- .../src/main.py | 10 +++++++++- .../tests/test_solution.py | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/p_3_longest_substring_without_repeating_characters/src/main.py b/p_3_longest_substring_without_repeating_characters/src/main.py index ae599ec..f8df828 100644 --- a/p_3_longest_substring_without_repeating_characters/src/main.py +++ b/p_3_longest_substring_without_repeating_characters/src/main.py @@ -1,6 +1,14 @@ # https://github.com/badprog/badprog_leetcode_python + ###################################### +# import +import sys +import pathlib + +# Adding the parent directory of this file to the sys.path +sys.path.append(str(pathlib.Path(__file__).resolve().parents[1])) + from src.solution import Solution ###################################### @@ -9,7 +17,7 @@ def main(): print("Hello from Badprog, Python and Leetcode :D") s = "Héllo world" solution = Solution() - result =solution.lengthOfLongestSubstring(s) + result = solution.lengthOfLongestSubstring(s) print(f"result = {result}") diff --git a/p_3_longest_substring_without_repeating_characters/tests/test_solution.py b/p_3_longest_substring_without_repeating_characters/tests/test_solution.py index 4d9a990..3bcfa17 100644 --- a/p_3_longest_substring_without_repeating_characters/tests/test_solution.py +++ b/p_3_longest_substring_without_repeating_characters/tests/test_solution.py @@ -1,5 +1,13 @@ # https://github.com/badprog/badprog_leetcode_python +############################ +# import +import sys +import pathlib + +# Adding the parent directory of this file to the sys.path +sys.path.append(str(pathlib.Path(__file__).resolve().parents[1])) + from src.solution import Solution class TestP3LongestSubstringWithoutRepeatingCharacters(): From 2ee05e5e6b639856ff7ef1bb5191bf8ddacb9aca Mon Sep 17 00:00:00 2001 From: Mi-K Date: Fri, 22 Aug 2025 19:04:28 +0200 Subject: [PATCH 2/3] Adding Challenge 4. --- .../src/__init__.py | 0 p_4_median_of_two_sorted_arrays/src/main.py | 31 ++++++++++++ .../src/solution.py | 40 +++++++++++++++ .../tests/test_solution.py | 50 +++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 p_4_median_of_two_sorted_arrays/src/__init__.py create mode 100644 p_4_median_of_two_sorted_arrays/src/main.py create mode 100644 p_4_median_of_two_sorted_arrays/src/solution.py create mode 100644 p_4_median_of_two_sorted_arrays/tests/test_solution.py diff --git a/p_4_median_of_two_sorted_arrays/src/__init__.py b/p_4_median_of_two_sorted_arrays/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/p_4_median_of_two_sorted_arrays/src/main.py b/p_4_median_of_two_sorted_arrays/src/main.py new file mode 100644 index 0000000..a24e3a4 --- /dev/null +++ b/p_4_median_of_two_sorted_arrays/src/main.py @@ -0,0 +1,31 @@ +# https://github.com/badprog/badprog_leetcode_python + +################ +# import +import sys +import pathlib + +# Adding the parent directory of this file to the sys.path +sys.path.append(str(pathlib.Path(__file__).resolve().parents[1])) + +from src.solution import Solution +from typing import List + +################ +# +def main(): + print("Hello from Badprog, Leetcode challenge 4 and Rust :D") + solution = Solution() + # list1: List[int] = [1,2] + list1: List[int] = [1,2,6,0,2,1,4,9] + list2: List[int] = [4,5,6,8,9] + list1.sort() + list2.sort() + result = solution.findMedianSortedArrays(list1, list2) + print("result = {}", result) + +################ +# +if __name__ == "__main__": + main() + diff --git a/p_4_median_of_two_sorted_arrays/src/solution.py b/p_4_median_of_two_sorted_arrays/src/solution.py new file mode 100644 index 0000000..fddc245 --- /dev/null +++ b/p_4_median_of_two_sorted_arrays/src/solution.py @@ -0,0 +1,40 @@ +# https://github.com/badprog/badprog_leetcode_python + +from typing import List, Tuple + +############################ +# +class Solution: + ############################ + # + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + # nums1 must have the lesser or equal length compared to nums2 + if len(nums1) > len(nums2): + nums1, nums2 = nums2, nums1 # swap + m, n = len(nums1), len(nums2) # define m and n + left, right = 0, m + + # Loop + while left <= right: + i = (left + right) // 2 # i for nums1 (left1 and right1) + j = (m + n + 1) // 2 - i # j for nums2 (left2 and right2) + + # check + left1 = nums1[i-1] if i > 0 else float('-inf') + right1 = nums1[i] if i < m else float('inf') + left2 = nums2[j-1] if j > 0 else float('-inf') + right2 = nums2[j] if j < n else float('inf') + + # final check + if left1 <= right2 and left2 <= right1: + # we can get the median + if (m + n) % 2 == 1: + return max(left1, left2) # odd + return (max(left1, left2) + min(right1, right2)) / 2 # even + elif left1 > right2: + right = i - 1 + else: + left = i + 1 + + raise ValueError("Lists not sorted or invalid") + diff --git a/p_4_median_of_two_sorted_arrays/tests/test_solution.py b/p_4_median_of_two_sorted_arrays/tests/test_solution.py new file mode 100644 index 0000000..72ca764 --- /dev/null +++ b/p_4_median_of_two_sorted_arrays/tests/test_solution.py @@ -0,0 +1,50 @@ +# https://github.com/badprog/badprog_leetcode_python + +############################ +# import +import sys +import pathlib + +# Adding the parent directory of this file to the sys.path +sys.path.append(str(pathlib.Path(__file__).resolve().parents[1])) + +import pytest +from src.solution import Solution + +@pytest.fixture +def solution(): + return Solution() + +def test_two_sorted_arrays_equal_length(solution): + assert solution.findMedianSortedArrays([1, 3], [2, 4]) == 2.5 + +def test_two_sorted_arrays_odd_total_length(solution): + assert solution.findMedianSortedArrays([1, 3], [2]) == 2.0 + +def test_one_empty_array(solution): + assert solution.findMedianSortedArrays([], [1]) == 1.0 + +def test_single_element_arrays(solution): + assert solution.findMedianSortedArrays([1], [2]) == 1.5 + +def test_large_arrays(solution): + assert solution.findMedianSortedArrays([1, 2, 3, 4], [5, 6, 7, 8]) == 4.5 + +def test_one_element_and_large_array(solution): + assert solution.findMedianSortedArrays([1], [2, 3, 4]) == 2.5 + +def test_arrays_with_negative_numbers(solution): + assert solution.findMedianSortedArrays([-5, -1], [2, 4]) == 0.5 + +def test_duplicate_elements(solution): + assert solution.findMedianSortedArrays([1, 1], [1, 1]) == 1.0 + +def test_large_difference_in_sizes(solution): + assert solution.findMedianSortedArrays([1, 2], [3, 4, 5, 6, 7]) == 4.0 + +def test_maximum_constraints(solution): + nums1 = [1] * 1000 + nums2 = [2] * 1000 + assert solution.findMedianSortedArrays(nums1, nums2) == 1.5 + + \ No newline at end of file From ed63d13060d766ddfb7c864087391555e34c0469 Mon Sep 17 00:00:00 2001 From: Mi-K Date: Fri, 22 Aug 2025 19:04:34 +0200 Subject: [PATCH 3/3] Udpate. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d5cf916..1142c99 100644 --- a/.gitignore +++ b/.gitignore @@ -206,3 +206,4 @@ cython_debug/ marimo/_static/ marimo/_lsp/ __marimo__/ +.vscode/launch.json