diff --git a/workspaces/adventure-pack/goodies/typescript/traversePreOrderNAry/index.test.ts b/workspaces/adventure-pack/goodies/typescript/traversePreOrderNAry/index.test.ts new file mode 100644 index 00000000..21e30acb --- /dev/null +++ b/workspaces/adventure-pack/goodies/typescript/traversePreOrderNAry/index.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, it } from "@jest/globals"; + +import { traversePreOrderNAry } from "./index.ts"; + +type Node = { val: number; children: Node[] }; + +describe("traversePreOrderNAry", () => { + it("yields nothing for null/undefined root", () => { + expect([...traversePreOrderNAry(null)]).toStrictEqual([]); + expect([...traversePreOrderNAry(undefined)]).toStrictEqual([]); + }); + + it("yields root only for single node", () => { + const root: Node = { val: 1, children: [] }; + expect([...traversePreOrderNAry(root)].map((n) => n.val)).toStrictEqual([ + 1, + ]); + }); + + it("visits parent before children", () => { + const root: Node = { + val: 1, + children: [ + { + val: 2, + children: [ + { val: 5, children: [] }, + { val: 6, children: [] }, + ], + }, + { val: 3, children: [] }, + { + val: 4, + children: [{ val: 7, children: [] }], + }, + ], + }; + expect([...traversePreOrderNAry(root)].map((n) => n.val)).toStrictEqual([ + 1, 2, 5, 6, 3, 4, 7, + ]); + }); + + it("traverses deeper trees in pre-order", () => { + const root: Node = { + val: 1, + children: [ + { + val: 2, + children: [ + { val: 4, children: [] }, + { val: 5, children: [] }, + ], + }, + { val: 3, children: [] }, + ], + }; + expect([...traversePreOrderNAry(root)].map((n) => n.val)).toStrictEqual([ + 1, 2, 4, 5, 3, + ]); + }); +}); diff --git a/workspaces/adventure-pack/goodies/typescript/traversePreOrderNAry/index.ts b/workspaces/adventure-pack/goodies/typescript/traversePreOrderNAry/index.ts new file mode 100644 index 00000000..9091473f --- /dev/null +++ b/workspaces/adventure-pack/goodies/typescript/traversePreOrderNAry/index.ts @@ -0,0 +1,18 @@ +export function* traversePreOrderNAry( + root: T | null | undefined, +): Generator { + if (root == null) { + return; + } + + const stack: T[] = [root]; + + do { + const node = stack.pop()!; + yield node; + // TODO: add an Array.prototype.valuesReversed() goody and use it here + for (let i = node.children.length - 1; i >= 0; --i) { + stack.push(node.children[i]); + } + } while (stack.length > 0); +} diff --git a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap index f7a3641e..9790af51 100644 --- a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap +++ b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap @@ -2724,6 +2724,31 @@ function* traversePreOrder(root) { /////////////////////////// END ADVENTURE PACK CODE ////////////////////////////" `; +exports[`App can equip single goody: JavaScript traversePreOrderNAry 1`] = ` +"////////////////////////// BEGIN ADVENTURE PACK CODE /////////////////////////// +// Adventure Pack commit fake-commit-hash +// Running at: https://example.com/ + +function* traversePreOrderNAry(root) { + if (root == null) { + return; + } + + const stack = [root]; + + do { + const node = stack.pop(); + yield node; + // TODO: add an Array.prototype.valuesReversed() goody and use it here + for (let i = node.children.length - 1; i >= 0; --i) { + stack.push(node.children[i]); + } + } while (stack.length > 0); +} + +/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////" +`; + exports[`App can equip single goody: Kotlin gcd(Int,Int) 1`] = ` "////////////////////////// BEGIN ADVENTURE PACK CODE /////////////////////////// // Adventure Pack commit fake-commit-hash @@ -5509,3 +5534,30 @@ function* traversePreOrder< /////////////////////////// END ADVENTURE PACK CODE ////////////////////////////" `; + +exports[`App can equip single goody: TypeScript traversePreOrderNAry 1`] = ` +"////////////////////////// BEGIN ADVENTURE PACK CODE /////////////////////////// +// Adventure Pack commit fake-commit-hash +// Running at: https://example.com/ + +function* traversePreOrderNAry( + root: T | null | undefined, +): Generator { + if (root == null) { + return; + } + + const stack: T[] = [root]; + + do { + const node = stack.pop()!; + yield node; + // TODO: add an Array.prototype.valuesReversed() goody and use it here + for (let i = node.children.length - 1; i >= 0; --i) { + stack.push(node.children[i]); + } + } while (stack.length > 0); +} + +/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////" +`; diff --git a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap index f0a6e8f0..e5a54523 100644 --- a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap +++ b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap @@ -1499,6 +1499,25 @@ exports[`App can render goody: JavaScript traversePreOrder 1`] = ` }" `; +exports[`App can render goody: JavaScript traversePreOrderNAry 1`] = ` +"export function* traversePreOrderNAry(root) { + if (root == null) { + return; + } + + const stack = [root]; + + do { + const node = stack.pop(); + yield node; + // TODO: add an Array.prototype.valuesReversed() goody and use it here + for (let i = node.children.length - 1; i >= 0; --i) { + stack.push(node.children[i]); + } + } while (stack.length > 0); +}" +`; + exports[`App can render goody: Kotlin gcd(Int,Int) 1`] = ` "package gcd_int_int @@ -3159,3 +3178,24 @@ exports[`App can render goody: TypeScript traversePreOrder 1`] = ` } while (stack.length > 0); }" `; + +exports[`App can render goody: TypeScript traversePreOrderNAry 1`] = ` +"export function* traversePreOrderNAry( + root: T | null | undefined, +): Generator { + if (root == null) { + return; + } + + const stack: T[] = [root]; + + do { + const node = stack.pop()!; + yield node; + // TODO: add an Array.prototype.valuesReversed() goody and use it here + for (let i = node.children.length - 1; i >= 0; --i) { + stack.push(node.children[i]); + } + } while (stack.length > 0); +}" +`;