aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2023-04-16 15:03:04 -0500
committerUtil <bruce.gray@acm.org>2023-04-16 15:03:04 -0500
commitae7e0b14d8c01d56d03ec9d630c19c6299e2fc44 (patch)
tree367b494d7dfd80d8ac6ac70cdf90e5c40048a52f
parent460563374929fde06af50ad536397bdde377d181 (diff)
downloadperlweeklychallenge-club-ae7e0b14d8c01d56d03ec9d630c19c6299e2fc44.tar.gz
perlweeklychallenge-club-ae7e0b14d8c01d56d03ec9d630c19c6299e2fc44.tar.bz2
perlweeklychallenge-club-ae7e0b14d8c01d56d03ec9d630c19c6299e2fc44.zip
Add TWC 212 solutions by Bruce Gray (Raku only).
-rw-r--r--challenge-212/bruce-gray/raku/ch-1.raku24
-rw-r--r--challenge-212/bruce-gray/raku/ch-2.raku46
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-212/bruce-gray/raku/ch-1.raku b/challenge-212/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..031b5855b9
--- /dev/null
+++ b/challenge-212/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,24 @@
+sub task1 ( Str $s, @ns --> Str ) {
+
+ sub step ( Str $letter, UInt $distance --> Str ) {
+ constant $a = 'a'.ord;
+
+ return ( (( $letter.lc.ord - $a + $distance ) % 26 ) + $a ).chr;
+ }
+
+ return ($s.comb Z[&step] @ns).join.samecase($s);
+
+ # Original one-liner:
+ # return ($s.comb Z @ns).map({ ( (( .[0].lc.ord - $a + .[1]) % 26 ) + $a ).chr }).join.samecase($s);
+}
+
+
+my @tests =
+ ( 'Perl', (2,22,19,9), 'Raku' ),
+ ( 'Raku', (24,4,7,17), 'Perl' ),
+;
+use Test;
+plan +@tests;
+for @tests -> ( $in_str, @in_jumps, $expected ) {
+ is task1( $in_str, @in_jumps ), $expected;
+}
diff --git a/challenge-212/bruce-gray/raku/ch-2.raku b/challenge-212/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..1e560f4c78
--- /dev/null
+++ b/challenge-212/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,46 @@
+sub in_sequential_order ( @ns --> Bool ) {
+ return @ns < 2 || so (@ns.skip Z- @ns).all == 1;
+}
+sub task2 ( UInt $size, @ns --> List ) {
+ die unless $size >= 2;
+ die unless @ns.all ~~ Real;
+ return Nil if @ns.elems !%% $size;
+
+ my @a = @ns.Bag.sort;
+
+ my @r = gather while @a.elems >= $size {
+ my @cand = @a.head($size)ยป.key;
+ return Nil unless in_sequential_order(@cand);
+
+ take @cand.List;
+
+ @a[$_] = @a[$_].key => (@a[$_].value - 1) for ^$size;
+ @a.shift while +@a and @a.head.value == 0;
+ }
+ return @r.List;
+}
+# In hindsight, I might have taken a different approach. I like the one-time .sort into an Array of Pairs, but Pairs are immutable, which requires the clumsy reconstruction in line 17, instead of just `.value--`.
+#
+# Note that the Bag will keep its keys untransformed, while a Hash would have (by default) had Str keys, which would have sorted wrong unless otherwise coerced.
+#
+# I could have further restricted the type of `ns` in the signatures, but I did not want to complicate the calling test code.
+
+
+my @tests =
+ ( 3, (1,2,3,5,1,2,7,6,3), ( (1,2,3), (1,2,3), (5,6,7) ) ),
+ ( 2, (1,2,3 ), -1 ),
+ ( 3, (1,2,4,3,5,3 ), ( (1,2,3), (3,4,5) ) ),
+ ( 3, (1,5,2,6,4,7 ), -1 ),
+
+ # Catch sorting as strings
+ ( 2, (9,10,12,13 ), ( (9,10), (12,13) ) ),
+
+ # Also works with Rats.
+ ( 3, (1.1,2.1,4.1,3.1,5.1,3.1), ( (1.1,2.1,3.1), (3.1,4.1,5.1) ) ),
+;
+use Test;
+plan +@tests;
+for @tests -> ( $in_size, @in_ns, $expected ) {
+ my $got = task2( $in_size, @in_ns ) // -1;
+ is-deeply $got, $expected;
+}