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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
15 changes: 7 additions & 8 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ There is no separate build step; this is a pure Python (3.14+) library of algori

## Architecture

The repo is organized by topic into top-level directories, each containing implementation modules directly at the
top level (no `src/` layout). `pyproject.toml` sets `pythonpath = ["."]`, so both implementation and test code
import via the top-level package name, e.g. `from algorithm_theory.binary_search import binary_search_iterative`.
Algorithm code is stored in `/src`. `pyproject.toml` sets `pythonpath = ["."]`, so both
implementation and test code import via the top-level package name, e.g. `from src.algorithm_theory.binary_search import binary_search_iterative`.

Three categories of top-level directories, per the root `README.md`:
Three categories of top-level `/src` directories, per the root `README.md`:

- **`algorithm_theory/`** — canonical/fundamental algorithm approaches (e.g. binary/jump/linear search, bubble
sort).
Expand Down Expand Up @@ -52,7 +51,7 @@ imported directly from their defining module rather than redefined per problem.

## Style

Ruff is configured in `pyproject.toml` (line length 100, target `py314`, rule sets `E, F, W, I, N, D`). Docstring
presence itself is not enforced (`D100-D107` ignored), but when docstrings are present their formatting is linted.
`tests/*` is exempt from docstring rules entirely. Solutions favor explicit, readable Python over terse one-liners
(stated explicitly in the root `README.md`).
- Solutions favor explicit, readable Python over complicated one-liners
- Ruff is configured in `pyproject.toml` (line length 100, target `py314`, rule sets `E, F, W, I, N, D`).
- Rules `D100-D107` are ignored
- `tests/*` is exempt from docstring rules entirely.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# Python Data Structures and Algorithms ![](https://github.com/ahcode0919/python-ds-algorithms/actions/workflows/Python.yml/badge.svg?branch=main) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Various Data Structures and Algorithm Solutions in Python (3.x). Succinct Python one-liners are avoided in most solutions
to prevent obscuring the function and logic of the algorithms / data-structures.
to prevent obscuring the function and logic of the algorithms / data-structures.

* [Algorithm Theory](algorithm_theory/)
* [Data Structures](data_structures/)
* [Algorithm Theory](src/algorithm_theory/)
* [Data Structures](src/data_structures/)

## Algorithms

* [Arrays](arrays/)
* [Binary Search](binary_search/)
* [Binary Tree](binary_tree/)
* [Circularly Linked Lists](circularly_linked_list/)
* [Doubly Linked Lists](doubly_linked_list/)
* [Integers](integers/)
* [Math Problems](math_problems/)
* [Matrices](multi_dimensional_arrays/)
* [N-ary Tree](n_ary_tree/)
* [Queues](queues/)
* [Singly Linked Lists](singly_linked_lists/)
* [Stacks](stacks/)
* [Strings](strings/)
* [Trie](trie/)
* [Arrays](src/arrays/)
* [Binary Search](src/binary_search/)
* [Binary Tree](src/binary_tree/)
* [Circularly Linked Lists](src/circularly_linked_list/)
* [Doubly Linked Lists](src/doubly_linked_list/)
* [Integers](src/integers/)
* [Math Problems](src/math_problems/)
* [Matrices](src/multi_dimensional_arrays/)
* [N-ary Tree](src/n_ary_tree/)
* [Queues](src/queues/)
* [Singly Linked Lists](src/singly_linked_lists/)
* [Stacks](src/stacks/)
* [Strings](src/strings/)
* [Trie](src/trie/)

Each directory's problems and approaches are documented as doc comments directly in the corresponding `.py` files.

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from algorithm_theory.linear_search import linear_search
from src.algorithm_theory.linear_search import linear_search


def jump_search(array: list[int], target: int) -> int | None:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from collections import deque

from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def even_odd_tree(root: TreeNode | None):
Expand Down
2 changes: 1 addition & 1 deletion binary_tree/find_mode.py → src/binary_tree/find_mode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import Counter

from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def find_mode(root: TreeNode) -> list:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def has_path_sum(root: TreeNode, target: int) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""

from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def inorder_traversal(root: TreeNode) -> list[int]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import deque

from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def level_order_traversal(root: TreeNode) -> list[list[int]]:
Expand Down
2 changes: 1 addition & 1 deletion binary_tree/max_depth.py → src/binary_tree/max_depth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def max_depth(root: TreeNode) -> int:
Expand Down
2 changes: 1 addition & 1 deletion binary_tree/min_depth.py → src/binary_tree/min_depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from collections import deque

from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def min_depth(root: TreeNode) -> int:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

