diff options
| author | arnesom <arne@bbop.org> | 2023-12-23 21:09:17 +0100 |
|---|---|---|
| committer | arnesom <arne@bbop.org> | 2023-12-23 21:09:17 +0100 |
| commit | 4ca79ef4aa1709915e6cb4a5ba6d94c5069eee17 (patch) | |
| tree | 77f6abb1735eaca98d75c95c21dde9548ac6bb97 | |
| parent | eb6fc334e5841458cdeaadd1b5bf4d68c7b54aed (diff) | |
| download | perlweeklychallenge-club-4ca79ef4aa1709915e6cb4a5ba6d94c5069eee17.tar.gz perlweeklychallenge-club-4ca79ef4aa1709915e6cb4a5ba6d94c5069eee17.tar.bz2 perlweeklychallenge-club-4ca79ef4aa1709915e6cb4a5ba6d94c5069eee17.zip | |
Arne Sommer
| -rw-r--r-- | challenge-248/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-248/arne-sommer/raku/ch-1.raku | 29 | ||||
| -rwxr-xr-x | challenge-248/arne-sommer/raku/ch-2.raku | 26 | ||||
| -rwxr-xr-x | challenge-248/arne-sommer/raku/shortest-distance | 44 | ||||
| -rwxr-xr-x | challenge-248/arne-sommer/raku/shortest-distance-leftright | 29 | ||||
| -rwxr-xr-x | challenge-248/arne-sommer/raku/submatrix-sum | 26 |
6 files changed, 155 insertions, 0 deletions
diff --git a/challenge-248/arne-sommer/blog.txt b/challenge-248/arne-sommer/blog.txt new file mode 100644 index 0000000000..90590efab0 --- /dev/null +++ b/challenge-248/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/shortest-sub.html diff --git a/challenge-248/arne-sommer/raku/ch-1.raku b/challenge-248/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..3b827442a5 --- /dev/null +++ b/challenge-248/arne-sommer/raku/ch-1.raku @@ -0,0 +1,29 @@ +#! /usr/bin/env raku + +unit sub MAIN ($str = "loveleetcode", $char where $char.chars == 1 = "e", :v(:$verbose)); + +my @input = $str.comb; +my @output; + +my $distance = Inf; + +for @input -> $i +{ + $distance = 0 if $i eq $char; + + @output.push: $distance; + $distance++; +} + +say ":From left: ({ @output.join(",") })" if $verbose; + +$distance = Inf; + +for @input.end ... 0 -> $index +{ + $distance = 0 if @input[$index] eq $char; + @output[$index] = $distance if $distance < @output[$index]; + $distance++; +} + +say "({ @output.join(",") })"; diff --git a/challenge-248/arne-sommer/raku/ch-2.raku b/challenge-248/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..f8cc6f565f --- /dev/null +++ b/challenge-248/arne-sommer/raku/ch-2.raku @@ -0,0 +1,26 @@ +#! /usr/bin/env raku + +unit sub MAIN (Str $matrix = "1 2 3 4 | 5 6 7 8 | 9 10 11 12", :v(:$verbose)); + +my @a = $matrix.split("|")>>.words>>.Int>>.Array; +my $rows = @a.elems; +my $cols = @a[0].elems; + +die "The rows must have the same size" unless [==] @a>>.elems; +die "Integers only" unless all(@a[*;*]) ~~ Int; + +say ": Matrix: { @a.raku }" if $verbose; + +my @b; + +for 0 .. $rows -2 -> $r +{ + for 0 .. $cols -2 -> $c + { + @b[$r][$c] = @a[$r][$c] + @a[$r][$c+1] + @a[$r+1][$c] + @a[$r+1][$c+1]; + } +} + +say "["; +@b.map({ say " [" ~ @_>>.join(", ") ~ "]" }); +say "]"; diff --git a/challenge-248/arne-sommer/raku/shortest-distance b/challenge-248/arne-sommer/raku/shortest-distance new file mode 100755 index 0000000000..1e06e5977b --- /dev/null +++ b/challenge-248/arne-sommer/raku/shortest-distance @@ -0,0 +1,44 @@ +#! /usr/bin/env raku + +unit sub MAIN ($str = "loveleetcode", $char where $char.chars == 1 && $str.contains($char) = "e", :v(:$verbose)); + +my @input = $str.comb; +my $end = @input.end; +my @output; # = "-" xx @input.elems; +my @todo; + +for 0 .. $end -> $index +{ + if @input[$index] eq $char + { + @output[$index] = 0; + @todo.push: ($index, -1) if $index > 0; + @todo.push: ($index, +1) if $index < $end; + say ":Index: $index (match) -> zero" if $verbose; + } +} + +while @todo +{ + say ":Todo: { @todo.raku }" if $verbose; + my ($pos, $offset) = @todo.shift; + + my $index = $pos + $offset; + + if ! @output[$index].defined + { + @output[$index] = $offset.abs; + my $new-offset = $offset < 0 ?? $offset -1 !! $offset +1; + @todo.push: ($pos, $new-offset) if 0 <= $pos + $new-offset <= $end; + + say ":Index:$index (from:$pos,$offset) -> { $offset.abs }" if $verbose; + } + elsif $verbose + { + say ":Index:$index (from:$pos,$offset) - already set (to:@output[$index])" if $verbose; + } +} + +say "({ @output.join(",") })"; + + diff --git a/challenge-248/arne-sommer/raku/shortest-distance-leftright b/challenge-248/arne-sommer/raku/shortest-distance-leftright new file mode 100755 index 0000000000..3b827442a5 --- /dev/null +++ b/challenge-248/arne-sommer/raku/shortest-distance-leftright @@ -0,0 +1,29 @@ +#! /usr/bin/env raku + +unit sub MAIN ($str = "loveleetcode", $char where $char.chars == 1 = "e", :v(:$verbose)); + +my @input = $str.comb; +my @output; + +my $distance = Inf; + +for @input -> $i +{ + $distance = 0 if $i eq $char; + + @output.push: $distance; + $distance++; +} + +say ":From left: ({ @output.join(",") })" if $verbose; + +$distance = Inf; + +for @input.end ... 0 -> $index +{ + $distance = 0 if @input[$index] eq $char; + @output[$index] = $distance if $distance < @output[$index]; + $distance++; +} + +say "({ @output.join(",") })"; diff --git a/challenge-248/arne-sommer/raku/submatrix-sum b/challenge-248/arne-sommer/raku/submatrix-sum new file mode 100755 index 0000000000..f8cc6f565f --- /dev/null +++ b/challenge-248/arne-sommer/raku/submatrix-sum @@ -0,0 +1,26 @@ +#! /usr/bin/env raku + +unit sub MAIN (Str $matrix = "1 2 3 4 | 5 6 7 8 | 9 10 11 12", :v(:$verbose)); + +my @a = $matrix.split("|")>>.words>>.Int>>.Array; +my $rows = @a.elems; +my $cols = @a[0].elems; + +die "The rows must have the same size" unless [==] @a>>.elems; +die "Integers only" unless all(@a[*;*]) ~~ Int; + +say ": Matrix: { @a.raku }" if $verbose; + +my @b; + +for 0 .. $rows -2 -> $r +{ + for 0 .. $cols -2 -> $c + { + @b[$r][$c] = @a[$r][$c] + @a[$r][$c+1] + @a[$r+1][$c] + @a[$r+1][$c+1]; + } +} + +say "["; +@b.map({ say " [" ~ @_>>.join(", ") ~ "]" }); +say "]"; |
