aboutsummaryrefslogtreecommitdiff
path: root/challenge-252
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2024-01-15 18:13:46 +0800
committer冯昶 <fengchang@novel-supertv.com>2024-01-15 18:13:46 +0800
commit4497a5f59bd9de56f576e49baf32528243699268 (patch)
tree1033fc74de1249b653a381c83e938edbd658016d /challenge-252
parenta48b8d5fda8c191d2bb00457128024a8357bcbdf (diff)
downloadperlweeklychallenge-club-4497a5f59bd9de56f576e49baf32528243699268.tar.gz
perlweeklychallenge-club-4497a5f59bd9de56f576e49baf32528243699268.tar.bz2
perlweeklychallenge-club-4497a5f59bd9de56f576e49baf32528243699268.zip
challenge 252, raku solutions
Diffstat (limited to 'challenge-252')
-rwxr-xr-xchallenge-252/feng-chang/raku/ch-1.raku5
-rwxr-xr-xchallenge-252/feng-chang/raku/ch-2.raku9
-rwxr-xr-xchallenge-252/feng-chang/raku/test.raku32
3 files changed, 46 insertions, 0 deletions
diff --git a/challenge-252/feng-chang/raku/ch-1.raku b/challenge-252/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..39ced4a162
--- /dev/null
+++ b/challenge-252/feng-chang/raku/ch-1.raku
@@ -0,0 +1,5 @@
+#!/bin/env raku
+
+unit sub MAIN(*@ints);
+
+put @ints.pairs.map({ .value² if +@ints %% (.key + 1) }).sum;
diff --git a/challenge-252/feng-chang/raku/ch-2.raku b/challenge-252/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..c6767855f5
--- /dev/null
+++ b/challenge-252/feng-chang/raku/ch-2.raku
@@ -0,0 +1,9 @@
+#!/bin/env raku
+
+unit sub MAIN($n where * > 0);
+
+put solve($n, 1).join(' ');
+
+multi solve(0, $) { () }
+multi solve(1, $) { 0 }
+multi solve($n, $i) { $i, -$i, |solve($n-2, $i+1) }
diff --git a/challenge-252/feng-chang/raku/test.raku b/challenge-252/feng-chang/raku/test.raku
new file mode 100755
index 0000000000..5df710a5b0
--- /dev/null
+++ b/challenge-252/feng-chang/raku/test.raku
@@ -0,0 +1,32 @@
+#!/bin/env raku
+
+# The Weekly Challenge 252
+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, Special Numbers
+pwc-test './ch-1.raku', |<1 2 3 4>, 21, 'Special Numbers: (1, 2, 3, 4) => 21';
+pwc-test './ch-1.raku', |<2 7 1 19 18 3>, 63, 'Special Numbers: (2, 7, 1, 19, 18, 3) => 63';
+
+sub pwc252-p2-test(Str:D $script, $n) {
+ my $p = run $script, $n, :out;
+ my @ints = +«$p.out.slurp(:close).chomp.words;
+
+ ok (@ints.all ~~ Int and @ints.sum == 0 and +@ints.unique == +@ints), "$n => ({@ints.join(', ')})";
+}
+
+# Task 2, Unique Sum Zero
+pwc252-p2-test './ch-2.raku', 5;
+pwc252-p2-test './ch-2.raku', 3;
+pwc252-p2-test './ch-2.raku', 1;
+
+done-testing;