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.1.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.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: algo_playground-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 f576b7bc85167a8d4e4f2e3ce429c576bc7fc72f397e754ba3a1e53967936730
MD5 cb01bd33d9d2b267acdc5c811d6b161b
BLAKE2b-256 0ed5cda94af41b10720969fe28ecd3f7cdc7cbedce9e782d402099e83cf070d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for algo_playground-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd9031232a050e9167fa74432a7cde527bf361eef1c31321b6c2bd6e12d1b153
MD5 79b4ae319a2c698dc9e3be3d76c4d614
BLAKE2b-256 f3c83e6eafa0e91407e690b03b59a491fcc1b87cfe90d5c6a00e3fd83aa71ae7

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