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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,4 @@ cython_debug/
marimo/_static/
marimo/_lsp/
__marimo__/
.vscode/launch.json
10 changes: 9 additions & 1 deletion p_3_longest_substring_without_repeating_characters/src/main.py
Original file line number Diff line number Diff line change
@@ -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

######################################
Expand All @@ -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}")


Expand Down
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
Empty file.
31 changes: 31 additions & 0 deletions p_4_median_of_two_sorted_arrays/src/main.py
Original file line number Diff line number Diff line change
@@ -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()

40 changes: 40 additions & 0 deletions p_4_median_of_two_sorted_arrays/src/solution.py
Original file line number Diff line number Diff line change
@@ -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")

50 changes: 50 additions & 0 deletions p_4_median_of_two_sorted_arrays/tests/test_solution.py
Original file line number Diff line number Diff line change
@@ -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