Skip to content

Commit 9fd086d

Browse files
authored
Add code coverage support (#193)
* Add code coverage support * Raise percentage to 98
1 parent bffcfce commit 9fd086d

56 files changed

Lines changed: 176 additions & 107 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/Python.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ jobs:
3131
- name: Lint
3232
run: |
3333
just lint
34-
- name: Unit tests
34+
- name: Unit tests + Code coverage check
3535
run: |
36-
just test
36+
just test-coverage-percentage

‎CLAUDE.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This project uses `uv` for dependency management and `just` as the command runne
1010
just sync # install/sync dependencies (uv sync --all-groups)
1111
just lint # uv run ruff check . --fix && uv run ruff format .
1212
just test # uv run pytest
13+
just test-coverage # uv run pytest --cov=src --cov-report=term-missing
14+
just test-coverage-percentage # uv run pytest --cov=src --cov-report=term --cov-fail-under=98
1315
uv run pytest tests/algorithm_theory/test_binary_search.py # run one test file
1416
uv run pytest tests/algorithm_theory/test_binary_search.py::test_binary_search_iterative # run one test
1517
uv add {package} # add a dependency

‎justfile‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ lint:
44
sync:
55
uv sync --all-groups
66
test:
7-
uv run pytest
7+
uv run pytest
8+
test-coverage:
9+
uv run pytest --cov=src --cov-report=term-missing
10+
test-coverage-percentage:
11+
uv run pytest --cov=src --cov-report=term --cov-fail-under=98

‎pyproject.toml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ requires-python = ">=3.14"
66
dependencies = []
77

88
[dependency-groups]
9-
dev = ["pytest", "ruff>=0.16.0"]
9+
dev = [
10+
"pytest",
11+
"pytest-cov>=7.1.0",
12+
"ruff>=0.16.0",
13+
]
14+
15+
[tool.coverage]
1016

1117
[tool.pytest.ini_options]
1218
pythonpath = ["."]

‎src/algorithm_theory/binary_search.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def binary_search_iterative(array: list[int], target: int) -> int | None:
1313
right = len(array) - 1
1414

1515
while left <= right:
16-
middle = int((left + right) / 2)
16+
middle = (left + right) // 2
1717

1818
if array[middle] == target:
1919
return middle
@@ -27,7 +27,7 @@ def binary_search_iterative(array: list[int], target: int) -> int | None:
2727
def binary_search_recursive(array: list[int], left: int, right: int, target: int) -> int | None:
2828
"""Recurse into the left or right sub-array each call until target is found or the bounds cross."""
2929
if right >= left:
30-
mid = int((left + right) / 2)
30+
mid = (left + right) // 2
3131

3232
# If element is present at the middle
3333
if array[mid] == target:

‎src/algorithm_theory/bubble_sort.py‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,13 @@ def bubble_sort(array: list[int]) -> list[int]:
66
77
Example: `[2, 1, 3] -> [1, 2, 3]`
88
"""
9-
length = len(array)
10-
11-
# Return if nothing to sort
12-
if length <= 1:
13-
return array
14-
159
# Reduce the upper limit with each iteration, since the correct value has 'bubbled' to the top
16-
for i in reversed(range(0, length)):
17-
swap = False
10+
for i in reversed(range(0, len(array))):
1811
# Compare value and switch up to the upper limit 'i'
1912
for j in range(0, i):
2013
next_val = j + 1
21-
if next_val < length and array[j] > array[next_val]:
14+
if next_val < len(array) and array[j] > array[next_val]:
2215
temp = array[j]
2316
array[j] = array[next_val]
2417
array[next_val] = temp
25-
swap = True
26-
27-
# Return if no swaps take place
28-
if not swap:
29-
return array
3018
return array

‎src/arrays/find_smallest_positive_integer.py‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ def find_smallest_positive_integer(arr: list[int]) -> int:
77
88
Single pass tracking the smallest positive value seen so far.
99
"""
10-
length = len(arr)
10+
smallest = -1
1111

12-
if length == 0:
13-
return 0
14-
15-
smallest = None
16-
17-
for i in range(length):
12+
for i in range(len(arr)):
1813
if arr[i] > 0:
19-
if smallest is None:
14+
if smallest == -1:
2015
smallest = arr[i]
2116
elif arr[i] < smallest:
2217
smallest = arr[i]
2318

24-
if smallest is None:
19+
if smallest == -1:
2520
return 0
2621
return smallest

‎src/arrays/make_array_consecutive.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ def make_array_consecutive(values: list[int]) -> int:
77
88
Sort the array, then sum the gaps between consecutive sorted values.
99
"""
10-
length = len(values)
1110
numbers_needed = 0
1211

13-
if length <= 1:
12+
if len(values) <= 1:
1413
return numbers_needed
1514

1615
sorted_numbers = sorted(values)
1716

18-
for i in range(1, length):
17+
for i in range(1, len(values)):
1918
numbers_needed += (sorted_numbers[i] - sorted_numbers[i - 1]) - 1
2019

2120
return numbers_needed

‎src/arrays/remove_duplicates.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ def remove_duplicates(nums: list[int]) -> int:
99
1010
Two-pointer in-place compaction over an already-sorted array.
1111
"""
12-
length = len(nums)
13-
if length <= 1:
14-
return length
12+
if len(nums) <= 1:
13+
return len(nums)
1514

1615
last_index = 0
1716

18-
for index in range(1, length):
17+
for index in range(1, len(nums)):
1918
if nums[index] == nums[last_index]:
2019
continue
2120
last_index += 1

‎src/arrays/two_sum_ii.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ def two_sum_ii(numbers: list[int], target: int) -> list[int]:
2121
index_right -= 1
2222
else:
2323
return [index_left + 1, index_right + 1]
24+
return []

0 commit comments

Comments
 (0)