From c65b1a64d18297b13d95a0f0ec5f51a46508a45c Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sun, 2 Aug 2026 18:55:37 -0700 Subject: [PATCH 1/2] Add code coverage support --- .github/workflows/Python.yml | 4 +- CLAUDE.md | 2 + justfile | 6 +- pyproject.toml | 8 ++- src/algorithm_theory/binary_search.py | 4 +- src/algorithm_theory/bubble_sort.py | 16 +----- src/arrays/find_smallest_positive_integer.py | 13 ++--- src/arrays/make_array_consecutive.py | 5 +- src/arrays/remove_duplicates.py | 7 +-- src/arrays/two_sum_ii.py | 1 + src/arrays/valid_mountain_array.py | 9 ++- src/data_structures/binary_tree_node.py | 4 +- src/singly_linked_lists/find_middle_node.py | 6 +- src/stacks/min_stack.py | 3 +- .../longest_substring_without_duplicates.py | 2 - src/strings/palindrome.py | 7 +-- src/strings/palindrome_permutation.py | 2 +- src/strings/string_to_integer.py | 4 -- src/trie/replace_words.py | 16 +++--- tests/algorithm_theory/test_binary_search.py | 10 ++++ tests/algorithm_theory/test_linear_search.py | 1 + tests/arrays/test_check_double.py | 1 + .../test_find_smallest_positive_integer.py | 1 + tests/arrays/test_make_array_consecutive.py | 6 +- tests/arrays/test_remove_duplicates.py | 6 ++ tests/arrays/test_single_number.py | 2 + tests/arrays/test_two_sum.py | 1 + tests/arrays/test_two_sum_ii.py | 2 + tests/arrays/test_valid_mountain_array.py | 1 + .../test_search_insert_position.py | 2 + tests/data_structures/test_stack.py | 1 + tests/stacks/test_min_stack.py | 1 + tests/stacks/test_polish_notation.py | 1 + tests/stacks/test_valid_parentheses.py | 1 + tests/strings/test_anagram.py | 2 + tests/strings/test_longest_common_prefix.py | 3 + tests/strings/test_palindrome.py | 21 +++---- ...tion.py => test_palindrome_permutation.py} | 1 + tests/strings/test_string_to_integer.py | 1 + tests/strings/test_string_to_integer_ii.py | 1 + tests/strings/test_valid_word_abbreviation.py | 1 + uv.lock | 55 +++++++++++++++++++ 42 files changed, 155 insertions(+), 86 deletions(-) rename tests/strings/{palindrome_permutation.py => test_palindrome_permutation.py} (88%) diff --git a/.github/workflows/Python.yml b/.github/workflows/Python.yml index ea3c59c..2f33569 100644 --- a/.github/workflows/Python.yml +++ b/.github/workflows/Python.yml @@ -31,6 +31,6 @@ jobs: - name: Lint run: | just lint - - name: Unit tests + - name: Unit tests + Code coverage check run: | - just test \ No newline at end of file + just test-coverage-percentage \ No newline at end of file diff --git a/CLAUDE.md b/CLAUDE.md index 88db918..82a87d1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -10,6 +10,8 @@ This project uses `uv` for dependency management and `just` as the command runne just sync # install/sync dependencies (uv sync --all-groups) just lint # uv run ruff check . --fix && uv run ruff format . just test # uv run pytest +just test-coverage # uv run pytest --cov=src --cov-report=term-missing +just test-coverage-percentage # uv run pytest --cov=src --cov-report=term --cov-fail-under=98 uv run pytest tests/algorithm_theory/test_binary_search.py # run one test file uv run pytest tests/algorithm_theory/test_binary_search.py::test_binary_search_iterative # run one test uv add {package} # add a dependency diff --git a/justfile b/justfile index bef3ba6..f826c7e 100644 --- a/justfile +++ b/justfile @@ -4,4 +4,8 @@ lint: sync: uv sync --all-groups test: - uv run pytest \ No newline at end of file + uv run pytest +test-coverage: + uv run pytest --cov=src --cov-report=term-missing +test-coverage-percentage: + uv run pytest --cov=src --cov-report=term --cov-fail-under=98 \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index b3c2243..b4e2e58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,13 @@ requires-python = ">=3.14" dependencies = [] [dependency-groups] -dev = ["pytest", "ruff>=0.16.0"] +dev = [ + "pytest", + "pytest-cov>=7.1.0", + "ruff>=0.16.0", +] + +[tool.coverage] [tool.pytest.ini_options] pythonpath = ["."] diff --git a/src/algorithm_theory/binary_search.py b/src/algorithm_theory/binary_search.py index b6cfd6f..9359849 100644 --- a/src/algorithm_theory/binary_search.py +++ b/src/algorithm_theory/binary_search.py @@ -13,7 +13,7 @@ def binary_search_iterative(array: list[int], target: int) -> int | None: right = len(array) - 1 while left <= right: - middle = int((left + right) / 2) + middle = (left + right) // 2 if array[middle] == target: return middle @@ -27,7 +27,7 @@ def binary_search_iterative(array: list[int], target: int) -> int | None: def binary_search_recursive(array: list[int], left: int, right: int, target: int) -> int | None: """Recurse into the left or right sub-array each call until target is found or the bounds cross.""" if right >= left: - mid = int((left + right) / 2) + mid = (left + right) // 2 # If element is present at the middle if array[mid] == target: diff --git a/src/algorithm_theory/bubble_sort.py b/src/algorithm_theory/bubble_sort.py index 201bd0b..02a11e0 100644 --- a/src/algorithm_theory/bubble_sort.py +++ b/src/algorithm_theory/bubble_sort.py @@ -6,25 +6,13 @@ def bubble_sort(array: list[int]) -> list[int]: Example: `[2, 1, 3] -> [1, 2, 3]` """ - length = len(array) - - # Return if nothing to sort - if length <= 1: - return array - # Reduce the upper limit with each iteration, since the correct value has 'bubbled' to the top - for i in reversed(range(0, length)): - swap = False + for i in reversed(range(0, len(array))): # Compare value and switch up to the upper limit 'i' for j in range(0, i): next_val = j + 1 - if next_val < length and array[j] > array[next_val]: + if next_val < len(array) and array[j] > array[next_val]: temp = array[j] array[j] = array[next_val] array[next_val] = temp - swap = True - - # Return if no swaps take place - if not swap: - return array return array diff --git a/src/arrays/find_smallest_positive_integer.py b/src/arrays/find_smallest_positive_integer.py index 244d180..7f0c114 100644 --- a/src/arrays/find_smallest_positive_integer.py +++ b/src/arrays/find_smallest_positive_integer.py @@ -7,20 +7,15 @@ def find_smallest_positive_integer(arr: list[int]) -> int: Single pass tracking the smallest positive value seen so far. """ - length = len(arr) + smallest = -1 - if length == 0: - return 0 - - smallest = None - - for i in range(length): + for i in range(len(arr)): if arr[i] > 0: - if smallest is None: + if smallest == -1: smallest = arr[i] elif arr[i] < smallest: smallest = arr[i] - if smallest is None: + if smallest == -1: return 0 return smallest diff --git a/src/arrays/make_array_consecutive.py b/src/arrays/make_array_consecutive.py index 17905dc..d6696d5 100644 --- a/src/arrays/make_array_consecutive.py +++ b/src/arrays/make_array_consecutive.py @@ -7,15 +7,14 @@ def make_array_consecutive(values: list[int]) -> int: Sort the array, then sum the gaps between consecutive sorted values. """ - length = len(values) numbers_needed = 0 - if length <= 1: + if len(values) <= 1: return numbers_needed sorted_numbers = sorted(values) - for i in range(1, length): + for i in range(1, len(values)): numbers_needed += (sorted_numbers[i] - sorted_numbers[i - 1]) - 1 return numbers_needed diff --git a/src/arrays/remove_duplicates.py b/src/arrays/remove_duplicates.py index ca5aa2e..d0f1992 100644 --- a/src/arrays/remove_duplicates.py +++ b/src/arrays/remove_duplicates.py @@ -9,13 +9,12 @@ def remove_duplicates(nums: list[int]) -> int: Two-pointer in-place compaction over an already-sorted array. """ - length = len(nums) - if length <= 1: - return length + if len(nums) <= 1: + return len(nums) last_index = 0 - for index in range(1, length): + for index in range(1, len(nums)): if nums[index] == nums[last_index]: continue last_index += 1 diff --git a/src/arrays/two_sum_ii.py b/src/arrays/two_sum_ii.py index 479d07c..4736d4f 100644 --- a/src/arrays/two_sum_ii.py +++ b/src/arrays/two_sum_ii.py @@ -21,3 +21,4 @@ def two_sum_ii(numbers: list[int], target: int) -> list[int]: index_right -= 1 else: return [index_left + 1, index_right + 1] + return [] diff --git a/src/arrays/valid_mountain_array.py b/src/arrays/valid_mountain_array.py index 13d60d4..1f41dd0 100644 --- a/src/arrays/valid_mountain_array.py +++ b/src/arrays/valid_mountain_array.py @@ -9,25 +9,24 @@ def valid_mountain_array(arr: list[int]) -> bool: Walk up the increasing run, then confirm a strictly decreasing run finishes the array. """ - length = len(arr) - if length <= 2: + if len(arr) <= 2: return False index = 1 # Check increasing - while index < length: + while index < len(arr): if arr[index - 1] < arr[index]: index += 1 else: break # Edge case: only increasing, only decreasing - if index in [length, 1]: + if index == len(arr) or index == 1: return False # Check decreasing - while index < length: + while index < len(arr): if arr[index - 1] > arr[index]: index += 1 else: diff --git a/src/data_structures/binary_tree_node.py b/src/data_structures/binary_tree_node.py index 797521f..975698c 100644 --- a/src/data_structures/binary_tree_node.py +++ b/src/data_structures/binary_tree_node.py @@ -4,7 +4,9 @@ class BinaryTreeNode[T]: A node in a binary tree, holding data plus references to its left and right child nodes. """ - def __init__(self, data: T, left: BinaryTreeNode[T] | None = None, right: BinaryTreeNode[T] | None = None): + def __init__( + self, data: T, left: BinaryTreeNode[T] | None = None, right: BinaryTreeNode[T] | None = None + ): self.data = data self.left = left self.right = right diff --git a/src/singly_linked_lists/find_middle_node.py b/src/singly_linked_lists/find_middle_node.py index 611688a..de55329 100644 --- a/src/singly_linked_lists/find_middle_node.py +++ b/src/singly_linked_lists/find_middle_node.py @@ -1,7 +1,7 @@ from src.data_structures.singly_linked_list_node import SinglyLinkedListNode -def find_middle_node(head: SinglyLinkedListNode | None) -> SinglyLinkedListNode | None: +def find_middle_node(head: SinglyLinkedListNode) -> SinglyLinkedListNode | None: """Find Middle Node. Find the node that comes before the middle node of a singly linked list. @@ -10,12 +10,10 @@ def find_middle_node(head: SinglyLinkedListNode | None) -> SinglyLinkedListNode Advances a slow and a fast pointer (2x speed) until the fast pointer runs out of room. """ - if not head: - return None slow = head fast = head - while fast.next and fast.next.next: + while slow and fast.next and fast.next.next: slow = slow.next fast = fast.next.next diff --git a/src/stacks/min_stack.py b/src/stacks/min_stack.py index ca24207..e461b60 100644 --- a/src/stacks/min_stack.py +++ b/src/stacks/min_stack.py @@ -7,8 +7,7 @@ class MinStack: """ def __init__(self): - # Tuple (value, current_min) - self.stack = [] + self.stack: list[tuple[int, int]] = [] def push(self, value: int) -> None: """Push value onto the stack, storing the minimum seen so far alongside it.""" diff --git a/src/strings/longest_substring_without_duplicates.py b/src/strings/longest_substring_without_duplicates.py index fb0658c..c849ea1 100644 --- a/src/strings/longest_substring_without_duplicates.py +++ b/src/strings/longest_substring_without_duplicates.py @@ -8,8 +8,6 @@ def longest_substring_without_duplicates(string: str) -> int: Slide a window with two pointers, expanding right while characters are unique and shrinking left otherwise. """ length = len(string) - if length <= 1: - return length left_index = 0 right_index = 0 longest = 0 diff --git a/src/strings/palindrome.py b/src/strings/palindrome.py index 7940375..aa553f3 100644 --- a/src/strings/palindrome.py +++ b/src/strings/palindrome.py @@ -13,13 +13,8 @@ def valid_palindrome_naive(string: str) -> bool: def valid_palindrome(string: str) -> bool: """Walk inward from both ends, skipping non-alphanumeric characters, comparing lowercased characters.""" - length = len(string) - - if length < 2: - return True - start = 0 - end = length - 1 + end = len(string) - 1 normalized_string = string.lower() diff --git a/src/strings/palindrome_permutation.py b/src/strings/palindrome_permutation.py index 85135c3..fd276f3 100644 --- a/src/strings/palindrome_permutation.py +++ b/src/strings/palindrome_permutation.py @@ -21,7 +21,7 @@ def palindrome_permutation(palindrome: str) -> bool: return False else: ones_count = Counter(counter.values()).get(1) - if ones_count > 1: + if ones_count and ones_count > 1: return False return True diff --git a/src/strings/string_to_integer.py b/src/strings/string_to_integer.py index 4eeec15..1465e9b 100644 --- a/src/strings/string_to_integer.py +++ b/src/strings/string_to_integer.py @@ -29,10 +29,6 @@ def string_to_integer(string: str) -> int: start_index = index break - # Handle empty string (no numbers) - if start_index == -1: - return 0 - end_index = start_index for index in range(start_index, length): diff --git a/src/trie/replace_words.py b/src/trie/replace_words.py index b118279..b2798a7 100644 --- a/src/trie/replace_words.py +++ b/src/trie/replace_words.py @@ -15,7 +15,7 @@ def replace_words(dictionary: list[str], sentence: str) -> str: `"the cat was rat by the bat"` """ words = sentence.split(" ") - updated_sentence: [str] = [] + updated_sentence: list[str] = [] prefix_trie = TrieNode() for word in dictionary: @@ -31,21 +31,21 @@ class TrieNode: """A prefix trie node used to store dictionary roots and match sentence words against them.""" def __init__(self): - self.nodes: dict[str:TrieNode] = dict() + self.nodes: dict[str, TrieNode] = dict() def insert(self, word: str) -> None: """Insert word into the trie, stopping early if a shorter existing root already covers it.""" current_node = self for index in range(1, len(word) + 1): - if word[:index] in current_node.nodes: - next_node = current_node.nodes.get(word[:index]) + if current_node and word[:index] in current_node.nodes: + next_node = current_node.nodes[word[:index]] if len(next_node.nodes) == 0: return if index == len(word): current_node.nodes[word[:index]] = TrieNode() return current_node = next_node - else: + elif current_node: current_node.nodes.setdefault(word[:index], TrieNode()) current_node = current_node.nodes.get(word[:index]) @@ -53,10 +53,8 @@ def replace_word(self, word: str) -> str: """Walk the trie along word's prefixes and return the shortest matching root, or word unchanged.""" current_node = self for index in range(1, len(word) + 1): - if word[:index] in current_node.nodes: - current_node = current_node.nodes.get(word[:index]) + if current_node and word[:index] in current_node.nodes: + current_node = current_node.nodes[word[:index]] if len(current_node.nodes) == 0: return word[:index] - else: - return word return word diff --git a/tests/algorithm_theory/test_binary_search.py b/tests/algorithm_theory/test_binary_search.py index 589f473..7d3da53 100644 --- a/tests/algorithm_theory/test_binary_search.py +++ b/tests/algorithm_theory/test_binary_search.py @@ -3,9 +3,19 @@ def test_binary_search_iterative(): arr = [2, 5, 36, 40, 58] + assert binary_search_iterative(arr, 2) == 0 + assert binary_search_iterative(arr, 5) == 1 + assert binary_search_iterative(arr, 36) == 2 assert binary_search_iterative(arr, 40) == 3 + assert binary_search_iterative(arr, 58) == 4 + assert binary_search_iterative(arr, 1) is None def test_binary_search_recursive(): arr = [2, 5, 36, 40, 58] + assert binary_search_recursive(arr, 0, len(arr) - 1, 2) == 0 assert binary_search_recursive(arr, 0, len(arr) - 1, 5) == 1 + assert binary_search_recursive(arr, 0, len(arr) - 1, 36) == 2 + assert binary_search_recursive(arr, 0, len(arr) - 1, 40) == 3 + assert binary_search_recursive(arr, 0, len(arr) - 1, 58) == 4 + assert binary_search_recursive(arr, 0, len(arr) - 1, 1) is None diff --git a/tests/algorithm_theory/test_linear_search.py b/tests/algorithm_theory/test_linear_search.py index fd43dbc..e5a68b1 100644 --- a/tests/algorithm_theory/test_linear_search.py +++ b/tests/algorithm_theory/test_linear_search.py @@ -4,3 +4,4 @@ def test_linear_search(): arr = [1, 2, 3, 4, 5] assert linear_search(arr, 0, 4, 4) == 3 + assert linear_search(arr, 0, 4, -1) is None diff --git a/tests/arrays/test_check_double.py b/tests/arrays/test_check_double.py index 52f6017..5fa74f9 100644 --- a/tests/arrays/test_check_double.py +++ b/tests/arrays/test_check_double.py @@ -3,5 +3,6 @@ def test_check_double(): assert check_double([10, 3, 5, 2, 1]) + assert check_double([1, 3, 5, 2, 10]) assert check_double([0, 0]) assert not check_double([1, 1]) diff --git a/tests/arrays/test_find_smallest_positive_integer.py b/tests/arrays/test_find_smallest_positive_integer.py index f8f8363..6d38635 100644 --- a/tests/arrays/test_find_smallest_positive_integer.py +++ b/tests/arrays/test_find_smallest_positive_integer.py @@ -2,6 +2,7 @@ def test_find_smallest_positive_integer(): + assert find_smallest_positive_integer([]) == 0 assert find_smallest_positive_integer([1, 2, 3]) == 1 assert find_smallest_positive_integer([-1, -2, -3]) == 0 assert find_smallest_positive_integer([1, 1, 1]) == 1 diff --git a/tests/arrays/test_make_array_consecutive.py b/tests/arrays/test_make_array_consecutive.py index c750046..ac1a24e 100644 --- a/tests/arrays/test_make_array_consecutive.py +++ b/tests/arrays/test_make_array_consecutive.py @@ -2,5 +2,7 @@ def test_make_array_consecutive(): - arr = [6, 2, 3, 8] - assert make_array_consecutive(arr) == 3 + assert make_array_consecutive([6, 2, 3, 8]) == 3 + assert make_array_consecutive([]) == 0 + assert make_array_consecutive([1]) == 0 + assert make_array_consecutive([1, 2]) == 0 diff --git a/tests/arrays/test_remove_duplicates.py b/tests/arrays/test_remove_duplicates.py index 2a73a92..0775de6 100644 --- a/tests/arrays/test_remove_duplicates.py +++ b/tests/arrays/test_remove_duplicates.py @@ -2,6 +2,12 @@ def test_remove_duplicates(): + nums = [] + assert remove_duplicates(nums) == 0 + + nums = [1] + assert remove_duplicates(nums) == 1 + nums = [1, 1, 2] assert remove_duplicates(nums) == 2 assert nums == [1, 2, 2] diff --git a/tests/arrays/test_single_number.py b/tests/arrays/test_single_number.py index c484b0e..5826803 100644 --- a/tests/arrays/test_single_number.py +++ b/tests/arrays/test_single_number.py @@ -2,10 +2,12 @@ def test_single_number(): + assert single_number([]) == 0 assert single_number([1, 2, 2, 3, 3]) == 1 assert single_number([1, 1]) == 0 def test_single_number_bitwise(): + assert single_number_bitwise([]) == 0 assert single_number_bitwise([1, 2, 2, 3, 3]) == 1 assert single_number_bitwise([1, 1]) == 0 diff --git a/tests/arrays/test_two_sum.py b/tests/arrays/test_two_sum.py index e2a86d0..7e6f860 100644 --- a/tests/arrays/test_two_sum.py +++ b/tests/arrays/test_two_sum.py @@ -2,5 +2,6 @@ def test_two_sum(): + assert two_sum([2, 7, 11, 15], 1) == [-1, -1] assert two_sum([2, 15, 7, 32], 9) == [0, 2] assert two_sum([-1, 0], -1) == [0, 1] diff --git a/tests/arrays/test_two_sum_ii.py b/tests/arrays/test_two_sum_ii.py index 76934d0..59da98a 100644 --- a/tests/arrays/test_two_sum_ii.py +++ b/tests/arrays/test_two_sum_ii.py @@ -2,5 +2,7 @@ def test_two_sum_ii(): + assert two_sum_ii([2, 7, 11, 15], 1) == [] assert two_sum_ii([2, 7, 11, 15], 9) == [1, 2] + assert two_sum_ii([2, 7, 11, 15], 26) == [3, 4] assert two_sum_ii([-1, 0], -1) == [1, 2] diff --git a/tests/arrays/test_valid_mountain_array.py b/tests/arrays/test_valid_mountain_array.py index 1c1b125..6b8b0a4 100644 --- a/tests/arrays/test_valid_mountain_array.py +++ b/tests/arrays/test_valid_mountain_array.py @@ -8,3 +8,4 @@ def test_valid_mountain_array(): assert not valid_mountain_array([1, 2, 3, 4]) assert not valid_mountain_array([3, 2, 1, 0]) assert not valid_mountain_array([1]) + assert not valid_mountain_array([1, 2, 3, 4, 3, 5]) diff --git a/tests/binary_search/test_search_insert_position.py b/tests/binary_search/test_search_insert_position.py index 568cf9f..2347c58 100644 --- a/tests/binary_search/test_search_insert_position.py +++ b/tests/binary_search/test_search_insert_position.py @@ -3,3 +3,5 @@ def test_search_insert(): assert search_insert([1, 3, 5, 6], 5) == 2 + assert search_insert([1, 3, 5, 6], 1) == 0 + assert search_insert([], 1) == 0 diff --git a/tests/data_structures/test_stack.py b/tests/data_structures/test_stack.py index ef7166c..a74b675 100644 --- a/tests/data_structures/test_stack.py +++ b/tests/data_structures/test_stack.py @@ -8,3 +8,4 @@ def test_stack(): stack.push(1) assert len(stack) == 1 assert stack.pop() == 1 + assert len(stack) == 0 diff --git a/tests/stacks/test_min_stack.py b/tests/stacks/test_min_stack.py index 0c58af3..cb4b057 100644 --- a/tests/stacks/test_min_stack.py +++ b/tests/stacks/test_min_stack.py @@ -9,6 +9,7 @@ def test_min_stack(): stack.push(-3) assert stack.get_min() == -3 + assert stack.top() == -3 stack.pop() stack.pop() diff --git a/tests/stacks/test_polish_notation.py b/tests/stacks/test_polish_notation.py index b02be3f..6078578 100644 --- a/tests/stacks/test_polish_notation.py +++ b/tests/stacks/test_polish_notation.py @@ -2,6 +2,7 @@ def test_evaluate_reverse_polish_notation(): + assert evaluate_rpn(["2", "1", "-", "3", "*"]) == 3 assert evaluate_rpn(["2", "1", "+", "3", "*"]) == 9 assert evaluate_rpn(["4", "13", "5", "/", "+"]) == 6 assert evaluate_rpn(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]) == 22 diff --git a/tests/stacks/test_valid_parentheses.py b/tests/stacks/test_valid_parentheses.py index 09c2816..a4a2b47 100644 --- a/tests/stacks/test_valid_parentheses.py +++ b/tests/stacks/test_valid_parentheses.py @@ -5,6 +5,7 @@ def test_valid_parentheses(): assert valid_parentheses("()") assert valid_parentheses("(){}[]") assert valid_parentheses("{[]}") + assert not valid_parentheses("(") assert not valid_parentheses("(]") assert not valid_parentheses("((") assert not valid_parentheses("([)]") diff --git a/tests/strings/test_anagram.py b/tests/strings/test_anagram.py index b92781a..40324b7 100644 --- a/tests/strings/test_anagram.py +++ b/tests/strings/test_anagram.py @@ -5,3 +5,5 @@ def test_valid_anagram(): assert valid_anagram("car", "rac") assert not valid_anagram("test", "tests") assert not valid_anagram("test", "tezt") + assert not valid_anagram("test", "tsst") + diff --git a/tests/strings/test_longest_common_prefix.py b/tests/strings/test_longest_common_prefix.py index 52a32f5..a95f06d 100644 --- a/tests/strings/test_longest_common_prefix.py +++ b/tests/strings/test_longest_common_prefix.py @@ -5,6 +5,8 @@ def test_longest_common_prefix_horizontal(): + assert longest_common_prefix_horizontal([]) == "" + assert longest_common_prefix_horizontal([""]) == "" assert longest_common_prefix_horizontal(["acv", "def", "sgh"]) == "" assert longest_common_prefix_horizontal(["abab", ""]) == "" assert longest_common_prefix_horizontal(["ababaabbab", "abab", "abbaba"]) == "ab" @@ -14,6 +16,7 @@ def test_longest_common_prefix_horizontal(): def test_longest_common_prefix_vertical(): + assert longest_common_prefix_vertical([]) == "" assert longest_common_prefix_vertical(["acv", "def", "sgh"]) == "" assert longest_common_prefix_vertical(["abab", ""]) == "" assert longest_common_prefix_vertical(["ababaabbab", "abab", "abbaba"]) == "ab" diff --git a/tests/strings/test_palindrome.py b/tests/strings/test_palindrome.py index 4dca977..25930da 100644 --- a/tests/strings/test_palindrome.py +++ b/tests/strings/test_palindrome.py @@ -2,20 +2,13 @@ def test_valid_palindrome_naive(): - val1 = "aba" - val2 = "aacbaa" - val3 = "a:bba" - - assert valid_palindrome_naive(val1) - assert not valid_palindrome_naive(val2) - assert not valid_palindrome_naive(val3) + assert valid_palindrome_naive("aba") + assert not valid_palindrome_naive("aacbaa") + assert not valid_palindrome_naive("a:bba") def test_valid_palindrome(): - val1 = "aba" - val2 = "aacbaa" - val3 = "a:bba" - - assert valid_palindrome(val1) - assert not valid_palindrome(val2) - assert valid_palindrome(val3) + assert valid_palindrome("aba ") + assert valid_palindrome("aba") + assert not valid_palindrome("aacbaa") + assert valid_palindrome("a:bba") diff --git a/tests/strings/palindrome_permutation.py b/tests/strings/test_palindrome_permutation.py similarity index 88% rename from tests/strings/palindrome_permutation.py rename to tests/strings/test_palindrome_permutation.py index 9bb7dd3..d37d91e 100644 --- a/tests/strings/palindrome_permutation.py +++ b/tests/strings/test_palindrome_permutation.py @@ -8,3 +8,4 @@ def test_palindrome_permutation(): assert palindrome_permutation("") assert not palindrome_permutation("test") assert not palindrome_permutation("aabc") + assert not palindrome_permutation("abc") diff --git a/tests/strings/test_string_to_integer.py b/tests/strings/test_string_to_integer.py index 7bddc90..2dd7e03 100644 --- a/tests/strings/test_string_to_integer.py +++ b/tests/strings/test_string_to_integer.py @@ -2,6 +2,7 @@ def test_string_to_integer(): + assert string_to_integer("") == 0 assert string_to_integer("34") == 34 assert string_to_integer(" 100abc") == 100 assert string_to_integer(" -12vdsr") == -12 diff --git a/tests/strings/test_string_to_integer_ii.py b/tests/strings/test_string_to_integer_ii.py index fed82c4..ca322a8 100644 --- a/tests/strings/test_string_to_integer_ii.py +++ b/tests/strings/test_string_to_integer_ii.py @@ -2,6 +2,7 @@ def test_string_to_integer_ii(): + assert string_to_integer_ii("") == 0 assert string_to_integer_ii("34") == 34 assert string_to_integer_ii("100") == 100 assert string_to_integer_ii("-12") == -12 diff --git a/tests/strings/test_valid_word_abbreviation.py b/tests/strings/test_valid_word_abbreviation.py index 34481df..d4433f3 100644 --- a/tests/strings/test_valid_word_abbreviation.py +++ b/tests/strings/test_valid_word_abbreviation.py @@ -5,5 +5,6 @@ def test_valid_word_abbreviation(): assert not valid_word_abbreviation("apple", "a2e") assert valid_word_abbreviation("internationalization", "i12iz4n") assert valid_word_abbreviation("internationalization", "i18n") + assert valid_word_abbreviation("acdeaaa", "a2e3") assert not valid_word_abbreviation("a", "01") assert not valid_word_abbreviation("oil", "01il") diff --git a/uv.lock b/uv.lock index b984de0..50d4369 100644 --- a/uv.lock +++ b/uv.lock @@ -11,6 +11,45 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" }, ] +[[package]] +name = "coverage" +version = "7.15.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f4/45/78dbf9604ee5b3db24efbf26bed1cb58862fb40480cba821963c69348751/coverage-7.15.3.tar.gz", hash = "sha256:ae7ea5a4614acf399ef0483c4cb34f8f8f01df848d8fcbe7d3ce0865733f1c4d", size = 935592, upload-time = "2026-08-02T18:50:17.006Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/35/6f/8c2dc014357618b3226c90f731b8282766c3685786f422558991dc49fbf2/coverage-7.15.3-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:1e3bb08ad574bd9fb6a991f645728f70d333c1c1958dd5fcde65e24cb862813d", size = 222571, upload-time = "2026-08-02T18:49:11.242Z" }, + { url = "https://files.pythonhosted.org/packages/07/50/d867c7ceae9d56b7e74ee61ea834f1aa4f9a1e1c7f0ce39393ba573b1c12/coverage-7.15.3-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:9e5860eaff02a0b7f1b73304bdf846596ee62ab3a78d25c68044ebf684cb1fef", size = 222902, upload-time = "2026-08-02T18:49:13.448Z" }, + { url = "https://files.pythonhosted.org/packages/62/77/4f6dfc490c5f2bcacb2d296d9aa4d1e128c43b48e94ad313fec7f49f09ad/coverage-7.15.3-cp314-cp314-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:60874e5bd67f0b1bdbe42ab42c7bafa66a6fb8de88721af6df3f7a02713960cd", size = 253947, upload-time = "2026-08-02T18:49:15.304Z" }, + { url = "https://files.pythonhosted.org/packages/16/8a/6777f192af264165103e2a3d3768dbadb9894a0a2359a16877141d9ae8f5/coverage-7.15.3-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:f9147be876e9d83765e0b82176674dc248a6b9283e25e01e7462611b97e9b731", size = 256452, upload-time = "2026-08-02T18:49:17.801Z" }, + { url = "https://files.pythonhosted.org/packages/7d/7b/3d7ac46a0234bc684f41ee42be95e29b2b6525695adb04083609d5ac2149/coverage-7.15.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:61a01f8c3804760fcc5a3d31c4f3cab792d660d44e17bf7adeaf0ea51e07821e", size = 257798, upload-time = "2026-08-02T18:49:19.878Z" }, + { url = "https://files.pythonhosted.org/packages/ff/1e/c6ee59c29afcb5fdb35f936381340d1a06429a07c48f20e809646647acbe/coverage-7.15.3-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:95bf3e7f26f792e25eb185f85a5a659d48479265176dcfe22b6f334fd0081b5c", size = 260112, upload-time = "2026-08-02T18:49:21.858Z" }, + { url = "https://files.pythonhosted.org/packages/c1/e1/e8ea39a46e89e3a143312ee5f80336e992e3ae8fe44bf9c76b83fefeed42/coverage-7.15.3-cp314-cp314-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:44c41eff9e413fed8740eca75d5438ebeb9d3e45e7cd37c67329213e7a72c764", size = 253944, upload-time = "2026-08-02T18:49:23.926Z" }, + { url = "https://files.pythonhosted.org/packages/95/67/31ab5f6a37fd887d1386f81f0da9306851ad2264e9baaa9c7f606e0b3e17/coverage-7.15.3-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:54146bafb61f3ba9895b43af0dd17eba01561d586d44ce84ea221b0cbbee5a9e", size = 255805, upload-time = "2026-08-02T18:49:25.973Z" }, + { url = "https://files.pythonhosted.org/packages/fb/6a/ee505a80c8fd89620fb337c0596daecff87f33171fbb4ee3015fc3d7331f/coverage-7.15.3-cp314-cp314-musllinux_1_2_i686.whl", hash = "sha256:af000dd1bb859ff8066fda4c79512ff938c798116540307226b373099c7b151f", size = 253769, upload-time = "2026-08-02T18:49:27.883Z" }, + { url = "https://files.pythonhosted.org/packages/b0/41/6ab0f81c9e89660230d8f3f581d4732e5ddb75a885b0a5dfc73d315dc94f/coverage-7.15.3-cp314-cp314-musllinux_1_2_ppc64le.whl", hash = "sha256:a1b82490577f3889950b5a04f18712aef0207243e0749d60fe28c3c73ebfd5fd", size = 258045, upload-time = "2026-08-02T18:49:30.201Z" }, + { url = "https://files.pythonhosted.org/packages/bc/62/c995e91cae28cf31d6defab3bfb553dda5ac83ac7381b0f2b121264c307a/coverage-7.15.3-cp314-cp314-musllinux_1_2_riscv64.whl", hash = "sha256:c4fc90a60154c3e4b8a2dc206d6dbe852f1c235c249e0dc0cef909d032c9591a", size = 253587, upload-time = "2026-08-02T18:49:32.349Z" }, + { url = "https://files.pythonhosted.org/packages/84/df/f2049980f82d6890321f2065f9e66216eabbf4b2001815db958bc543f40a/coverage-7.15.3-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:f25bb884814a892948b4c20394db3f2364dd452d9492736479e7a493e63b0eb6", size = 255243, upload-time = "2026-08-02T18:49:34.324Z" }, + { url = "https://files.pythonhosted.org/packages/1d/82/2c841b67a978c0eb9c3707630b68f93f9e7585d78bb906bc8823ec6b07a5/coverage-7.15.3-cp314-cp314-win32.whl", hash = "sha256:722dbf8e7828fbcfe0dc8586167dc0a5ce85ad6ea171dbb21ed3f8d6581d3cb8", size = 224759, upload-time = "2026-08-02T18:49:36.326Z" }, + { url = "https://files.pythonhosted.org/packages/b3/78/5c93ec43784fd3e404ca23cd0584ae24bc1732de4a3fc194b68c3be88db0/coverage-7.15.3-cp314-cp314-win_amd64.whl", hash = "sha256:64d0845f9c3ed47302bed265c15ab4dbb64aa4ec1490839b8e328f4e7fa914d2", size = 225246, upload-time = "2026-08-02T18:49:38.366Z" }, + { url = "https://files.pythonhosted.org/packages/9d/77/813a054371f3b018cc63c6bdb46a3c35d5e95d4e3ed4f1449d4196106db5/coverage-7.15.3-cp314-cp314-win_arm64.whl", hash = "sha256:69bc14684f8fbbee9f9dbaa4fe79719b0da9725fc37956785c06ec365acf6926", size = 224673, upload-time = "2026-08-02T18:49:40.552Z" }, + { url = "https://files.pythonhosted.org/packages/8f/63/8c9f36cc71178d26db930baa03a4494abcc516d8d41bf820d0d85ef1d80b/coverage-7.15.3-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:f92df943c24b96cb215ca26b4f6a2283e63c5db80f1635aceea7fff11311917b", size = 223298, upload-time = "2026-08-02T18:49:42.634Z" }, + { url = "https://files.pythonhosted.org/packages/54/66/211f24d058ce9f56ebf1420d55b7574fdae924f6da3836f83c8bd4793e38/coverage-7.15.3-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:66591c46bdd2971d3ae2bc503a5f0459c2edcaf6b7e045b292000cc95bc6cb95", size = 223568, upload-time = "2026-08-02T18:49:44.706Z" }, + { url = "https://files.pythonhosted.org/packages/dd/bb/9c2ad5574a0d6420a96c6cade4f8a683931b9e79fe609f8924d7b6964616/coverage-7.15.3-cp314-cp314t-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:caa64458b81b18bfc67cdf1f6dc02b23e3edc672f2f8e11771fad75865415a43", size = 264932, upload-time = "2026-08-02T18:49:47.153Z" }, + { url = "https://files.pythonhosted.org/packages/ba/91/938c39e77bdd5a0a440412f975609ce3702dabbda6ac715719d93ca45a7b/coverage-7.15.3-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:447f5421ccf5475956cf516d4ca1d575f487947b6f4e11f9d80c6aefe24b3dc8", size = 267052, upload-time = "2026-08-02T18:49:49.324Z" }, + { url = "https://files.pythonhosted.org/packages/b0/a3/7b431a98af35d9cc6394e54cde9435b33b8591672fbece6a4931267d7a8e/coverage-7.15.3-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7a0c77ef8cd483a4987a5d12d1d9d5f7ee598dfdc6c0844417d847e5768dc779", size = 269473, upload-time = "2026-08-02T18:49:51.599Z" }, + { url = "https://files.pythonhosted.org/packages/32/58/dbc9951dce46be47a732823a1c571f62bcabdd54a68d8c281489a1a55cfb/coverage-7.15.3-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:0b273f4ff657446a06c2d85bf80e134fa869a92852ba5f87854a70e1fb44da77", size = 270591, upload-time = "2026-08-02T18:49:53.865Z" }, + { url = "https://files.pythonhosted.org/packages/71/bd/1d610772c7c0889bfe477a59c46ee66ea53e271f3f06951e9d55b317f7c6/coverage-7.15.3-cp314-cp314t-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:daea8c4fafa22488600405be2c2be525a9406fba3fc0a83acc726db3e14e2005", size = 264007, upload-time = "2026-08-02T18:49:55.875Z" }, + { url = "https://files.pythonhosted.org/packages/69/97/852eb3dcdba156b1a9078503f098499916bf889f964b61ad4a08223ac169/coverage-7.15.3-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:93ff57c530f3fa7aa69f92fb9b8892b8aa82712aa970842f4abf28657f42fb57", size = 266926, upload-time = "2026-08-02T18:49:57.944Z" }, + { url = "https://files.pythonhosted.org/packages/52/f8/b72cd238757fba2b587fc7dee047efe6e10b0c18343509faaaf502dd4680/coverage-7.15.3-cp314-cp314t-musllinux_1_2_i686.whl", hash = "sha256:4df21bef8b800eebda9018f53d49c9ace3aeb0090c850139b27923aafcb83e91", size = 264529, upload-time = "2026-08-02T18:50:00.035Z" }, + { url = "https://files.pythonhosted.org/packages/b8/0a/6c52ec4b7fb007cb6433d1fcfda4080cb15d75ad37ef9c31025f3427293e/coverage-7.15.3-cp314-cp314t-musllinux_1_2_ppc64le.whl", hash = "sha256:db567b02685f26034adcbd85055f80d12cdf02111b8ed00886093d98b2874ce2", size = 268263, upload-time = "2026-08-02T18:50:02.161Z" }, + { url = "https://files.pythonhosted.org/packages/c0/4e/f1f9aa3efd109a04353563a43fb5155340c1fdcdeaa6296ebed3b6f510ea/coverage-7.15.3-cp314-cp314t-musllinux_1_2_riscv64.whl", hash = "sha256:5318dd51b8600b947e058cf5a4fe54d183d9d13c49b97b64ca7be05a34df9bef", size = 263377, upload-time = "2026-08-02T18:50:04.243Z" }, + { url = "https://files.pythonhosted.org/packages/dd/fb/6b268a0b2728ef1c379ad656b899274477a5f6bed1bf6765b4b387fb0601/coverage-7.15.3-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:c995bfa383c54704839b6c4c2627a1c00895597ada0e5e8190c81d8bd620555c", size = 265688, upload-time = "2026-08-02T18:50:06.428Z" }, + { url = "https://files.pythonhosted.org/packages/29/54/1a3ea96e5d5e7cd41dc432597bfc60692910e635d05e1cc25a8ccc243581/coverage-7.15.3-cp314-cp314t-win32.whl", hash = "sha256:6433fafb8da0e1d02eb53411e0ecdadb6b88f0224fdc23317e703c0e88937d42", size = 225066, upload-time = "2026-08-02T18:50:08.533Z" }, + { url = "https://files.pythonhosted.org/packages/31/9d/a7b0d9afd18ed5274dd00651a78e7810a931c70d94b79996f150bec1a30f/coverage-7.15.3-cp314-cp314t-win_amd64.whl", hash = "sha256:fe578952b1b29fe8c777f43f241d49efac4b56724a3434f5d22ebe3c208df429", size = 225897, upload-time = "2026-08-02T18:50:10.572Z" }, + { url = "https://files.pythonhosted.org/packages/ca/11/34c5ae40b945e69aa72b87dc268135b7049905f3824af573b7073acbb946/coverage-7.15.3-cp314-cp314t-win_arm64.whl", hash = "sha256:d2e1acb7aee29dfa8f3e48c23f36670898baca1209d9bdd3985a50c7f982165e", size = 225212, upload-time = "2026-08-02T18:50:12.63Z" }, + { url = "https://files.pythonhosted.org/packages/37/e7/7069b3d6c018917f49ba2e1c5fb910e498c7fefa3a1b78cb1b79e61ff45d/coverage-7.15.3-py3-none-any.whl", hash = "sha256:da78fa6fc7dafe4212839173133ee85afcf42c5cd5f3e47fa7c1c210453b445e", size = 214297, upload-time = "2026-08-02T18:50:14.709Z" }, +] + [[package]] name = "iniconfig" version = "2.3.0" @@ -63,6 +102,20 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/24/25/1de2678b631f5a49215c6c96fff41ba892b0a34df68d6d80292b1b48aa7f/pytest-9.1.1-py3-none-any.whl", hash = "sha256:37a86b45efb9a47a61a36449063e8e18d0cab3161329fc099eb21783169c4f0c", size = 386536, upload-time = "2026-06-19T10:58:31.347Z" }, ] +[[package]] +name = "pytest-cov" +version = "7.1.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "coverage" }, + { name = "pluggy" }, + { name = "pytest" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b1/51/a849f96e117386044471c8ec2bd6cfebacda285da9525c9106aeb28da671/pytest_cov-7.1.0.tar.gz", hash = "sha256:30674f2b5f6351aa09702a9c8c364f6a01c27aae0c1366ae8016160d1efc56b2", size = 55592, upload-time = "2026-03-21T20:11:16.284Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/9d/7a/d968e294073affff457b041c2be9868a40c1c71f4a35fcc1e45e5493067b/pytest_cov-7.1.0-py3-none-any.whl", hash = "sha256:a0461110b7865f9a271aa1b51e516c9a95de9d696734a2f71e3e78f46e1d4678", size = 22876, upload-time = "2026-03-21T20:11:14.438Z" }, +] + [[package]] name = "python-ds-algorithms" version = "0.0.1" @@ -71,6 +124,7 @@ source = { virtual = "." } [package.dev-dependencies] dev = [ { name = "pytest" }, + { name = "pytest-cov" }, { name = "ruff" }, ] @@ -79,6 +133,7 @@ dev = [ [package.metadata.requires-dev] dev = [ { name = "pytest" }, + { name = "pytest-cov", specifier = ">=7.1.0" }, { name = "ruff", specifier = ">=0.16.0" }, ] From 3a99fc23b3a2ad1a55d8b3162290cc96dc4ef8f2 Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcode0919@users.noreply.github.com> Date: Sun, 2 Aug 2026 19:13:32 -0700 Subject: [PATCH 2/2] Raise percentage to 98 --- src/n_ary_tree/max_depth.py | 7 ++----- src/n_ary_tree/preorder_traversal.py | 4 ++-- src/n_ary_tree/preorder_traversal_iterative.py | 2 +- src/queues/walls_and_gates.py | 6 +----- src/singly_linked_lists/copy_list_with_random_pointer.py | 3 --- src/singly_linked_lists/find_middle_node.py | 4 ++-- src/singly_linked_lists/intersection_two_linked_lists.py | 3 --- src/singly_linked_lists/palindrome_linked_list.py | 2 +- src/singly_linked_lists/reverse_linked_list.py | 2 +- tests/n_ary_tree/test_max_depth.py | 2 ++ tests/n_ary_tree/test_preorder_traversal.py | 2 ++ tests/n_ary_tree/test_preorder_traversal_iterative.py | 2 ++ tests/singly_linked_lists/test_merge_two_sorted_lists.py | 1 + tests/singly_linked_lists/test_odd_even_linked_list.py | 3 +++ tests/singly_linked_lists/test_palindrome_linked_list.py | 3 +++ 15 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/n_ary_tree/max_depth.py b/src/n_ary_tree/max_depth.py index bdda18e..984cb00 100644 --- a/src/n_ary_tree/max_depth.py +++ b/src/n_ary_tree/max_depth.py @@ -9,14 +9,11 @@ from src.n_ary_tree.nary_tree_node import NaryTreeNode -def max_depth_top_down(root: NaryTreeNode) -> int: +def max_depth_top_down(root: NaryTreeNode | None) -> int: """Recurse top-down, threading the current depth through each call as an argument.""" def traverse(node, depth): """Return the deepest depth reached among node's descendants, starting from depth.""" - if not node: - return depth - maximum_depth = depth if node.children: @@ -30,7 +27,7 @@ def traverse(node, depth): return traverse(root, 1) -def max_depth_bottom_up(root: NaryTreeNode) -> int: +def max_depth_bottom_up(root: NaryTreeNode | None) -> int: """Recurse bottom-up, computing each subtree's depth from its children's returned depths.""" if not root: return 0 diff --git a/src/n_ary_tree/preorder_traversal.py b/src/n_ary_tree/preorder_traversal.py index 4f4f67c..68973c7 100644 --- a/src/n_ary_tree/preorder_traversal.py +++ b/src/n_ary_tree/preorder_traversal.py @@ -1,7 +1,7 @@ -from src.n_ary_tree import nary_tree_node +from src.n_ary_tree.nary_tree_node import NaryTreeNode -def preorder_traversal(root: nary_tree_node) -> list: +def preorder_traversal(root: NaryTreeNode | None) -> list: """Preorder Traversal. Given the root of an n-ary tree, return the preorder traversal of its nodes' values — a diff --git a/src/n_ary_tree/preorder_traversal_iterative.py b/src/n_ary_tree/preorder_traversal_iterative.py index c626e84..42c4c43 100644 --- a/src/n_ary_tree/preorder_traversal_iterative.py +++ b/src/n_ary_tree/preorder_traversal_iterative.py @@ -1,7 +1,7 @@ from src.n_ary_tree.nary_tree_node import NaryTreeNode -def preorder_traversal_iterative(root: NaryTreeNode) -> list: +def preorder_traversal_iterative(root: NaryTreeNode | None) -> list: """Preorder Traversal (Iterative). Given the root of an n-ary tree, return the preorder traversal of its nodes' values — a diff --git a/src/queues/walls_and_gates.py b/src/queues/walls_and_gates.py index 7f64d42..758315a 100644 --- a/src/queues/walls_and_gates.py +++ b/src/queues/walls_and_gates.py @@ -1,7 +1,7 @@ from collections import deque -def walls_and_gates(rooms_input: list[list[int]]) -> list[list[int]]: +def walls_and_gates(rooms: list[list[int]]) -> list[list[int]]: """Walls and Gates (BFS). Run a multi-source BFS from every gate to fill each empty room with its distance to the @@ -28,11 +28,7 @@ def walls_and_gates(rooms_input: list[list[int]]) -> list[list[int]]: 0 -1 3 4 ``` """ - rooms = rooms_input[:] height = len(rooms) - if height == 0: - return [[]] - width = len(rooms[0]) queue = deque() diff --git a/src/singly_linked_lists/copy_list_with_random_pointer.py b/src/singly_linked_lists/copy_list_with_random_pointer.py index 765b2f0..cafff1e 100644 --- a/src/singly_linked_lists/copy_list_with_random_pointer.py +++ b/src/singly_linked_lists/copy_list_with_random_pointer.py @@ -17,9 +17,6 @@ def copy_random_list[T](head: Node[T]) -> Node[T] | None: Walks the list once, cloning each node and its `random` target via a visited-node lookup. """ - if not head: - return None - current: Node[T] | None = head nodes: dict[Node[T], Node[T]] = {} new_node: Node[T] | None = Node[T](head.value, None, None) diff --git a/src/singly_linked_lists/find_middle_node.py b/src/singly_linked_lists/find_middle_node.py index de55329..6319ff5 100644 --- a/src/singly_linked_lists/find_middle_node.py +++ b/src/singly_linked_lists/find_middle_node.py @@ -1,7 +1,7 @@ from src.data_structures.singly_linked_list_node import SinglyLinkedListNode -def find_middle_node(head: SinglyLinkedListNode) -> SinglyLinkedListNode | None: +def find_middle_node(head: SinglyLinkedListNode) -> SinglyLinkedListNode: """Find Middle Node. Find the node that comes before the middle node of a singly linked list. @@ -13,7 +13,7 @@ def find_middle_node(head: SinglyLinkedListNode) -> SinglyLinkedListNode | None: slow = head fast = head - while slow and fast.next and fast.next.next: + while slow and slow.next and fast.next and fast.next.next: slow = slow.next fast = fast.next.next diff --git a/src/singly_linked_lists/intersection_two_linked_lists.py b/src/singly_linked_lists/intersection_two_linked_lists.py index 45dc54d..2d27a55 100644 --- a/src/singly_linked_lists/intersection_two_linked_lists.py +++ b/src/singly_linked_lists/intersection_two_linked_lists.py @@ -15,9 +15,6 @@ def get_intersection_node( cover equal total distance and meet at the intersection (or both reach None if there is none). Time: O(N), Space: O(1). """ - if not head_a or not head_b: - return None - node_a: SinglyLinkedListNode = head_a node_b: SinglyLinkedListNode = head_b diff --git a/src/singly_linked_lists/palindrome_linked_list.py b/src/singly_linked_lists/palindrome_linked_list.py index 4a0df1d..9739209 100644 --- a/src/singly_linked_lists/palindrome_linked_list.py +++ b/src/singly_linked_lists/palindrome_linked_list.py @@ -26,7 +26,7 @@ def is_palindrome(head: SinglyLinkedListNode | None) -> bool: first_pointer = head second_pointer = second_half_head - while palindrome and second_pointer: + while palindrome and first_pointer and second_pointer: if first_pointer.data != second_pointer.data: return False first_pointer = first_pointer.next diff --git a/src/singly_linked_lists/reverse_linked_list.py b/src/singly_linked_lists/reverse_linked_list.py index 8c5f945..9be4ba0 100644 --- a/src/singly_linked_lists/reverse_linked_list.py +++ b/src/singly_linked_lists/reverse_linked_list.py @@ -1,7 +1,7 @@ from src.data_structures.singly_linked_list_node import SinglyLinkedListNode -def reverse_linked_list(head: SinglyLinkedListNode) -> SinglyLinkedListNode | None: +def reverse_linked_list(head: SinglyLinkedListNode | None) -> SinglyLinkedListNode | None: """Reverse Linked List. Reverse a singly linked list. diff --git a/tests/n_ary_tree/test_max_depth.py b/tests/n_ary_tree/test_max_depth.py index 4b84e2b..99b44c1 100644 --- a/tests/n_ary_tree/test_max_depth.py +++ b/tests/n_ary_tree/test_max_depth.py @@ -3,8 +3,10 @@ def test_max_depth_bottom_up(): + assert max_depth_bottom_up(None) == 0 assert max_depth_bottom_up(get_n_nary_tree()) == 4 def test_max_depth_top_down(): + assert max_depth_top_down(None) == 0 assert max_depth_top_down(get_n_nary_tree()) == 4 diff --git a/tests/n_ary_tree/test_preorder_traversal.py b/tests/n_ary_tree/test_preorder_traversal.py index 4eddd80..399dfb9 100644 --- a/tests/n_ary_tree/test_preorder_traversal.py +++ b/tests/n_ary_tree/test_preorder_traversal.py @@ -3,5 +3,7 @@ def test_preorder_traversal(): + assert preorder_traversal(None) == [] + root = get_n_nary_tree() assert preorder_traversal(root) == [1, 2, 5, 7, 6, 3, 8, 4, 9, 10, 11, 12] diff --git a/tests/n_ary_tree/test_preorder_traversal_iterative.py b/tests/n_ary_tree/test_preorder_traversal_iterative.py index 2396aa0..64270b6 100644 --- a/tests/n_ary_tree/test_preorder_traversal_iterative.py +++ b/tests/n_ary_tree/test_preorder_traversal_iterative.py @@ -3,5 +3,7 @@ def test_preorder_traversal_iterative(): + assert preorder_traversal_iterative(None) == [] + root = get_n_nary_tree() assert preorder_traversal_iterative(root) == [1, 2, 5, 7, 6, 3, 8, 4, 9, 10, 11, 12] diff --git a/tests/singly_linked_lists/test_merge_two_sorted_lists.py b/tests/singly_linked_lists/test_merge_two_sorted_lists.py index 4f5b980..a38f9e2 100644 --- a/tests/singly_linked_lists/test_merge_two_sorted_lists.py +++ b/tests/singly_linked_lists/test_merge_two_sorted_lists.py @@ -4,6 +4,7 @@ def test_merge_two_lists(): + assert merge_two_lists(None, None) is None list1 = SinglyLinkedListNode(1) assert get_list_values(merge_two_lists(list1, None)) == [1] diff --git a/tests/singly_linked_lists/test_odd_even_linked_list.py b/tests/singly_linked_lists/test_odd_even_linked_list.py index de4bc3a..46c2332 100644 --- a/tests/singly_linked_lists/test_odd_even_linked_list.py +++ b/tests/singly_linked_lists/test_odd_even_linked_list.py @@ -4,6 +4,9 @@ def test_odd_even_linked_list(): + head = None + assert odd_even_list(head) is None + head = SinglyLinkedListNode(1) assert odd_even_list(head) == head diff --git a/tests/singly_linked_lists/test_palindrome_linked_list.py b/tests/singly_linked_lists/test_palindrome_linked_list.py index 93a2233..0f0e988 100644 --- a/tests/singly_linked_lists/test_palindrome_linked_list.py +++ b/tests/singly_linked_lists/test_palindrome_linked_list.py @@ -3,6 +3,9 @@ def test_is_palindrome(): + head = None + assert not is_palindrome(head) + head = SinglyLinkedListNode(1) assert is_palindrome(head)