From b0f24f98eeaccec4f365422910eae11632faaefb Mon Sep 17 00:00:00 2001 From: pearmini Date: Tue, 9 Dec 2025 10:44:47 -0500 Subject: [PATCH 1/2] Add example stack --- app/examples/binary-search.recho.js | 4 +- app/examples/stack.recho.js | 96 +++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 app/examples/stack.recho.js diff --git a/app/examples/binary-search.recho.js b/app/examples/binary-search.recho.js index c7ac7be..f1da7f1 100644 --- a/app/examples/binary-search.recho.js +++ b/app/examples/binary-search.recho.js @@ -2,8 +2,8 @@ * @title Binary Search * @author Bairui Su * @github pearmini - * @created 2025-12-09 - * @label Algorithm, Beginner + * @created 2025-12-08 + * @label Algorithm * @pull_request 200 */ diff --git a/app/examples/stack.recho.js b/app/examples/stack.recho.js new file mode 100644 index 0000000..d2e3e0f --- /dev/null +++ b/app/examples/stack.recho.js @@ -0,0 +1,96 @@ +/** + * @title Stack + * @author Bairui Su + * @github pearmini + * @created 2025-12-09 + * @label Algorithm + * @thumbnail_start 20 + */ + +/** + * ============================================================================ + * = Stack = + * ============================================================================ + * + * This example demonstrates a classic Stack data structure with push and pop + * operations. A stack follows the LIFO (Last In, First Out) principle. The + * following is a simple demonstration of how it looks like. + */ + +const [stack, setStack] = recho.state(new Stack()); + +//➜ "gol\\hRXCro^^suA" +echo(stack.items.join("")); + +// Push button - adds a random alphabet character to the stack +recho.button("Push", push); + +// Pop button - removes the top element from the stack +recho.button("Pop", pop); + +/** + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Implementation + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * The stack is implemented as a class with a few methods: + * + * - `push(element)`: adds an element to the stack + * - `pop()`: removes the top element from the stack + * - `clone()`: creates a deep copy of the stack + * + */ + +class Stack { + constructor() { + this.items = []; + } + + push(element) { + this.items.push(element); + } + + pop() { + if (this.items.length === 0) return undefined; + return this.items.pop(); + } + + clone() { + const cloned = new Stack(); + cloned.items = [...this.items]; + return cloned; + } +} + +/** + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Helper Functions + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * The following are helper functions to push and pop elements from the stack, + * and a function to generate a random alphabet character (A-z). + */ + +function push() { + setStack((s) => { + const cloned = s.clone(); + cloned.push(randomAlphabet()); + return cloned; + }); +} + +function pop() { + setStack((s) => { + const cloned = s.clone(); + cloned.pop(); + return cloned; + }); +} + +// Function to generate a random alphabet character (A-z) +function randomAlphabet() { + // Generate random character from A (65) to z (122) + // This includes A-Z (65-90), some special chars (91-96), and a-z (97-122) + const code = Math.floor(Math.random() * (122 - 65 + 1)) + 65; + return String.fromCharCode(code); +} From 607e1798ccc066e9d647c412096ed078602e7e5f Mon Sep 17 00:00:00 2001 From: pearmini Date: Tue, 9 Dec 2025 10:45:58 -0500 Subject: [PATCH 2/2] Update pull request --- app/examples/stack.recho.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/examples/stack.recho.js b/app/examples/stack.recho.js index d2e3e0f..5695a0b 100644 --- a/app/examples/stack.recho.js +++ b/app/examples/stack.recho.js @@ -5,6 +5,7 @@ * @created 2025-12-09 * @label Algorithm * @thumbnail_start 20 + * @pull_request 201 */ /**