aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-11-13 10:14:53 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-11-13 10:14:53 +0800
commit9831ad5b94643aec63e30e720b83dff7a5eac18b (patch)
tree57b2b66d6a58b08ee662e4c09205183b8c0e1045
parent50b486fa26bc3ad30ed80a83f269f8dc93fd8bcd (diff)
downloadperlweeklychallenge-club-9831ad5b94643aec63e30e720b83dff7a5eac18b.tar.gz
perlweeklychallenge-club-9831ad5b94643aec63e30e720b83dff7a5eac18b.tar.bz2
perlweeklychallenge-club-9831ad5b94643aec63e30e720b83dff7a5eac18b.zip
challenge 241, raku solutions
-rwxr-xr-xchallenge-241/feng-chang/raku/ch-1.raku7
-rwxr-xr-xchallenge-241/feng-chang/raku/ch-2.raku28
-rwxr-xr-xchallenge-241/feng-chang/raku/test.raku23
3 files changed, 58 insertions, 0 deletions
diff --git a/challenge-241/feng-chang/raku/ch-1.raku b/challenge-241/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..b31dbf97eb
--- /dev/null
+++ b/challenge-241/feng-chang/raku/ch-1.raku
@@ -0,0 +1,7 @@
+#!/bin/env raku
+
+unit sub MAIN(*@ints where +* > 3);
+
+@ints = +«@ints;
+my $diff = @ints.pop;
+put +@ints.grep({ all($_ - $diff, $_ + $diff) (elem) @ints });
diff --git a/challenge-241/feng-chang/raku/ch-2.raku b/challenge-241/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..6648fa40d8
--- /dev/null
+++ b/challenge-241/feng-chang/raku/ch-2.raku
@@ -0,0 +1,28 @@
+#!/bin/env raku
+
+unit sub MAIN(*@ints where *.all > 1);
+
+sub prime-factors(UInt:D $n) {
+ my Int $m = $n;
+ my @factors;
+ my $cnt = 0;
+
+ for (^Inf).grep: *.is-prime -> $p {
+ while ($m %% $p) {
+ @factors.push($p);
+ ++$cnt;
+ $m div= $p;
+ }
+
+ last if $m < $p * $p;
+ }
+
+ if $m > 1 {
+ @factors.push($m);
+ ++$cnt;
+ }
+
+ $cnt, |@factors
+}
+
+put @ints.sort({ prime-factors($_) });
diff --git a/challenge-241/feng-chang/raku/test.raku b/challenge-241/feng-chang/raku/test.raku
new file mode 100755
index 0000000000..affec710c4
--- /dev/null
+++ b/challenge-241/feng-chang/raku/test.raku
@@ -0,0 +1,23 @@
+#!/bin/env raku
+
+# The Weekly Challenge 241
+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, Arithmetic Triplets
+pwc-test './ch-1.raku', |<0 1 4 6 7 10>, 3, 2, 'Arithmetic Triplets: @nums = (0, 1, 4, 6, 7, 10), $diff = 3 => 2';
+pwc-test './ch-1.raku', |<4 5 6 7 8 9>, 2, 2, 'Arithmetic Triplets: @nums = (4, 5, 6, 7, 8, 9), $diff = 2 => 2';
+
+# Task 2, Prime Order
+pwc-test './ch-2.raku', |<11 8 27 4>, '11 4 8 27', 'Prime Order: (11, 8, 27, 4) => (11, 4, 8, 27)';
+
+done-testing;