"""

from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def postorder_traversal_recursive(root: TreeNode) -> list[int]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""

from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def preorder_traversal_iterative(root: TreeNode) -> list[int]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def symmetric_binary_tree(root: TreeNode | None) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def tree_from_inorder_and_postorder(inorder: list[int], postorder: list[int]) -> TreeNode:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def tree_from_preorder_inorder(preorder: list[int], inorder: list[int]) -> TreeNode:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def two_sum_iv(root: TreeNode, target: int) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from binary_tree.tree_node import TreeNode
from src.binary_tree.tree_node import TreeNode


def valid_bst(root: TreeNode, min_value=float("-inf"), max_value=float("inf")) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def insert(head: SinglyLinkedListNode | None, value: int) -> SinglyLinkedListNode | None:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.binary_tree_node import BinaryTreeNode
from src.data_structures.binary_tree_node import BinaryTreeNode


class BinaryTree:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


class CircularlyLinkedList:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.doubly_linked_list_node import DoublyLinkedListNode
from src.data_structures.doubly_linked_list_node import DoublyLinkedListNode


class DoublyLinkedList[T]:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
directly as a queue (`queue.append(1)` to enqueue, `queue.popleft()` to dequeue).
"""

from data_structures.doubly_linked_list import DoublyLinkedList
from src.data_structures.doubly_linked_list import DoublyLinkedList


class QueueList:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


class SinglyLinkedList[T]:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from collections import deque

from binary_tree.tree_node import TreeNode
from n_ary_tree.nary_tree_node import NaryTreeNode
from src.binary_tree.tree_node import TreeNode
from src.n_ary_tree.nary_tree_node import NaryTreeNode


def encode(root: NaryTreeNode) -> TreeNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from n_ary_tree.nary_tree_node import NaryTreeNode
from src.n_ary_tree.nary_tree_node import NaryTreeNode


def level_order_traversal(root: NaryTreeNode) -> list[list[int]]:
Expand Down
2 changes: 1 addition & 1 deletion n_ary_tree/max_depth.py → src/n_ary_tree/max_depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
farthest leaf node.
"""

from n_ary_tree.nary_tree_node import NaryTreeNode
from src.n_ary_tree.nary_tree_node import NaryTreeNode


def max_depth_top_down(root: NaryTreeNode) -> int:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from n_ary_tree.nary_tree_node import NaryTreeNode
from src.n_ary_tree.nary_tree_node import NaryTreeNode


def postorder_traversal[T](root: NaryTreeNode[T] | None) -> list[T]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import deque

from n_ary_tree.nary_tree_node import NaryTreeNode
from src.n_ary_tree.nary_tree_node import NaryTreeNode


def postorder_traversal_iterative[T](root: NaryTreeNode[T] | None) -> list[T]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from n_ary_tree import nary_tree_node
from src.n_ary_tree import nary_tree_node


def preorder_traversal(root: nary_tree_node) -> list:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from n_ary_tree.nary_tree_node import NaryTreeNode
from src.n_ary_tree.nary_tree_node import NaryTreeNode


def preorder_traversal_iterative(root: NaryTreeNode) -> list:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def add_two_numbers(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def delete_duplicates(head: SinglyLinkedListNode | None) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Given a linked list, return the node where the cycle begins. If there is no cycle, return None.
"""

from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def detect_cycle(head: SinglyLinkedListNode) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def find_middle_node(head: SinglyLinkedListNode | None) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def get_intersection_node(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def get_intersection_node_with_set(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
the linked list where the tail connects to. If pos is -1, then there is no cycle in the linked list.
"""

from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


# Time: O(N + K) -> O(N), Space: O(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def merge_k_lists[T](lists) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def merge_two_lists(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def odd_even_list(head: SinglyLinkedListNode | None) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from singly_linked_lists.find_middle_node import find_middle_node
from singly_linked_lists.reverse_linked_list import reverse_linked_list
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.singly_linked_lists.find_middle_node import find_middle_node
from src.singly_linked_lists.reverse_linked_list import reverse_linked_list


def is_palindrome(head: SinglyLinkedListNode | None) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def remove_elements(head: SinglyLinkedListNode | None, data: int) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def remove_nth_from_end(head: SinglyLinkedListNode, nth: int) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def reverse_linked_list(head: SinglyLinkedListNode) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def reverse_linked_list_recursive(head: SinglyLinkedListNode) -> SinglyLinkedListNode | None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_structures.singly_linked_list_node import SinglyLinkedListNode
from src.data_structures.singly_linked_list_node import SinglyLinkedListNode


def rotate_list(head: SinglyLinkedListNode | None, amount: int) -> SinglyLinkedListNode | None:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/algorithm_theory/test_binary_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from algorithm_theory.binary_search import binary_search_iterative, binary_search_recursive
from src.algorithm_theory.binary_search import binary_search_iterative, binary_search_recursive


def test_binary_search_iterative():
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithm_theory/test_bubble_sort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from algorithm_theory.bubble_sort import bubble_sort
from src.algorithm_theory.bubble_sort import bubble_sort


def test_bubble_sort():
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithm_theory/test_jump_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from algorithm_theory.jump_search import jump_search
from src.algorithm_theory.jump_search import jump_search


def test_jump_search():
Expand Down
Loading
Loading