Skip to main content

Interactive terminal playground for classic algorithms (sorting, searching, graphs)

Project description

algo-playground

Interactive terminal playground for classic algorithms (sorting, searching, graphs).

  • Beginner-friendly, step-by-step visualizations in your terminal using Rich
  • CLI to list and run algorithms with adjustable speed and data size
  • Pure, type-hinted Python library you can import and reuse

Installation

pip install algo-playground

Python 3.9+ is required.

Quick Start

  • List algorithms:
algo-playground list
  • Run quick sort on 20 elements with visualization:
algo-playground run quick_sort --size 20 --visual --speed 0.05
  • Binary search over a generated sorted list:
algo-playground run binary_search --size 30 --target 15
  • BFS traversal on a generated graph (shows a quick graph drawing, then a live adjacency view):
algo-playground run bfs --size 8 --visual --speed 0.08

CLI Reference

The CLI provides two commands: list and run.

  • List available algorithms
algo-playground list
  • Run an algorithm
algo-playground run <algorithm> [--size N] [--visual] [--speed S] [--target T] [--seed SEED]
  • Common options

  • --size: dataset size (array length or number of nodes). Default 20

  • --visual: enable step-by-step, colored animations in the terminal

  • --speed: delay between visual steps in seconds (e.g. 0.02, 0.1). Default 0.02

  • --seed: seed for reproducible random data

  • Searching only: --target: the value to find (picked randomly if omitted)

  • Supported algorithms

  • Sorting: bubble_sort, merge_sort, quick_sort

  • Searching: linear_search, binary_search

  • Graph: bfs, dfs, dijkstra

Examples

# Sorting
algo-playground run bubble_sort --size 15 --visual --speed 0.04
algo-playground run merge_sort --size 25
algo-playground run quick_sort --size 20 --visual

# Searching
algo-playground run linear_search --size 20 --target 7 --visual --speed 0.03
algo-playground run binary_search --size 30 --target 15 --visual

# Graphs
algo-playground run bfs --size 10 --visual --speed 0.08
algo-playground run dfs --size 10 --visual
algo-playground run dijkstra --size 10 --visual

Visualizations

  • Sorting

  • Colored bars represent values

  • Yellow = compare, Red = swap, Blue = normal

  • Indices are shown above bars, with step/phase status

  • Searching

  • Array is shown as bars

  • For binary search, pointers lo, mid, hi are labeled under the array

  • Step status shows progress and results

  • Graphs

  • A quick ASCII drawing places nodes around a circle with arrows indicating edge directions

  • Then an interactive live view displays the adjacency list, highlighting the current and visited nodes

Tip: Increase --speed for slower, more guided visuals.

Python Library Usage

You can import and reuse the algorithms as pure functions.

Sorting

from algo_playground.algorithms.sorting import bubble_sort, merge_sort, quick_sort

arr = [5, 3, 8, 2]
sorted_arr, comparisons = quick_sort(arr)
print(sorted_arr)  # [2, 3, 5, 8]
print(comparisons) # comparisons performed

Searching

from algo_playground.algorithms.searching import linear_search, binary_search

arr = [1, 3, 5, 7, 9]
print(linear_search(arr, 7))  # 3
print(binary_search(arr, 5))  # 2

Graphs

from algo_playground.algorithms.graphs import bfs, dfs, dijkstra

graph = {
    "A": ["B", "C"],
    "B": ["D"],
    "C": ["D"],
    "D": [],
}
print(bfs(graph, "A"))  # ['A', 'B', 'C', 'D']
print(dfs(graph, "A"))  # ['A', 'B', 'D', 'C']

weighted = {
    "A": [("B", 2), ("C", 5)],
    "B": [("C", 1), ("D", 4)],
    "C": [("D", 1)],
    "D": [],
}
print(dijkstra(weighted, "A"))  # {'A':0, 'B':2, 'C':3, 'D':4}

Tracing Hooks (Advanced)

Sorting algorithms can emit fine-grained steps for visualization. You can attach a callback:

from algo_playground.algorithms.sorting import quick_sort, SortStep

arr = [4, 2, 6]

def on_step(step: SortStep) -> None:
    # inspect step.array, step.compare_indices, step.swap_indices
    pass

sorted_arr, comparisons = quick_sort(arr, trace=True, on_step=on_step)

FAQ

  • The algo-playground command isn’t found
    • Ensure your virtualenv is active, or add ~/.local/bin to your PATH if using --user
  • Visuals flicker or look odd in some terminals
    • Try a TrueColor-capable terminal and a monospaced font
  • Graph drawing looks crowded
    • Reduce --size or increase terminal width; the live adjacency view remains clear

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

algo_playground-0.1.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

algo_playground-0.1.2-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file algo_playground-0.1.2.tar.gz.

File metadata

  • Download URL: algo_playground-0.1.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for algo_playground-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3b13c00d81e7a6e67a463c192efd78f7b524ccaf7fb72b9866c40f1f1e90292a
MD5 86a224d93e4149fbf3c2ad2ef2a913ec
BLAKE2b-256 77823a6485ac27e97256caab77e4caff933686d7da4149162f577f49e9d94572

See more details on using hashes here.

File details

Details for the file algo_playground-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for algo_playground-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3d95c46686888210fdebbf32ea222cfd77723533943bf54a36660c13e49b2a53
MD5 7effc383fe6a6d6b98d9264680cc5a4c
BLAKE2b-256 becf2d9ec54caf4ef4b3f066b9bdec99336aefda4d5435e81c5ceeeb7715bda0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page