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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ or cell with the same `data-*` attributes, so give each clickable one a stable
`data-*` key (`data-set`, `data-i`). The part 1 widget uses `presetChips(sets, active)` for its chips
(the active one is `aria-pressed`, which lesson.css draws as selected; a
widget's own toggle chips should set it too) and `widgetLabel(text)` for the
note beside its heading. Anything a lesson draws uses a class from `lesson.css`, `kit.css`
note beside its heading. It ends with `wireWidget(host, { render, choose })`,
which draws it now and on every language change, calls `choose(i)` then
`render()` when chip `i` is clicked, and turns Enter or space on a
`role="button"` cell into a click, so the widget has one click listener and no
keydown of its own. Anything a lesson draws uses a class from `lesson.css`, `kit.css`
or its own `style.css`; never a `style="…"` string (a width that varies can be
a class per step, as x-sum's tally bars are). In `page.js`'s cost table, the
gloss under an approach's name is `<span class="sub">`. A class a second lesson would
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ boxes of your own inside the player.
Shapes for the stage are in `src/lib/stage.js` (`cells`, `kv`, `stack`,
`chain`, `tree`, `bars`, `readout`). Small helpers are in `src/lib/kit.js` —
including `intList` / `intValue` for a control's `parse`, and `presetChips` /
`widgetLabel` for the widget — and LeetCode binary trees in `src/lib/tree.js`. Use their classes, never a
`widgetLabel` / `wireWidget` for the widget — and LeetCode binary trees in `src/lib/tree.js`. Use their classes, never a
`style="…"`; a class only your page needs goes in its own `style.css`. Words
and calls get `cells(items, { wide: true })` and a board or matrix
`labelledRows(rows, { grid: true })`, so neither needs CSS of its own.
Expand Down
13 changes: 3 additions & 10 deletions src/lessons/3sum/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* a two-pointer squeeze, where equal neighbours can simply be stepped over.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, kv, readout, slots, stagePanel } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, intList, presetChips, widgetLabel } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js';

const MAX_LEN = 10;

Expand Down Expand Up @@ -535,17 +535,10 @@ function mountTripletWidget(host) {
render();
}
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (chip) { state.set = Number(chip.dataset.set); state.sel = [0, 1, 2]; return render(); }
const cell = ev.target.closest('[data-i]');
if (cell) toggle(Number(cell.dataset.i));
});
host.addEventListener('keydown', (ev) => {
const cell = ev.target.closest('[data-i]');
if (cell && (ev.key === 'Enter' || ev.key === ' ')) { ev.preventDefault(); toggle(Number(cell.dataset.i)); }
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { state.set = i; state.sel = [0, 1, 2]; } });
}

/* ---------------- the approach, in brief ---------------- */
Expand Down
9 changes: 3 additions & 6 deletions src/lessons/basic-calculator/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* recursive parser), or an explicit stack of (total, sign) pairs can.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, slots, stagePanel, stack, readout } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js';

const MAX_LEN = 24;

Expand Down Expand Up @@ -528,8 +528,6 @@ function mountSignWidget(host) {
q('[data-total]').innerHTML = `${total}<small>${pick(t('value', 'တန်ဖိုး'))}</small>`;
}
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (chip) { state.set = Number(chip.dataset.set); state.s = QW_SETS[state.set].s; return render(); }
const op = ev.target.closest('[data-flip]');
if (op) {
const i = Number(op.dataset.flip);
Expand All @@ -541,8 +539,7 @@ function mountSignWidget(host) {
host.querySelector(`[data-flip="${i}"]`)?.focus();
}
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { state.set = i; state.s = QW_SETS[state.set].s; } });
}

/* ---------------- the approach, in brief ---------------- */
Expand Down
20 changes: 9 additions & 11 deletions src/lessons/best-time-to-buy-and-sell-stock/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* after the buy, which is the whole reason the answer is not
* max(prices) - min(prices).
*/
import { t, plural, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel } from '../../lib/kit.js';
import { t, plural, exampleTitle, LANGUAGES, k, c, intList, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js';
import { mountLesson } from '../../lib/stepper.js';
import { bars, cells, slots, stagePanel } from '../../lib/stage.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';

/* A bilingual string: the { en, my } pair pick() reads at render time. */

Expand Down Expand Up @@ -528,16 +528,14 @@ function mountTradeWidget(host) {
else return;
render();
});
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (!chip) return;
state.set = Number(chip.dataset.set);
const { pair } = bestTrade(QW_SETS[state.set].prices);
[state.buy, state.sell] = pair ?? [0, 1];
render();
wireWidget(host, {
render,
choose: (i) => {
state.set = i;
const { pair } = bestTrade(QW_SETS[state.set].prices);
[state.buy, state.sell] = pair ?? [0, 1];
},
});
onLangChange(render);
render();
}

