aboutsummaryrefslogtreecommitdiff
path: root/challenge-272
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-10 01:10:10 +0100
committerGitHub <noreply@github.com>2024-06-10 01:10:10 +0100
commit0f404d4f17b226d4d5fc19df822659ff9fc5bc63 (patch)
treee536d877757ef811fd159fcbd5b18ac20b5af16c /challenge-272
parent14507061e2787c082e9431a97f6513eb8d3ea1e0 (diff)
parent0ba5e71b02eb0b054e934b1aa01dea34f1c51670 (diff)
downloadperlweeklychallenge-club-0f404d4f17b226d4d5fc19df822659ff9fc5bc63.tar.gz
perlweeklychallenge-club-0f404d4f17b226d4d5fc19df822659ff9fc5bc63.tar.bz2
perlweeklychallenge-club-0f404d4f17b226d4d5fc19df822659ff9fc5bc63.zip
Merge pull request #10237 from Util/c272
Add TWC 272 solutions by Bruce Gray, in Raku only.
Diffstat (limited to 'challenge-272')
-rw-r--r--challenge-272/bruce-gray/raku/ch-1.raku20
-rw-r--r--challenge-272/bruce-gray/raku/ch-2.raku34
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-272/bruce-gray/raku/ch-1.raku b/challenge-272/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..fa82cbe963
--- /dev/null
+++ b/challenge-272/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,20 @@
+# Notice the use of the plain dot as a string, instead of the escaped dot in a regex that Perl's `s/\./[.]/g` would need.
+sub task1 ( Str $fanged --> Str ) {
+ return $fanged.subst: :g, '.', '[.]';
+}
+
+# constant &task1 = *.subst: :g, '.', '[.]'; # Same thing, but shorter.
+
+# .trans would also work, possibly more efficiently than .subst,
+# but with harder-to-read syntax due to replacing single-chars with multi-chars, like:
+# .trans: ['.'] => ['[.]'];
+
+
+
+use Test; plan +constant @tests =
+ ( '1.1.1.1' , '1[.]1[.]1[.]1' ),
+ ( '255.101.1.0' , '255[.]101[.]1[.]0' ),
+;
+for @tests -> ($in, $expected) {
+ is task1($in), $expected, "$in";
+}
diff --git a/challenge-272/bruce-gray/raku/ch-2.raku b/challenge-272/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..5b66308fef
--- /dev/null
+++ b/challenge-272/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,34 @@
+# FYI: I originally wrote `$s.comb».ord ...` in both subs,
+# before I remembered https://docs.raku.org/routine/ords
+
+sub task2_rotor ( Str $s --> UInt ) {
+ return $s.ords
+ .rotor(2 => -1)
+ .map({ abs .[0] - .[1] })
+ .sum;
+
+ # Could replace the map with either of:
+ # .map({ abs [-] .list })
+ # .flat.map(&[-])».abs
+}
+
+sub task2_Z_skip ( Str $s --> UInt ) {
+ return ( .list Z- .skip )».abs.sum given $s.ords.cache;
+}
+
+
+constant @tests =
+ < 13 hello >,
+ < 30 perl >,
+ < 37 raku >,
+;
+constant @subs =
+ :&task2_rotor,
+ :&task2_Z_skip,
+;
+use Test; plan +@tests * +@subs;
+for @subs -> ( :key($sub_name), :value(&task2) ) {
+ for @tests -> ($expected, $in) {
+ is task2($in), $expected, "$sub_name : $in";
+ }
+}