diff options
| -rwxr-xr-x | challenge-248/feng-chang/raku/ch-1.raku | 10 | ||||
| -rwxr-xr-x | challenge-248/feng-chang/raku/ch-2.raku | 15 | ||||
| -rwxr-xr-x | challenge-248/feng-chang/raku/test.raku | 36 |
3 files changed, 61 insertions, 0 deletions
diff --git a/challenge-248/feng-chang/raku/ch-1.raku b/challenge-248/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..e925cf3210 --- /dev/null +++ b/challenge-248/feng-chang/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s, Str:D $c where { $c.chars == 1 and $s.index($c) }); + +my @indices = $s.index($c); +while my $i = $s.index($c, @indices.tail + 1) { + @indices.push($i); +} + +put '(', (^$s.chars).map({ (@indices »-» $_)».abs.min }).join(', '), ')'; diff --git a/challenge-248/feng-chang/raku/ch-2.raku b/challenge-248/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..e5e73575d8 --- /dev/null +++ b/challenge-248/feng-chang/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s); + +use MONKEY-SEE-NO-EVAL; +my @a; +EVAL '@a = ' ~ $s; + +my @b; +for ^(+@a - 1) -> $i { + for ^(+@a[0] - 1) -> $j { + @b[$i;$j] = @a[$i;$j] + @a[$i;$j+1] + @a[$i+1;$j] + @a[$i+1;$j+1]; + } +} +put @b.raku; diff --git a/challenge-248/feng-chang/raku/test.raku b/challenge-248/feng-chang/raku/test.raku new file mode 100755 index 0000000000..93e9a35af9 --- /dev/null +++ b/challenge-248/feng-chang/raku/test.raku @@ -0,0 +1,36 @@ +#!/bin/env raku + +# The Weekly Challenge 248 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Shortest Distance +pwc-test './ch-1.raku', + 'loveleetcode', 'e', + '(3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0)', + 'Shortest Distance: loveleetcode, e => (3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0)'; +pwc-test './ch-1.raku', + 'aaab', 'b', + '(3, 2, 1, 0)', + 'Shortest Distance: aaab, b => (3, 2, 1, 0)'; + +# Task 2, Submatrix Sum +pwc-test './ch-2.raku', + '[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]', + '[[14, 18, 22], [30, 34, 38]]', + 'Submatrix Sum: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] => [[14, 18, 22], [30, 34, 38]]'; +pwc-test './ch-2.raku', + '[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]]', + '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; |
