aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-248/0rir/raku/ch-1.raku99
-rw-r--r--challenge-248/0rir/raku/ch-2.raku96
2 files changed, 195 insertions, 0 deletions
diff --git a/challenge-248/0rir/raku/ch-1.raku b/challenge-248/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..6e176bedc1
--- /dev/null
+++ b/challenge-248/0rir/raku/ch-1.raku
@@ -0,0 +1,99 @@
+#!/usr/bin/env rak;
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ;
+use v6;
+use Test;
+
+=begin comment
+248-1: Shortest Distance Submitted by: Mohammad S Anwar
+You are given a string and a character in the given string.
+Write a script to return an array of integers of size same as length of
+the given string such that:
+ distance[i] is the distance from index i to the closest occurence of
+ the given character in the given string;
+
+The distance between two indices i and j is abs(i - j);
+Example 1
+Input: $str = "loveleetcode", $char = "e";
+Output: (3,2,1,0,1,0,0,1,2,2,1,0)
+
+The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
+The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
+The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
+For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5,
+but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
+The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.
+Example 2
+Input: $str = "aaab", $char = "b"
+Output: (3,2,1,0)
+=end comment
+
+my @distance-test = 1,(0,), 2,(1,0), 3,(1,1,0), 4,(1,2,1,0), 5,(1,2,2,1,0);
+
+my @Test =
+ # char str expect
+ 'a', "bb", (),
+ 'a', "bbba", (3,2,1,0),
+ 'a', "bbbabbb", (3,2,1,0,1,2,3),
+ 'a', 'a', (0,),
+ 'a', 'aa', (0,0),
+ 'a', "aaa", (0,0,0),
+ 'a', "aaaaa", (0,0,0,0,0),
+ 'a', "aba", (0,1,0),
+ 'a', "abbba", (0,1,2,1,0),
+ 'a', "abbbba", (0,1,2,2,1,0),
+ 'a', "abcdeabcdefa", (0,1,2,2,1,0,1,2,3,2,1,0),
+ 'a', "zyabcdeabcdefabc", (2,1,0,1,2,2,1,0,1,2,3,2,1,0,1,2),
+ 'e', "loveleetcode", (3,2,1,0,1,0,0,1,2,2,1,0),
+ 'e', "loveleetcodeax", (3,2,1,0,1,0,0,1,2,2,1,0,1,2),
+;
+
+plan @Test ÷ 3 + @distance-test ÷ 2;
+
+sub distance-to-magic( Str:D $s, Str:D $magic where *.chars == 1 --> List) {
+ my $end = $s.chars - 1;
+ my @zed = $s.comb.grep: $magic, :k;
+
+ return () unless @zed;
+
+ my $prior = @zed.shift;
+ my $ret;
+ $ret.append: (0 … $prior).reverse; # distance to first
+
+ for ^@zed { # distance to next zed
+ my $cur = @zed[$_];
+ $ret.append: fill-in( $cur - $prior);
+ $prior = $cur;
+ }
+
+ if $prior < $end { # #distance after last
+ $ret.push: ($prior ^.. $end) - $prior;
+ }
+ $ret;
+}
+
+# Create a list of ascending and descending numbers (distances) to fill
+# in between two indices, include the second index:
+# 5,7 -> 1,0; 5,8 -> 1,1,0; 5,9 -> 1,2,1,0.
+sub fill-in( $distance -->List) {
+ given $distance {
+ when -∞ .. 0 { die "Programer too stupid." }
+ when 1 { (0,) }
+ when $_ %% 2 { |( 1 … $_ div 2), |( $_ div 2 -1 … 0) }
+ default { |( 1 … $_ div 2), |( $_ div 2 … 0) }
+ }
+}
+
+for @distance-test -> $d, $exp {
+ is fill-in( $d), $exp, "fill-in($d)-> $exp.raku()";
+}
+
+for @Test -> $char, $in, $exp {
+ is distance-to-magic($in,$char), $exp, $exp ~ " <- $char * $in";
+}
+done-testing;
+
+my $str = "1oveVe1eetaCodex";
+my $char = "e";
+say "\nInput: \$str = $str\nOutput: &distance-to-magic( $str, $char)";
+
+exit;
diff --git a/challenge-248/0rir/raku/ch-2.raku b/challenge-248/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..fda5a838c2
--- /dev/null
+++ b/challenge-248/0rir/raku/ch-2.raku
@@ -0,0 +1,96 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+248-2: Submatrix Sum Submitted by: Jorg Sommrey
+
+You are given a NxM matrix A of integers. Write a script to construct a
+(N-1)x(M-1) matrix B having elements that are the sum over the 2x2
+submatrices of A, b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1]
+
+Example 1
+Input: $a = [
+ [1, 2, 3, 4],
+ [5, 6, 7, 8],
+ [9, 10, 11, 12]
+ ]
+
+Output: $b = [
+ [14, 18, 22],
+ [30, 34, 38]
+ ]
+Example 2
+Input: $a = [
+ [1, 0, 0, 0],
+ [0, 1, 0, 0],
+ [0, 0, 1, 0],
+ [0, 0, 0, 1]
+ ]
+
+Output: $b = [
+ [2, 1, 0],
+ [1, 2, 1],
+ [0, 1, 2]
+ ]
+=end comment
+
+my @Test =
+ [ [ 1, 2], [3,4] ],
+ [10,],
+ [ [ 1, 2, 3], [3,4,5] ],
+ [10,14],
+ [ [ 1, 2, 3,], [3,4,5,], [21,22,23] ],
+ [[10,14], [50,54]],
+ [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],],
+ [ [14, 18, 22], [30, 34, 38], ],
+ [ [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], ],
+;
+my @Die =
+ [,],
+ [[1,],],
+ [[1,],[,],],
+ [[,],[2,],],
+ [[1,2,3],],
+ [[1,],[2,],[3,],],
+;
+
+plan +@Die + @Test ÷ 2;
+
+sub submatrix-sum( @m is copy --> Array) {
+ die 'matrix too short' unless @m.elems > 1;
+ die 'matrix too skinny' unless @m[0].elems > 1;
+ die 'ill-formed matrix' unless @m[0.. *-1]».elems.all == @m[0].elems;
+ my @ret;
+ for ^@m.end -> \i {
+ for ^@m[i].end -> \j {
+ @ret[i;j] = [+] @m[i;j], @m[i+1;j], @m[i;j+1], @m[i+1;j+1];
+ }
+ }
+ @ret;
+}
+
+for @Test -> @in, @exp {
+ is submatrix-sum(@in), @exp, "@exp[] <- @in[]";
+}
+for @Die -> @in {
+ dies-ok { submatrix-sum(@in)}, "die on @in.raku()";
+}
+done-testing;
+
+sub display-matrix( $prefix, @matrix -->Str) {
+ my $ret = "$prefix [\n";
+ my $shift = $prefix.chars;
+ for @matrix -> @r {
+ $ret ~= ' ' x ($shift + 3) ~ @r.raku() ~ "\n";
+ }
+ $ret ~= ' ' x $shift ~ "]\n";
+ $ret;
+}
+
+my @a = [ [ 1, 2, 3,], [3,4,5,], [21,22,23] ];
+say "\n", display-matrix 'Input: $a = ', @a;
+say display-matrix 'Output: $b = ', submatrix-sum( @a);;
+exit;