aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-248/wambash/raku/ch-1.raku29
-rw-r--r--challenge-248/wambash/raku/ch-2.raku20
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
+}