Skip to content

Commit 929dd9f

Browse files
Restructure documentation (#189)
* Bump setuptools from 82.0.1 to 83.0.0 (#182) * MinHeap Data Structure (#183) * Migrate project to Justfile * Update Github action * Add min heap data structure * Format files * Reorganize test files (#184) * Migrate test files * Reorganize test files * Update readme files (#185) * Update data structure readme * Update README.mds * Update project to use uv (#186) * Fix Github Action (#187) * Fix formatting * Fix astral version * Update project configurations (#188) * Initial CLAUDE.md * Update gitignore * Migrate algorithm_theory folder * Migrate remaining readme files * Update remaining files * Ignore local configuration files Prevents *.local.* files (e.g. .claude/settings.local.json) from being tracked going forward. --------- Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 19756ce commit 929dd9f

156 files changed

Lines changed: 1689 additions & 2064 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.

‎.gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,9 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
# Ruff
107+
.ruff_cache/
108+
109+
# Local configuration files
110+
*.local.*

‎CLAUDE.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Commands
6+
7+
This project uses `uv` for dependency management and `just` as the command runner.
8+
9+
```bash
10+
just sync # install/sync dependencies (uv sync --all-groups)
11+
just lint # uv run ruff check . --fix && uv run ruff format .
12+
just test # uv run pytest
13+
uv run pytest tests/algorithm_theory/test_binary_search.py # run one test file
14+
uv run pytest tests/algorithm_theory/test_binary_search.py::test_binary_search_iterative # run one test
15+
uv add {package} # add a dependency
16+
```
17+
18+
There is no separate build step; this is a pure Python (3.14+) library of algorithms with no runtime dependencies.
19+
20+
## Architecture
21+
22+
The repo is organized by topic into top-level directories, each containing implementation modules directly at the
23+
top level (no `src/` layout). `pyproject.toml` sets `pythonpath = ["."]`, so both implementation and test code
24+
import via the top-level package name, e.g. `from algorithm_theory.binary_search import binary_search_iterative`.
25+
26+
Three categories of top-level directories, per the root `README.md`:
27+
28+
- **`algorithm_theory/`** — canonical/fundamental algorithm approaches (e.g. binary/jump/linear search, bubble
29+
sort).
30+
- **`data_structures/`** — custom implementations of core data structures (binary tree, linked lists, queue,
31+
stack, trie) that other topic directories' problems build on top of.
32+
- **Topic/problem directories** (`arrays/`, `binary_search/`, `binary_tree/`, `circularly_linked_list/`,
33+
`doubly_linked_list/`, `integers/`, `math_problems/`, `multi_dimensional_arrays/`, `n_ary_tree/`, `queues/`,
34+
`singly_linked_lists/`, `stacks/`, `strings/`, `trie/`)
35+
36+
The problem description, constraints, and examples live as a
37+
**doc comment directly on the function/class that solves it** (see any existing file, e.g.
38+
`arrays/two_sum.py`, as the template when adding a new problem). Only when a file genuinely has multiple
39+
top-level functions/classes for one problem (e.g. a brute-force and an optimized approach, like
40+
`stacks/daily_temperatures.py`) does it use a module-level docstring for the shared problem statement plus a
41+
short docstring per function for the approach-specific detail — a single-function file should never have both a module docstring and a redundant function docstring.
42+
43+
Tests live under `tests/<same-directory-name>/test_<module>.py`, mirroring the source layout 1:1 (e.g.
44+
`algorithm_theory/binary_search.py` → `tests/algorithm_theory/test_binary_search.py`). `test_helpers/test_helpers.py`
45+
holds shared builders/fixtures (e.g. `get_binary_search_tree`, `get_singly_linked_list`) used across multiple test
46+
directories for constructing trees and linked lists — check there before writing new setup code for tree/list-based
47+
tests.
48+
49+
Node/structure classes used across multiple directories (e.g. `binary_tree.tree_node.TreeNode`,
50+
`data_structures.singly_linked_list_node.SinglyLinkedListNode`, `n_ary_tree.nary_tree_node.NaryTreeNode`) are
51+
imported directly from their defining module rather than redefined per problem.
52+
53+
## Style
54+
55+
Ruff is configured in `pyproject.toml` (line length 100, target `py314`, rule sets `E, F, W, I, N, D`). Docstring
56+
presence itself is not enforced (`D100-D107` ignored), but when docstrings are present their formatting is linted.
57+
`tests/*` is exempt from docstring rules entirely. Solutions favor explicit, readable Python over terse one-liners
58+
(stated explicitly in the root `README.md`).

‎README.md‎

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,27 @@
33
Various Data Structures and Algorithm Solutions in Python (3.x). Succinct Python one-liners are avoided in most solutions
44
to prevent obscuring the function and logic of the algorithms / data-structures.
55

6-
* [Algorithm Theory](algorithm_theory/README.md)
7-
* [Searches](algorithm_theory/README.md#searches)
8-
* [Binary Search](algorithm_theory/README.md#binary-search)
9-
* [Jump Search](algorithm_theory/README.md#jump-search)
10-
* [Linear Search](algorithm_theory/README.md#linear-search)
11-
* [Sorts](algorithm_theory/README.md#sorts)
12-
* [Bubble Sort](algorithm_theory/README.md#bubble-sort)
13-
* [Data Structures](data_structures/README.md)
14-
* [Binary Tree](data_structures/README.md#binary-tree)
15-
* [Circularly Linked List](data_structures/README.md#circularly-linked-list)
16-
* [Doubly Linked List](data_structures/README.md#doubly-linked-list)
17-
* [Singly Linked List](data_structures/README.md#singly-linked-list)
18-
* [Queue](data_structures/README.md#queue)
19-
* [Stack](data_structures/README.md#stack)
20-
* [Trie](data_structures/README.md#trie)
6+
* [Algorithm Theory](algorithm_theory/)
7+
* [Data Structures](data_structures/)
218

229
## Algorithms
2310

24-
* [Arrays](arrays/README.md)
25-
* [Binary Search](binary_search/README.md)
26-
* [Binary Tree](binary_tree/README.md)
27-
* [Circularly Linked Lists](circularly_linked_list/README.md)
28-
* [Doubly Linked Lists](doubly_linked_list/README.md)
29-
* [Integers](integers/README.md)
30-
* [Matrices](multi_dimensional_arrays/README.md)
31-
* [N-ary Tree](n_ary_tree/README.md)
32-
* [Queues](queues/README.md)
33-
* [Singly Linked Lists](singly_linked_lists/README.md)
34-
* [Stacks](stacks/README.md)
35-
* [Strings](strings/README.md)
36-
* [Trie](trie/README.md)
11+
* [Arrays](arrays/)
12+
* [Binary Search](binary_search/)
13+
* [Binary Tree](binary_tree/)
14+
* [Circularly Linked Lists](circularly_linked_list/)
15+
* [Doubly Linked Lists](doubly_linked_list/)
16+
* [Integers](integers/)
17+
* [Math Problems](math_problems/)
18+
* [Matrices](multi_dimensional_arrays/)
19+
* [N-ary Tree](n_ary_tree/)
20+
* [Queues](queues/)
21+
* [Singly Linked Lists](singly_linked_lists/)
22+
* [Stacks](stacks/)
23+
* [Strings](strings/)
24+
* [Trie](trie/)
25+
26+
Each directory's problems and approaches are documented as doc comments directly in the corresponding `.py` files.
3727

3828
## Project Setup
3929

‎algorithm_theory/README.md‎

Lines changed: 0 additions & 138 deletions
This file was deleted.

‎algorithm_theory/binary_search.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
# Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
2-
# covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
3-
# the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or
4-
# the interval is empty.
1+
"""Binary Search
2+
3+
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
4+
covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
5+
the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or
6+
the interval is empty.
7+
"""
8+
59
from typing import Optional
610

711

812
def binary_search_iterative(array: [int], target: int) -> Optional[int]:
13+
"""Narrow the [left, right] bounds by half each iteration until target is found or the bounds cross."""
914
left = 0
1015
right = len(array) - 1
1116

@@ -22,6 +27,7 @@ def binary_search_iterative(array: [int], target: int) -> Optional[int]:
2227

2328

2429
def binary_search_recursive(array: [int], left: int, right: int, target: int) -> Optional[int]:
30+
"""Recurse into the left or right sub-array each call until target is found or the bounds cross."""
2531
if right >= left:
2632
mid = int((left + right) / 2)
2733

‎algorithm_theory/bubble_sort.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in
2-
# wrong order.
1+
def bubble_sort(array: [int]) -> []:
2+
"""Bubble Sort.
33
4+
The simplest sorting algorithm, working by repeatedly swapping the adjacent elements if they are
5+
in wrong order.
46
5-
def bubble_sort(array: [int]) -> []:
7+
Example: `[2, 1, 3] -> [1, 2, 3]`
8+
"""
69
length = len(array)
710

811
# Return if nothing to sort

‎algorithm_theory/jump_search.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
# Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search)
2-
# by jumping ahead by fixed steps or skipping some elements in place of searching all elements.
3-
#
4-
# step size is m = √n.
5-
61
import math
72
from typing import Optional
83

94
from algorithm_theory.linear_search import linear_search
105

116

127
def jump_search(array: [int], target: int) -> Optional[int]:
8+
"""Jump Search (https://www.geeksforgeeks.org/jump-search/).
9+
10+
A searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear
11+
search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.
12+
13+
For example, suppose we have an array arr[] of size n and block (to be jumped) size `m`. Then we search
14+
at the indexes `arr[0]`, `arr[m]`, `arr[2m]…..arr[km]`, and so on. Once we find the interval
15+
(`arr[km] < x < arr[(k+1)m]`), we perform a linear search operation from the index `km` to find `x`.
16+
17+
What is the optimal block size to be skipped?
18+
In the worst case, we have to do `n/m` jumps and if the last checked value is greater than the element
19+
to be searched for, we perform `m-1` comparisons more for linear search. Therefore the total number of
20+
comparisons in the worst case will be `((n/m) + m-1)`. The value of the function `((n/m) + m-1)` will be
21+
minimum when `m = √n`. Therefore, the best step size is `m = √n`. (Geeks For Geeks)
22+
"""
1323
length = len(array)
1424
interval = int(math.sqrt(length))
1525
index = interval - 1 # 0 Based

‎algorithm_theory/linear_search.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import Optional
22

3-
# The most basic search. Start from the leftmost element of the array and one by one compare the target with each
4-
# element of the array. If the target matches with an element, return the index.
5-
63

74
def linear_search(array: [int], left: int, right: int, target: int) -> Optional[int]:
5+
"""Linear Search.
6+
7+
The most basic search. Start from the leftmost element of the array and one by one compare the target
8+
with each element of the array. If the target matches with an element, return the index.
9+
"""
810
for index in range(left, right + 1):
911
if array[index] == target:
1012
return index

‎arrays/arrays_intersection.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
from typing import List
1+
"""Intersection of Three Sorted Arrays.
2+
3+
Given three integer arrays arr1, arr2, and arr3 sorted in strictly increasing order, return a sorted array of
4+
only the integers that appear in all three arrays.
25
3-
# Leetcode 1213. Intersection of Three Sorted Arrays
6+
Example: `arr1 = [1, 2, 3, 4, 5], arr2 = [1, 2, 5, 7, 9], arr3 = [1, 3, 4, 5, 8]` -> `[1, 5]`
7+
"""
8+
9+
from typing import List
410

511

612
def arrays_intersection(arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]:
13+
"""Set-intersection approach across all three arrays."""
714
intersection = set(arr1)
815
intersection = intersection.intersection(arr2)
916
return list(intersection.intersection(arr3))
1017

1118

1219
def arrays_intersection2(arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]:
20+
"""Three-pointer sweep exploiting the sorted order of each array."""
1321
index1 = 0
1422
index2 = 0
1523
index3 = 0

‎arrays/check_double.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33

44
def check_double(arr: List[int]) -> bool:
5+
"""Check Double.
6+
7+
Given an array arr of integers, check if there exist two integers N and M such that N is the double of M (i.e.
8+
N = 2 * M). More formally, check if there exist two indices i and j such that i != j and arr[i] == 2 * arr[j].
9+
10+
Example: `arr = [10, 2, 5, 3]` -> `True` (`N = 10` is the double of `M = 5`, since `10 = 2 * 5`)
11+
12+
Track seen values in a set and check for a value's double or half on each pass.
13+
"""
514
elements = set()
615

716
for num in arr:

0 commit comments

Comments
 (0)