To help any AI coding agent understand and contribute effectively to The Algorithm Codex, we will create an AGENT.md file. This document outlines the project's unique "literate programming" architecture and established coding standards.
Welcome to The Algorithm Codex. This document provides the necessary context for AI agents to contribute code and prose while maintaining the project's integrity.
The primary source of truth for this project is the documentation located in docs/. We use a tool called illiterate to extract code from Markdown files and generate the Python source and test suites.
To export a block of code to a specific file, use the following syntax in Markdown:
```python {export=path/to/target_file.py}
# Your code here
```- Extraction: Running
make sourcetriggersilliterate, which scans thedocs/directory and writes the code blocks to their respective paths insrc/ortests/. - Append Mode: Multiple blocks can export to the same file;
illiteratewill append them in the order they appear in the documentation. - Constraint: Never edit files in
src/ortests/directly. Always modify the corresponding.mdfile indocs/and runmake sourceto sync the changes.
-
docs/: Contains the narrative and the source code (in Markdown). -
index.qmd: The book's preface and roadmap. -
01_search.md, etc.: Individual chapters. -
src/codex/: The generated Python package. -
types.py: Contains core protocols likeOrdering[T]anddefault_order. -
tests/: The generated test suite. -
makefile: Usemake sourceto generate code,make teststo runpytest, andmake docsto render the book.
- Python Version: Use Python 3.13+ features, including modern generic syntax (e.g.,
def func[T](...)). - Type Hinting: All functions must be fully typed. Use
Sequenceinstead oflistfor input parameters to remain generic. - Ordering Protocol: For any algorithm involving comparisons, use the
Ordering[T]type alias anddefault_orderprovided incodex.types. - Functional Style: Prefer pure functions and minimal class usage. Classes should primarily serve as simple data stores.
The goal of the Codex is to build intuition before implementation.
- The "Why" First: Before presenting code, explain the problem and the core intuition behind the solution.
- Back-of-the-envelope Analysis: Provide high-level time and space complexity analysis (, , etc.) for every major algorithm.
- Simplicity over Micro-optimization: Avoid language-specific hacks or loop unrolling. Focus on algorithmic efficiency—speed should come from the algorithm's structure, not runtime tricks.
- Incremental Learning: Introduce simpler concepts first and use them as building blocks for more complex chapters.
- Identify the target chapter in
docs/or create a new one. - Write the narrative explanation in Markdown.
- Insert code blocks with the
{export=...}attribute for both the implementation and its corresponding test. - Run
make sourceto generate the.pyfiles. - Run
make teststo verify the implementation. - Update
_quarto.ymlif a new chapter was added.