/* ---------------- the approach, in brief ----------------
Expand Down
11 changes: 3 additions & 8 deletions src/lessons/binary-tree-maximum-path-sum/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* parent and records the bend on the way back: O(n).
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, tree, stack, readout, panels, slots, stagePanel } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js';
import { buildTree, levelOrder, asNested, preorderKeys, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js';

const MAX_NODES = 15;
Expand Down Expand Up @@ -448,12 +448,7 @@ function mountTopWidget(host) {
q('[data-total]').innerHTML = `${best}<small>${pick(t('best path', 'အကောင်းဆုံး'))}</small>`;
}
host.addEventListener('input', (ev) => { if (ev.target.id === 'mps-b') { state.b = Number(ev.target.value) - 1; render(); } });
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (chip) { state.set = Number(chip.dataset.set); state.b = 0; render(); }
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { state.set = i; state.b = 0; } });
}

function atLeastOne(text) {
Expand Down
7 changes: 3 additions & 4 deletions src/lessons/climbing-stairs/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* variables walking up the stairs are all the memory the problem needs.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, stack, bars, readout, panels, slots, stagePanel } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, intValue, widgetLabel } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageGap, intValue, widgetLabel, wireWidget } from '../../lib/kit.js';

/* ten steps is 177 calls in the plain recursion — 441 frames, which is as far
* as stepping by hand stays worth it */
Expand Down Expand Up @@ -464,8 +464,7 @@ function mountLastMoveWidget(host) {
state.n = Number(chip.dataset.n);
render();
});
onLangChange(render);
render();
wireWidget(host, { render });
}

/* ---------------- the approach, in brief ----------------
Expand Down
14 changes: 3 additions & 11 deletions src/lessons/coin-change/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* so every answer it needs is already written down.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, kv, stack, readout, slots, stagePanel } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js';

const MAX_AMOUNT = 11;

Expand Down Expand Up @@ -434,15 +434,7 @@ function mountGreedyWidget(host) {
if (ev.target.id !== 'qw-amt') return;
state.amount = Number(ev.target.value); render();
});
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (!chip) return;
const set = QW_SETS[Number(chip.dataset.set)];
Object.assign(state, { set: Number(chip.dataset.set), amount: set.amount });
render();
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { const set = QW_SETS[i]; Object.assign(state, { set: i, amount: set.amount }); } });
}

/* ---------------- the approach, in brief ---------------- */
Expand Down
18 changes: 3 additions & 15 deletions src/lessons/course-schedule/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* nothing left to wait for and checks whether it ran out early.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, stagePanel, stack, readout } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, verdictAnswer, intValue, widgetLabel, presetChips } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, labelledRows, stageRow, stageGap, verdictAnswer, intValue, widgetLabel, presetChips, wireWidget } from '../../lib/kit.js';

const MAX_N = 8, MAX_PAIRS = 12;

Expand Down Expand Up @@ -601,22 +601,10 @@ function mountArrowWidget(host) {
render();
}
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (chip) { load(Number(chip.dataset.set)); return render(); }
const node = ev.target.closest('[data-u]');
if (node) tap(Number(node.dataset.u));
});
host.addEventListener('keydown', (ev) => {
const node = ev.target.closest('[data-u]');
if (node && (ev.key === 'Enter' || ev.key === ' ')) {
ev.preventDefault();
const u = Number(node.dataset.u);
tap(u);
host.querySelector(`[data-u="${u}"]`)?.focus();
}
});
onLangChange(render);
render();
wireWidget(host, { render, choose: load });
}

/* ---------------- the approach, in brief ---------------- */
Expand Down
14 changes: 3 additions & 11 deletions src/lessons/diameter-of-binary-tree/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* bottom-up, and records the bend on the way back.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, tree, stack, readout, panels, slots, stagePanel } from '../../lib/stage.js';
import { t, plural, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js';
import { t, plural, exampleTitle, LANGUAGES, k, c, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js';
import { buildTree, levelOrder, asNested, preorderKeys, treeDepth, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js';

const MAX_NODES = 15;
Expand Down Expand Up @@ -551,15 +551,7 @@ function mountBendWidget(host) {
if (ev.target.id !== 'qw-b') return;
state.b = Number(ev.target.value) - 1; render();
});
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (!chip) return;
state.set = Number(chip.dataset.set);
state.b = 0;
render();
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { state.set = i; state.b = 0; } });
}

