aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2025-11-03 22:11:52 +0800
committer冯昶 <fengchang@novel-supertv.com>2025-11-03 22:11:52 +0800
commitb8502931bf4dc893b98419c120ad8ea6dab19cda (patch)
tree505cd42aea45f4665247129ae0a0af518c9be36d
parentc3cc706ddfb26c0a2d3dba5b0704b35e99a1beef (diff)
downloadperlweeklychallenge-club-b8502931bf4dc893b98419c120ad8ea6dab19cda.tar.gz
perlweeklychallenge-club-b8502931bf4dc893b98419c120ad8ea6dab19cda.tar.bz2
perlweeklychallenge-club-b8502931bf4dc893b98419c120ad8ea6dab19cda.zip
challenge 346, raku solutions
-rwxr-xr-xchallenge-346/feng-chang/raku/ch-1.raku7
-rwxr-xr-xchallenge-346/feng-chang/raku/ch-2.raku16
-rwxr-xr-xchallenge-346/feng-chang/raku/test.raku31
3 files changed, 54 insertions, 0 deletions
diff --git a/challenge-346/feng-chang/raku/ch-1.raku b/challenge-346/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..e92f64c8a6
--- /dev/null
+++ b/challenge-346/feng-chang/raku/ch-1.raku
@@ -0,0 +1,7 @@
+#!/bin/env raku
+
+unit sub MAIN(Str:D $s where *.comb.all eq <( )>.any);
+
+my regex Parens { '(' <&Parens>* ')' };
+
+put $s.match(/<Parens>+/, :ex)».chars.max;
diff --git a/challenge-346/feng-chang/raku/ch-2.raku b/challenge-346/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..eb33c068d7
--- /dev/null
+++ b/challenge-346/feng-chang/raku/ch-2.raku
@@ -0,0 +1,16 @@
+#!/bin/env raku
+
+unit sub MAIN(Str:D $s, Int:D $tgt);
+
+use MONKEY-SEE-NO-EVAL;
+
+my @s = $s.comb;
+my @opers = '+', '-', '*', '';
+
+my @ans;
+for [X] @opers xx (+@s-1) -> @ops {
+ my $exp = roundrobin(@s, @ops).flat.join;
+ next if $exp ~~ /« '0' \d/; # drop expressions which contains number with a leading 0
+ @ans.push($exp) if EVAL($exp) == $tgt;
+}
+put @ans.join(', ');
diff --git a/challenge-346/feng-chang/raku/test.raku b/challenge-346/feng-chang/raku/test.raku
new file mode 100755
index 0000000000..045c2248d5
--- /dev/null
+++ b/challenge-346/feng-chang/raku/test.raku
@@ -0,0 +1,31 @@
+#!/bin/env raku
+
+# The Weekly Challenge 346
+use Test;
+
+sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) {
+ my ($expect, $assertion) = @input.splice(*-2, 2);
+ my $p = run $script, |@input, :out;
+ if $deeply {
+ is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion;
+ } else {
+ is $p.out.slurp(:close).chomp, $expect, $assertion;
+ }
+}
+
+# Task 1, Longest Parenthesis
+pwc-test './ch-1.raku', '(()())', 6, 'Longest Parenthesis: (()()) => 6';
+pwc-test './ch-1.raku', ')()())', 4, 'Longest Parenthesis: )()()) => 4';
+pwc-test './ch-1.raku', '((()))()(((()', 8, 'Longest Parenthesis: ((()))()(((() => 8';
+pwc-test './ch-1.raku', '))))((()(', 2, 'Longest Parenthesis: ))))((()( => 2';
+pwc-test './ch-1.raku', '()(()', 2, 'Longest Parenthesis: ()(() => 2';
+pwc-test './ch-1.raku', '()(()()', 4, 'Longest Parenthesis: ()(()() => 4';
+
+# Task 2, Magic Expression
+pwc-test './ch-2.raku', 123, 6, '1+2+3, 1*2*3', 'Magic Expression: 123,6 => "1+2+3, 1*2*3"';
+pwc-test './ch-2.raku', 105, 5, '1*0+5, 10-5', 'Magic Expression: 105,5 => "1*0+5, 10-5"';
+pwc-test './ch-2.raku', 232, 8, '2+3*2, 2*3+2', 'Magic Expression: 232,8 => "2+3*2, 2*3+2"';
+pwc-test './ch-2.raku', 1234, 10, '1+2+3+4, 1*2*3+4', 'Magic Expression: 1234,10 => "1+2+3+4, 1*2*3+4"';
+pwc-test './ch-2.raku', 1001, 2,
+ '1+0+0+1, 1+0-0+1, 1+0*0+1, 1-0+0+1, 1-0-0+1, 1-0*0+1',
+ 'Magic Expression: 1001,2 => "1+0+0+1, 1+0-0+1, 1+0*0+1, 1-0+0+1, 1-0-0+1, 1-0*0+1"';