diff options
| author | Util <bruce.gray@acm.org> | 2024-06-09 18:30:46 -0500 |
|---|---|---|
| committer | Util <bruce.gray@acm.org> | 2024-06-09 18:30:46 -0500 |
| commit | 0ba5e71b02eb0b054e934b1aa01dea34f1c51670 (patch) | |
| tree | bd7fed7422720962cd3b21a652ecf768b2700482 /challenge-272 | |
| parent | 7aeb2014a04d815bc006a3c337b73426a10ea591 (diff) | |
| download | perlweeklychallenge-club-0ba5e71b02eb0b054e934b1aa01dea34f1c51670.tar.gz perlweeklychallenge-club-0ba5e71b02eb0b054e934b1aa01dea34f1c51670.tar.bz2 perlweeklychallenge-club-0ba5e71b02eb0b054e934b1aa01dea34f1c51670.zip | |
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.raku | 20 | ||||
| -rw-r--r-- | challenge-272/bruce-gray/raku/ch-2.raku | 34 |
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"; + } +} |
