diff options
| author | Jan Krňávek <Jan.Krnavek@gmail.com> | 2023-12-27 12:06:35 +0100 |
|---|---|---|
| committer | Jan Krňávek <Jan.Krnavek@gmail.com> | 2023-12-27 12:06:35 +0100 |
| commit | 5d1cbc65f2af0c9088e4794a5361d91c2cc97eef (patch) | |
| tree | c28fa01c84bbcbf0830588c8d28a392c2cae8cec | |
| parent | e056520665a1179416e001742a7c11a504548c51 (diff) | |
| download | perlweeklychallenge-club-5d1cbc65f2af0c9088e4794a5361d91c2cc97eef.tar.gz perlweeklychallenge-club-5d1cbc65f2af0c9088e4794a5361d91c2cc97eef.tar.bz2 perlweeklychallenge-club-5d1cbc65f2af0c9088e4794a5361d91c2cc97eef.zip | |
solutions week 248
| -rw-r--r-- | challenge-248/wambash/raku/ch-1.raku | 29 | ||||
| -rw-r--r-- | challenge-248/wambash/raku/ch-2.raku | 20 |
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-248/wambash/raku/ch-1.raku b/challenge-248/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..ca7faf8cb0 --- /dev/null +++ b/challenge-248/wambash/raku/ch-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +sub shortest-distance-index ($index, $str, $char) { + $str + andthen .indices: $char + andthen .map: { abs $_ - $index }\ + andthen .min +} + +sub shortest-distance ($str, $char) { + my $length = $str.chars; + + ^$length .map: *.&shortest-distance-index: $str, $char +} + +multi MAIN (Bool :test($)!) { + use Test; + is shortest-distance-index(3,'loveleetcode','l'), 1; + is shortest-distance-index(1,'loveleetcode','l'), 1; + is shortest-distance-index(0,'loveleetcode','c'), 8; + is shortest-distance-index(11,'loveleetcode','c'), 3; + is shortest-distance('loveleetcode','e'), (3,2,1,0,1,0,0,1,2,2,1,0); + is shortest-distance('aaab','b'), (3,2,1,0); + done-testing; +} + +multi MAIN ($str, $char) { + say shortest-distance $str, $char +} diff --git a/challenge-248/wambash/raku/ch-2.raku b/challenge-248/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..acd7ab5310 --- /dev/null +++ b/challenge-248/wambash/raku/ch-2.raku @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + + +sub submatrix-sum (+a) { + a + andthen .map: *.rotor( 2 => -1 ).map: *.sum + andthen .rotor: 2 => -1 + andthen .map: -> (@f, @s) { @f Z+ @s } +} + +multi MAIN (Bool :test($)!) { + use Test; + cmp-ok submatrix-sum((1...12).rotor(4)), &[~~], ((14,18,22),(30,34,38)); + cmp-ok submatrix-sum((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1)), &[~~], ((2,1,0),(1,2,1),(0,1,2)); + done-testing; +} + +multi MAIN (+a) { + say submatrix-sum +a +} |
