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
4 changes: 2 additions & 2 deletions .github/workflows/Python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ jobs:
- name: Lint
run: |
just lint
- name: Unit tests
- name: Unit tests + Code coverage check
run: |
just test
just test-coverage-percentage
2 changes: 2 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ lint:
sync:
uv sync --all-groups
test:
uv run pytest
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
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["."]
Expand Down
4 changes: 2 additions & 2 deletions src/algorithm_theory/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
16 changes: 2 additions & 14 deletions src/algorithm_theory/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 4 additions & 9 deletions src/arrays/find_smallest_positive_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions src/arrays/make_array_consecutive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 3 additions & 4 deletions src/arrays/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/arrays/two_sum_ii.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
9 changes: 4 additions & 5 deletions src/arrays/valid_mountain_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion src/data_structures/binary_tree_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions src/n_ary_tree/max_depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/n_ary_tree/preorder_traversal.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/n_ary_tree/preorder_traversal_iterative.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/queues/walls_and_gates.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()

Expand Down
3 changes: 0 additions & 3 deletions src/singly_linked_lists/copy_list_with_random_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions src/singly_linked_lists/find_middle_node.py
Original file line number Diff line number Diff line change
@@ -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:
"""Find Middle Node.

Find the node that comes before the middle node of a singly linked list.
Expand All @@ -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 slow.next and fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next

Expand Down
3 changes: 0 additions & 3 deletions src/singly_linked_lists/intersection_two_linked_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/singly_linked_lists/palindrome_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/singly_linked_lists/reverse_linked_list.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 1 addition & 2 deletions src/stacks/min_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
2 changes: 0 additions & 2 deletions src/strings/longest_substring_without_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions src/strings/palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/strings/palindrome_permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions src/strings/string_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading
Loading