aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2025-11-11 22:51:38 +0800
committer冯昶 <fengchang@novel-supertv.com>2025-11-11 22:51:38 +0800
commit0f16a24e565a6f69414dd5094df8a1d3a732e457 (patch)
tree05f5fa2dbc4fe9fada81cc95d7d48323c10ad211
parentf8883a3960b1aeadd2aa696f44335742861475ed (diff)
downloadperlweeklychallenge-club-0f16a24e565a6f69414dd5094df8a1d3a732e457.tar.gz
perlweeklychallenge-club-0f16a24e565a6f69414dd5094df8a1d3a732e457.tar.bz2
perlweeklychallenge-club-0f16a24e565a6f69414dd5094df8a1d3a732e457.zip
blogs for challenge 346 tasks
-rw-r--r--.gitignore3
-rw-r--r--challenge-346/feng-chang/raku/ch-1.ipynb137
-rwxr-xr-xchallenge-346/feng-chang/raku/ch-1.raku2
-rw-r--r--challenge-346/feng-chang/raku/ch-2.ipynb181
4 files changed, 322 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index f96416b7dc..b4eb3ffade 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,6 +26,9 @@ _Inline
# Rust languageoutput directory
target/
+# Jupyter notebook directory
+.ipynb_checkpoints/
+
# BBEdit project files
.bbprojectd
diff --git a/challenge-346/feng-chang/raku/ch-1.ipynb b/challenge-346/feng-chang/raku/ch-1.ipynb
new file mode 100644
index 0000000000..3888db8882
--- /dev/null
+++ b/challenge-346/feng-chang/raku/ch-1.ipynb
@@ -0,0 +1,137 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "5ae71ca6-6570-4e52-b297-73b3e7cc38bd",
+ "metadata": {},
+ "source": [
+ "# PWC346, Task 1 Longest Parenthesis\n",
+ "**冯昶 2025-11-11**\n",
+ "\n",
+ "First, Let's define a self referencing regex Parens:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "a63dab4e-e213-4bbb-84d9-d7fe05df6caa",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(())\n",
+ "(())\n"
+ ]
+ }
+ ],
+ "source": [
+ "my token Parens { '(' <&Parens>* ')' };\n",
+ "put '(())'.match(/<Parens>/).Str;\n",
+ "put ')(())('.match(/<Parens>/).Str;"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e15e74bc-2025-41a8-abea-52c05dab5805",
+ "metadata": {},
+ "source": [
+ "The match result can be both fully-matched and partially-matched."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "ccbe139b-7204-448f-a67d-788c1ee5c95f",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "()()\n"
+ ]
+ }
+ ],
+ "source": [
+ "my token Parens { '(' <&Parens>* ')' };\n",
+ "put '()()'.match(/<Parens>+/).Str;"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "948ee1d4-faa7-4b09-8e18-a0ddeb1485fc",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "Adding a '+' sign at the end make it match valid parenthesis consequnces."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "09a5f174-173b-4ec4-87a2-1820c9b2490f",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(\"()()\", \"()\", \"()\", \"((()))\", \"(())\", \"()\")\n"
+ ]
+ }
+ ],
+ "source": [
+ "my token Parens { '(' <&Parens>* ')' };\n",
+ "put '()()|((()))'.match(/<Parens>+/, :ex)».Str.raku;"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "431ad41f-4ceb-4c03-bb32-01d3f3a19bea",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "Adding a ':ex' argument(which is 'exhaustively', and it is a adverb for the match) make it match all possible matches.\n",
+ "We finally come to the solution:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "c7d4b365-3713-47f7-9801-3c2d4f966dca",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "6\n"
+ ]
+ }
+ ],
+ "source": [
+ "my token Parens { '(' <&Parens>* ')' };\n",
+ "put '()((())()'.match(/<Parens>+/, :ex)».chars.max;"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Raku",
+ "language": "raku",
+ "name": "raku"
+ },
+ "language_info": {
+ "file_extension": ".raku",
+ "mimetype": "text/plain",
+ "name": "raku",
+ "version": "6.d"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/challenge-346/feng-chang/raku/ch-1.raku b/challenge-346/feng-chang/raku/ch-1.raku
index e92f64c8a6..babf773aa9 100755
--- a/challenge-346/feng-chang/raku/ch-1.raku
+++ b/challenge-346/feng-chang/raku/ch-1.raku
@@ -2,6 +2,6 @@
unit sub MAIN(Str:D $s where *.comb.all eq <( )>.any);
-my regex Parens { '(' <&Parens>* ')' };
+my token Parens { '(' <&Parens>* ')' };
put $s.match(/<Parens>+/, :ex)».chars.max;
diff --git a/challenge-346/feng-chang/raku/ch-2.ipynb b/challenge-346/feng-chang/raku/ch-2.ipynb
new file mode 100644
index 0000000000..e6f0cdd2bc
--- /dev/null
+++ b/challenge-346/feng-chang/raku/ch-2.ipynb
@@ -0,0 +1,181 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "73898474-bf47-4a53-96a0-3673eb0014a6",
+ "metadata": {},
+ "source": [
+ "# PWC346, Task 2 Magic Expression\n",
+ "**冯昶 2025-11-11**\n",
+ "\n",
+ "Following is my ch-2.raku script:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ff38da0a-6e06-4505-9de4-44065347e725",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#!/bin/env raku\n",
+ "\n",
+ "unit sub MAIN(Str:D $s, Int:D $tgt);\n",
+ "\n",
+ "use MONKEY-SEE-NO-EVAL;\n",
+ "\n",
+ "my @s = $s.comb;\n",
+ "my @opers = '+', '-', '*', '';\n",
+ "\n",
+ "my @ans;\n",
+ "for [X] @opers xx (+@s-1) -> @ops {\n",
+ " my $exp = roundrobin(@s, @ops).flat.join;\n",
+ " next if $exp ~~ /« '0' \\d/; # drop expressions which contains number with a leading 0\n",
+ " @ans.push($exp) if EVAL($exp) == $tgt;\n",
+ "}\n",
+ "put @ans.join(', ');"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "27f89c89-9fd2-4d02-ae80-2415a2905ee3",
+ "metadata": {},
+ "source": [
+ "Let me explain the code line by line."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "5731b860-2f98-407a-bcdd-e848c4054bd2",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[\"1\", \"2\", \"3\", \"4\"]\n"
+ ]
+ }
+ ],
+ "source": [
+ "# $s.comb translates string $s into list of numbers\n",
+ "my $s = '1234';\n",
+ "my @s = $s.comb;\n",
+ "put @s.raku;"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "62c52ba5-81d8-4d2e-8a96-99f79ee86379",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# @opers array consists of all possible operations\n",
+ "my @opers = '+', '-', '*', '';\n",
+ "# of which the '' op is the operation to join 2 numbers together"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "034dbc9e-b85e-4608-91e7-f5651c57c293",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "4\n"
+ ]
+ }
+ ],
+ "source": [
+ "# +@s is the size of array @s, while +@s is size of @s minus 1\n",
+ "my @s = <1 2 3 4 5>;\n",
+ "put +@s-1;"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "2f58fc51-bdda-4ec0-a0c5-f5ae7863f848",
+ "metadata": {},
+ "source": [
+ "`[X] @opers xx 3` is the same as `@opers X @opers X @opers`.\n",
+ "So @ops in the for loop iterates through all possible operation combinations.\n",
+ "\n",
+ "roundrobin() combines the digits and operations together. e.g.,"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "id": "4e252fee-fff3-4d67-9ecd-ed067a507441",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "1+2-3*4+5\n"
+ ]
+ }
+ ],
+ "source": [
+ "put roundrobin(<1 2 3 4 5>, <+ - * +>).flat.join;"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7c9588c5-4032-4427-96ac-a4f1eb596544",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# the « sign in regex is left anchor for a word\n",
+ "next if $exp ~~ /« '0' \\d/; # drop expressions which contains number with a leading 0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3a3b76bb-49a6-4eba-a4d0-9b2074c62809",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# at the end of the loop, @ans will contain all answers\n",
+ "@ans.push($exp) if EVAL($exp) == $tgt;\n",
+ "\n",
+ "`use MONKEY-SEE-NO-EVAL;` must be declared because EVAL() is considered unsafe.\n",
+ "\n",
+ "The last line prints all answers separated by commas:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "23ea10e4-8fa0-4654-ad89-c6495f451f2b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "put @ans.join(', ');"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Raku",
+ "language": "raku",
+ "name": "raku"
+ },
+ "language_info": {
+ "file_extension": ".raku",
+ "mimetype": "text/plain",
+ "name": "raku",
+ "version": "6.d"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}