aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2023-01-02 08:05:01 -0600
committerUtil <bruce.gray@acm.org>2023-01-02 08:05:01 -0600
commitc9cf91fb1aaeb4fe95c7e36a3b415f70045d4f05 (patch)
tree8c8acb17ef41072b2b2eea52a7dea35d3941f427
parentd10a3ecb46bdb3d152894c56b94e306bf0dcc12d (diff)
downloadperlweeklychallenge-club-c9cf91fb1aaeb4fe95c7e36a3b415f70045d4f05.tar.gz
perlweeklychallenge-club-c9cf91fb1aaeb4fe95c7e36a3b415f70045d4f05.tar.bz2
perlweeklychallenge-club-c9cf91fb1aaeb4fe95c7e36a3b415f70045d4f05.zip
Add TWC 198 solutions by Bruce Gray (Raku only).
-rw-r--r--challenge-198/bruce-gray/raku/ch-1.raku18
-rw-r--r--challenge-198/bruce-gray/raku/ch-2.raku34
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-198/bruce-gray/raku/ch-1.raku b/challenge-198/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..ca8f7c0aa8
--- /dev/null
+++ b/challenge-198/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,18 @@
+sub task1 ( @list --> UInt ) {
+ return @list.sort(+*)
+ .rotor(2 => -1)
+ .map({[R-] .list})
+ .maxpairs
+ .elems;
+}
+
+
+my @tests =
+ ( (2,5,8,1) , 2 ),
+ ( (3,) , 0 ),
+;
+use Test;
+plan +@tests;
+for @tests -> ($in, $expected) {
+ is task1($in), $expected;
+}
diff --git a/challenge-198/bruce-gray/raku/ch-2.raku b/challenge-198/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..b4eb93910e
--- /dev/null
+++ b/challenge-198/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,34 @@
+sub task2a ( UInt $n --> UInt ) { return +grep &is-prime, ^$n }
+sub task2b ( UInt $n --> UInt ) {
+ constant @A230980 = [\+] map +*.is-prime, ^Inf;
+ return @A230980[$n - 1];
+}
+sub task2c ( UInt $n --> UInt ) {
+ use Math::Prime::Util:from<Perl5> <prime_count>;
+ return prime_count(1, $n-1);
+}
+
+
+my @tests =
+ ( 10, 4 ),
+ ( 15, 6 ),
+ ( 1, 0 ),
+ ( 25, 9 ),
+;
+my @subs = :&task2a, :&task2b, :&task2c;
+
+use Test;
+plan (+@tests + 1) * +@subs;
+
+for @subs {
+ my ( $sub_name, $sub_code ) = .kv;
+ for @tests.pairs {
+ my $test_num = .key + 1;
+ my ($in, $expected) = .value.list;
+
+ is $sub_code.($in), $expected, "$test_num : $sub_name";
+ }
+ is-deeply (map $sub_code, 1..30),
+ (0,0,1,2,2,3,3,4,4,4,4,5,5,6,6,6,6,7,7,8,8,8,8,9,9,9,9,9,9,10),
+ "A230980 : $sub_name"; # https://oeis.org/A230980
+}