Skip to content

sjnam/gweb-examples

Repository files navigation

GWEB examples

Building

A Makefile builds any example by its name (the .w basename):

make ziptree   # ziptree.w -> ziptree.go (+ any @( ) files) + ziptree.pdf
make           # show usage
make clean     # remove all generated files, keeping the .w/.ch sources

Build one example at a time (there is no make all): nearly every tangled .go is a package main with its own main(), so emitting them into one directory at once would make Go refuse to compile (main redeclared).

The TeX engine is chosen automatically — luatex for Korean documents (those with \input kotexgweb.tex, see below), pdftex otherwise.

  • convmod.w — Library Checker's Convolution (mod 998244353): polynomial multiplication in O(n log n) via the number theoretic transform (NTT). Korean (typeset with luatex).
  • fast_cancel.w — It shows a complementary pattern useful in any concurrent Go program: how to propagate a first-error signal to all sibling goroutines cleanly.
  • floyd.w — Floyd's partition problem, the classic "toy problem" Knuth discusses in Are Toy Problems Useful?: partition √1…√50 into two nearly-equal halves. A worked literate solution (meet-in-the-middle search, Gray-code enumeration, compensated summation, and a math/big verification).
  • pairsums.w — HackerRank's Pair Sums: the largest pair-product sum over all subarrays. The identity value = (S²−Q)/2 and a prefix-sum twist turn it into the upper envelope of a family of lines, solved with a Li Chao tree in O(n log n).
  • pipeline.w — a tutorial that bridges Go's two pipeline worlds: lazy iter.Seq transforms and a fan-out of channel workers, joined by two boundary adapters, with first-error cancellation flowing across both. Uses range-over-func and a pocket errgroup.
  • poison.w — HackerRank's Poisonous Plants: how many days until no plant dies, in O(n) with an increasing stack that gives each plant its day of death. Korean (typeset with luatex).
  • pqundo.w — the priority-queue undo trick: a rollback DSU extended to delete the max-priority element from the middle of its update stack in amortized O(log²n), applied to Codeforces 603E. Korean (typeset with luatex).
  • prjeuler152.w — Project Euler Problem 152, The key challenge—and the appeal—is that you cannot compare the sums using floating-point arithmetic. When adding the $1/n^2$ terms, precise rational number operations are required, and a brute-force approach that simply cycles through all $2^{79}$ subsets is impossible.
  • runningmedian.w — HackerRank's Find the Running Median: the median of a growing stream, kept in O(log n) per value with two heaps (a max-heap and a min-heap). Korean (typeset with luatex).
  • seq.w — a tiny lazy-sequence library (Map, Filter, Take over infinite Fibonacci numbers), showing off the Go features C has no answer to: first-class functions and closures, anonymous functions, generics, and Go 1.23 range-over-func iterators.
  • sham.w — a GWEB port of Knuth's Stanford GraphBase demo sham: count the symmetric Hamiltonian cycles of the knight's graph on an 8×9 board, by folding the graph in half and backtracking with goto labels. It builds on go-sgb, a Go port of the SGB, so running it needs that module (go get github.com/sjnam/go-sgb); the commentary is newly written. Shows GWEB handling an external dependency and a real Knuth program.
  • slidingmax.w — LeetCode's Sliding Window Maximum, solved in O(n) with a monotonic deque. A Korean literate essay (typeset with luatex, like hangul.w).
  • squint.w — lazy power series as demand-driven channel networks (sum, product, composition, reciprocal, functional inverse, and differential equations like exp), after McIlroy's Squinting at Power Series.
  • suffixautomaton.w — an exposition of the suffix automaton (after cp-algorithms): the online O(n) construction with suffix links and the clone/split step, applied to counting distinct substrings. Korean (typeset with luatex).
  • topswops.w — Conway's topswops game, solved by A. Pepperdine's backward search (run the game in reverse from its ending state). An essay-style port of Knuth's CWEB topswops.w.
  • topswops_fwd.w — the same game solved forwards: a branch-and-bound search with placeholder cards and an f(m) pruning bound, written as a goto state machine. A port of Knuth's CWEB topswops-fwd.w.
  • trucktour.w — HackerRank's Truck Tour (the circular gas-station problem), solved in O(n) by a greedy single pass; the essay explains why one pass suffices. Korean (typeset with luatex).
  • waiter.w — HackerRank's Waiter: a stack simulation that splits plates by successive primes. A Korean literate essay (typeset with luatex).
  • wc.w — a literate word-count program; its tangled output matches the system wc. It also shows @f setting a user type in bold.
  • ziptree.w — the zip tree of Tarjan, Levy, and Timmel: a randomized BST that is max-heap-ordered by a geometric random rank (ties favoring the smaller key), updated by unzipping and zipping search paths instead of rotations. Implements the paper's recursive insert, zip, and delete as a library, then derives two memory-savers: an arena (index-based, byte rank, free list) that halves node size and cuts a million-node build from ~1M allocations to ~40, and a pseudo-random rank variant that drops the rank field entirely (computing it from the key). The @(zip_test.go@> test file (run with go test) reproduces the paper's Figure 1 exactly, cross-checks all three representations, fuzz-checks the BST/heap invariants, and benchmarks allocations. Korean (typeset with luatex).

Korean (and other non-English) documentation

The woven output can be written in Korean by processing it with luatex (not pdftex) and putting one line in the .w file's limbo:

\input kotexgweb.tex

tex/kotexgweb.tex loads luatexko and selects the Noto Serif/Sans CJK KR fonts (edit the \sethangulfont lines to change typefaces), translates gweave's fixed wording into Korean, and supplies a LuaTeX PDF back end so that blue cross-reference links and the PDF outline (bookmark) pane work, with Korean bookmark titles. Then:

gweave foo.w           # -> foo.tex
luatex foo.tex         # -> foo.pdf   (kotexgweb.tex on TEXINPUTS)

gweave needs no flag; all the human-readable text it emits goes through macros (\GU, \GNused, \Gsectionword, …) that kotexgweb.tex overrides, so the same mechanism localizes to any language — write your own \input file modelled on it.

About

GWEB Examples

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors