aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-20 09:10:35 +0100
committerGitHub <noreply@github.com>2025-05-20 09:10:35 +0100
commit44c8332cc4a1a7d9f817d444d43fb544e89d5eae (patch)
tree1df4b22b38d1a1e870c09fe545ed41dd0d201df1
parentb88bfac5f59c90bbc3fe7fb75b9409d32ae4767d (diff)
parent174dc182320b7103cc835481f798c0284af14d81 (diff)
downloadperlweeklychallenge-club-44c8332cc4a1a7d9f817d444d43fb544e89d5eae.tar.gz
perlweeklychallenge-club-44c8332cc4a1a7d9f817d444d43fb544e89d5eae.tar.bz2
perlweeklychallenge-club-44c8332cc4a1a7d9f817d444d43fb544e89d5eae.zip
Merge pull request #12049 from andemark/challenge-322
Challenge 322 Solutions (Raku)
-rw-r--r--challenge-322/mark-anderson/raku/ch-1.raku36
-rw-r--r--challenge-322/mark-anderson/raku/ch-2.raku14
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-322/mark-anderson/raku/ch-1.raku b/challenge-322/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..d5fe719531
--- /dev/null
+++ b/challenge-322/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+use Test;
+
+is format-string("ABC-D-E-F", 3), "ABC-DEF";
+is format-string("A-BC-D-E", 2), "A-BC-DE";
+is format-string("-A-B-CD-E", 4), "A-BCDE";
+
+sub format-string($str, $int)
+{
+ # This is the version I like but it doesn't always work.
+ # The problem is with the {$int}. If it's hardcoded with
+ # the appropriate int then it works. Not sure what I'm
+ # doing wrong. 🤔 🤷
+
+ $str ~~ /^
+ ( \-? <upper> \-? ) ** {0..$int}
+ ([ \-? <upper> \-? ] ** {$int} )*
+ $/;
+
+ (flat $/[0]>><upper>.join, $/[1]>><upper>>>.join).join("-")
+}
+
+sub format-string-v2($str is copy, $int)
+{
+ $str ~~ s:g/"-"//;
+ my $m = "." x ($str.chars mod $int or $int);
+ my $i = "." x $int;
+ S:g/<?after <$m>> (<$i>)/-$0/ given $str
+}
+
+sub format-string-v3($str, $int)
+{
+ my @chars = $str.comb(/<upper>/);
+ my $list = flat (@chars mod $int or Empty), ($int, $int...*);
+ @chars.rotor($list)>>.join.join("-")
+}
diff --git a/challenge-322/mark-anderson/raku/ch-2.raku b/challenge-322/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..b0df999fce
--- /dev/null
+++ b/challenge-322/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,14 @@
+#/usr/bin/env raku
+use Test;
+
+is-deeply rank-array(55,22,44,33), (4,1,3,2);
+is-deeply rank-array(10,10,10), (1,1,1);
+is-deeply rank-array(5,1,1,4,3), (4,1,1,3,2);
+
+sub rank-array(+@ints)
+{
+ .{@ints} given [-Inf, |@ints].unique
+ .sort
+ .antipairs
+ .Map
+}