/* The constraints start at one node, and a diameter needs somewhere to bend. */
Expand Down
11 changes: 3 additions & 8 deletions src/lessons/find-median-from-data-stream/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*/
import { mountLesson } from '../../lib/stepper.js';
import { heapNested } from '../../lib/tree.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, slots, stagePanel, tree, readout, panels } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js';

const MAX_OPS = 12;

Expand Down Expand Up @@ -550,12 +550,7 @@ function mountHalvesWidget(host) {
q('[data-total]').innerHTML = `${fmt(med)}<small>${pick(t('median', 'median'))}</small>`;
}
host.addEventListener('input', (ev) => { if (ev.target.id === 'mf-n') { state.n = Number(ev.target.value); render(); } });
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (chip) { state.set = Number(chip.dataset.set); state.n = QW_SETS[state.set].nums.length; render(); }
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { state.set = i; state.n = QW_SETS[state.set].nums.length; } });
}

/* ---------------- the approach, in brief ---------------- */
Expand Down
11 changes: 3 additions & 8 deletions src/lessons/implement-trie-prefix-tree/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* but has to look at every word for startsWith.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, slots, stagePanel, readout, trieOutline } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, wireWidget } from '../../lib/kit.js';

const MAX_OPS = 8, MAX_LEN = 6;
const NAMES = { insert: 'insert', search: 'search', startsWith: 'startsWith' };
Expand Down Expand Up @@ -574,12 +574,7 @@ function mountPrefixWidget(host) {
q('[data-total]').innerHTML = `${hits.length}<small>${pick(t('words match', 'word ကိုက်'))}</small>`;
}
input.addEventListener('input', () => { state.prefix = input.value.toLowerCase().replace(/[^a-z]/g, '').slice(0, 6); render(); });
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (chip) { state.set = Number(chip.dataset.set); state.prefix = QW_SETS[state.set].words[0].slice(0, 2); render(); }
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { state.set = i; state.prefix = QW_SETS[state.set].words[0].slice(0, 2); } });
}

/* ---------------- the approach, in brief ---------------- */
Expand Down
14 changes: 3 additions & 11 deletions src/lessons/invert-binary-tree/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* the stack and the queue beside it are the lesson.
*/
import { mountLesson } from '../../lib/stepper.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, tree, stack, panels, slots, stagePanel } from '../../lib/stage.js';
import { t, plural, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js';
import { t, plural, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js';
import { buildTree, levelOrder, asNested, treeDepth, copyKids as copy, nameOf, treeInput, formatLevelOrder } from '../../lib/tree.js';

/* The tree helpers — parsing LeetCode's level order, drawing, serializing —
Expand Down Expand Up @@ -460,15 +460,7 @@ function mountMirrorWidget(host) {
if (ev.target.id !== 'qw-k') return;
state.k = Number(ev.target.value); render();
});
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (!chip) return;
state.set = Number(chip.dataset.set);
state.k = 0;
render();
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { state.set = i; state.k = 0; } });
}

/* ---------------- the approach, in brief ----------------
Expand Down
14 changes: 3 additions & 11 deletions src/lessons/kth-largest-element-in-an-array/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/
import { mountLesson } from '../../lib/stepper.js';
import { heapNested } from '../../lib/tree.js';
import { pick, onLangChange } from '../../lib/i18n.js';
import { pick } from '../../lib/i18n.js';
import { cells, tree, readout, slots, stagePanel } from '../../lib/stage.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel, stageEmpty } from '../../lib/kit.js';
import { t, exampleTitle, LANGUAGES, k, c, stageRow, stageGap, intList, intValue, presetChips, widgetLabel, stageEmpty, wireWidget } from '../../lib/kit.js';

const MAX_LEN = 10;

Expand Down Expand Up @@ -382,15 +382,7 @@ function mountRankWidget(host) {
if (ev.target.id !== 'qw-k') return;
state.k = Number(ev.target.value); render();
});
host.addEventListener('click', (ev) => {
const chip = ev.target.closest('[data-set]');
if (!chip) return;
const set = QW_SETS[Number(chip.dataset.set)];
Object.assign(state, { set: Number(chip.dataset.set), k: set.k });
render();
});
onLangChange(render);
render();
wireWidget(host, { render, choose: (i) => { const set = QW_SETS[i]; Object.assign(state, { set: i, k: set.k }); } });
}

/* ---------------- the approach, in brief ---------------- */
Expand Down
Loading