diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-21 09:48:48 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-21 09:48:48 +0000 |
| commit | f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc (patch) | |
| tree | ccebb955018077d92a371f9e5db31152bc8d15a1 | |
| parent | a04ded57f1d184eefeb0f192502338b46064b8e9 (diff) | |
| parent | 3f6a4dce3fdbe28fdb83c5e1ede37f9cacfc1faf (diff) | |
| download | perlweeklychallenge-club-f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc.tar.gz perlweeklychallenge-club-f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc.tar.bz2 perlweeklychallenge-club-f181f6707ad4afc7c6dccf263e1f3f911f5c1cbc.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-248/joelle-maslak/raku/ch-1.raku | 42 | ||||
| -rw-r--r-- | challenge-248/joelle-maslak/raku/ch-2.raku | 37 | ||||
| -rw-r--r-- | challenge-248/packy-anderson/README.md | 118 | ||||
| -rw-r--r-- | challenge-248/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-248/packy-anderson/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-248/packy-anderson/perl/ch-2.pl | 48 | ||||
| -rwxr-xr-x | challenge-248/packy-anderson/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-248/packy-anderson/python/ch-2.py | 49 | ||||
| -rwxr-xr-x | challenge-248/packy-anderson/raku/ch-1.raku | 30 | ||||
| -rwxr-xr-x | challenge-248/packy-anderson/raku/ch-2.raku | 52 |
10 files changed, 369 insertions, 69 deletions
diff --git a/challenge-248/joelle-maslak/raku/ch-1.raku b/challenge-248/joelle-maslak/raku/ch-1.raku new file mode 100644 index 0000000000..a9f3bedcfc --- /dev/null +++ b/challenge-248/joelle-maslak/raku/ch-1.raku @@ -0,0 +1,42 @@ +#!/usr/bin/env raku +use v6.d; + +# +# Copyright © 2023 Joelle Maslak +# All Rights Reserved - See License +# + +sub MAIN(Str:D $str = "loveleetcode", Str:D $char = "e") { + if $char.chars ≠ 1 { + say("You must provide exactly one letter as your character input."); + return 1; + } + my @c = $str.split("", :skip-empty); + my @positions; + for @c.kv -> $i, $letter { + @positions.push($i) if $letter eq $char; + } + + if @positions.elems == 0 { + say("The letter '$char' is not found."); + return 2; + } + + my $prev = @c.elems; + my $next = shift @positions; + my @output; + for @c.keys -> $i { + if $next == $i { + @output.push: 0; + $prev = $next; + $next = @positions.elems ?? shift @positions !! @c.elems; + } else { + @output.push: min abs($i - $prev), abs($next - $i); + } + } + + say "(" ~ @output.join(",") ~ ")"; + return 0; +} + + diff --git a/challenge-248/joelle-maslak/raku/ch-2.raku b/challenge-248/joelle-maslak/raku/ch-2.raku new file mode 100644 index 0000000000..5fa392d9d2 --- /dev/null +++ b/challenge-248/joelle-maslak/raku/ch-2.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku +use v6.d; + +# +# Copyright © 2023 Joelle Maslak +# All Rights Reserved - See License +# + +sub MAIN(*@rows) { + my @a = gather { + if @rows.elems { + for @rows<> -> $row { + take $row.words.map({Int($^a)}).list; + } + } else { + for $*IN.lines() -> $row { + take $row.words.map({Int($^a)}).list; + } + } + } + + my $row-count = @a.elems(); + my $col-count = @a[0].elems(); + if @a.first( { $^a.elems ≠ $col-count } ) { + die("All rows must have the same number of elements"); + } + + my @b; + for 0..^($row-count - 1) -> $i { + @b.push: []; + for 0..^($col-count - 1) -> $k { + @b[*-1].push: [+] cross(($i, $i+1),($k, $k+1)).map( { @a[$^a[0]; $^a[1]] } ); + } + say join " ", @b[*-1].map: { sprintf("%4d", $^a) }; + } +} + diff --git a/challenge-248/packy-anderson/README.md b/challenge-248/packy-anderson/README.md index 319baacb3d..f08eb862a3 100644 --- a/challenge-248/packy-anderson/README.md +++ b/challenge-248/packy-anderson/README.md @@ -8,28 +8,12 @@ Sample output ``` $ raku/ch-1.raku Example 1: -Input: @names = ('Mr. Wall', - 'Mrs. Wall', - 'Mr. Anwar', - 'Mrs. Anwar', - 'Mr. Conway', - 'Mr. Cross') -Output: - Mr. Wall -> Mr. Cross - Mrs. Wall -> Mr. Conway - Mr. Anwar -> Mr. Wall - Mrs. Anwar -> Mrs. Wall - Mr. Conway -> Mrs. Anwar - Mr. Cross -> Mr. Anwar +Input: $str = "loveleetcode", $char = "e" +Output: (3,2,1,0,1,0,0,1,2,2,1,0) Example 2: -Input: @names = ('Mr. Wall', - 'Mrs. Wall', - 'Mr. Anwar') -Output: - Mr. Wall -> Mr. Anwar - Mrs. Wall -> Mr. Wall - Mr. Anwar -> Mrs. Wall +Input: $str = "aaab", $char = "b" +Output: (3,2,1,0) ``` * [Task 2](raku/ch-2.raku) @@ -38,22 +22,28 @@ Sample output ``` $ raku/ch-2.raku Example 1: -Input: $s = 'abcdbca' -Output: 'bc' - -'bc' appears twice in $s +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: $s = 'cdeabeabfcdfabgcd' -Output: 'ab' - -'ab' and 'cd' appear three times in $s and 'ab' is lexicographically smallest. - -Example 3: -Input: $s = 'abcdeabcde' -Output: 'ab' - -'ab', 'bc', 'cd', and 'de' appear twice in $s and 'ab' is lexicographically smallest. +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] + ] ``` ## Perl @@ -64,28 +54,12 @@ Sample output ``` $ perl/ch-1.pl Example 1: -Input: @names = ('Mr. Wall', - 'Mrs. Wall', - 'Mr. Anwar', - 'Mrs. Anwar', - 'Mr. Conway', - 'Mr. Cross') -Output: - Mr. Wall -> Mrs. Anwar - Mrs. Wall -> Mr. Cross - Mr. Anwar -> Mr. Conway - Mrs. Anwar -> Mr. Wall - Mr. Conway -> Mr. Anwar - Mr. Cross -> Mrs. Wall +Input: $str = "loveleetcode", $char = "e" +Output: (3,2,1,0,1,0,0,1,2,2,1,0) Example 2: -Input: @names = ('Mr. Wall', - 'Mrs. Wall', - 'Mr. Anwar') -Output: - Mr. Wall -> Mr. Anwar - Mrs. Wall -> Mr. Wall - Mr. Anwar -> Mrs. Wall +Input: $str = "aaab", $char = "b" +Output: (3,2,1,0) ``` * [Task 2](perl/ch-2.pl) @@ -94,22 +68,28 @@ Sample output ``` $ perl/ch-2.pl Example 1: -Input: $s = 'abcdbca' -Output: 'bc' - -'bc' appears twice in $s +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: $s = 'cdeabeabfcdfabgcd' -Output: 'ab' - -'ab' and 'cd' appear three times in $s and 'ab' is lexicographically smallest. - -Example 3: -Input: $s = 'abcdeabcde' -Output: 'ab' - -'ab', 'bc', 'cd', and 'de' appear twice in $s and 'ab' is lexicographically smallest. +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] + ] ``` ## Guest Language: Python @@ -118,4 +98,4 @@ Output: 'ab' ## Blog Post -[Perl Weekly Challenge: Writing Letter Pairs to Santa](https://packy.dardan.com/b/FK) +[Perl Weekly Challenge: The Shortest Distance between Submatrix Sums](https://packy.dardan.com/b/Ff) diff --git a/challenge-248/packy-anderson/blog.txt b/challenge-248/packy-anderson/blog.txt new file mode 100644 index 0000000000..0f52388fb9 --- /dev/null +++ b/challenge-248/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Ff
\ No newline at end of file diff --git a/challenge-248/packy-anderson/perl/ch-1.pl b/challenge-248/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..44ac4d4e6d --- /dev/null +++ b/challenge-248/packy-anderson/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( min ); + +sub shortestDistance($str, $char) { + # split the string into an array of characters + my @strchar = split(//, $str); + # find the positions of the target $char + my @pos = grep { $strchar[$_] eq $char } 0 .. $#strchar; + + my @output; + foreach my $i ( 0 .. $#strchar ) { + # find the distances + my @distance = map { abs($i - $_) } @pos; + # find the minimum distance + push @output, min(@distance); + } + return @output; +} + +sub solution($str, $char) { + say qq{Input: \$str = "$str", \$char = "$char"}; + my @output = shortestDistance($str, $char); + say 'Output: (' . join(',', @output) . ')'; +} + +say "Example 1:"; +solution("loveleetcode", "e"); + +say "\nExample 2:"; +solution("aaab", "b");
\ No newline at end of file diff --git a/challenge-248/packy-anderson/perl/ch-2.pl b/challenge-248/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..c7af82634e --- /dev/null +++ b/challenge-248/packy-anderson/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +use v5.38; + +sub submatrixSum(@a) { + my $M = $#a; # rows + my $N = $#{$a[0]}; # columns + # we are ASSUMING the matrix is consistent with + # each row having the same number of columns + my @b; + foreach my $i ( 0 .. $M - 1 ) { + push @b, []; + foreach my $k ( 0 .. $N - 1 ) { + $b[$i]->[$k] = $a[$i]->[$k] + $a[$i]->[$k+1] + + $a[$i+1]->[$k] + $a[$i+1]->[$k+1]; + } + } + return @b; +} + +sub formatMatrix($matrix, $indent) { + my @output; + foreach my $row ( @$matrix ) { + my $output_row = q{ } x $indent . " ["; + $output_row .= join(', ', @$row) . "]"; + push @output, $output_row; + } + return "[\n" + . join(",\n", @output) + . "\n" + . q{ } x $indent . "]"; +} + +sub solution(@a) { + say 'Input: $a = ' . formatMatrix(\@a, 13); + my @b = submatrixSum(@a); + say 'Output: $b = ' . formatMatrix(\@b, 13); +} + +say "Example 1:"; +solution([1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12]); + +say "\nExample 2:"; +solution([1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1]); diff --git a/challenge-248/packy-anderson/python/ch-1.py b/challenge-248/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..e4f309808e --- /dev/null +++ b/challenge-248/packy-anderson/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +def shortestDistance(s, c): + # split the string into an array of characters + strchar = list(s) + # find the positions of the target char + pos = [ x for x in range(len(s)) if strchar[x] == c ] + + output = [] + for i in range(len(s)): + # find the distances + distance = [ abs(i - p) for p in pos ] + # find the minimum distance + output.append( min(distance) ) + return output + +def comma_join(arr): + return ','.join(map(lambda i: str(i), arr)) + +def solution(s, c): + print(f'Input: $str = "{s}", $char = "{c}"') + output = shortestDistance(s, c) + print(f'Output: ({comma_join(output)})') + +print('Example 1:') +solution("loveleetcode", "e") + +print('\nExample 2:') +solution("aaab", "b")
\ No newline at end of file diff --git a/challenge-248/packy-anderson/python/ch-2.py b/challenge-248/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..a31b91af1a --- /dev/null +++ b/challenge-248/packy-anderson/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +def submatrixSum(a): + # subtract 1 because we're 0-indexed + M = len(a) - 1 # rows + N = len(a[0]) - 1 # columns + # we are ASSUMING the matrix is consistent with + # each row having the same number of columns + b = [] + for i in range(M): + row = [] + for k in range(N): + row.append( a[i ][k] + a[i ][k+1] + + a[i+1][k] + a[i+1][k+1] ) + b.append(row) + return b + +def formatMatrix(matrix, indent=13): + output = [] + for row in matrix: + output_row = ' ' * indent + ' [' + output_row += ', '.join(map(lambda i: str(i), row)) + output_row += ']' + output.append(output_row) + + return( + "[\n" + ",\n".join(output) + "\n" + + ' ' * indent + ']' + ) + +def solution(a): + print(f'Input: $a = {formatMatrix(a)}') + b = submatrixSum(a) + print(f'Output: $b = {formatMatrix(b)}') + +print('Example 1:') +solution([ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] + ]) + +print('\nExample 2:') +solution([ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1] + ]) diff --git a/challenge-248/packy-anderson/raku/ch-1.raku b/challenge-248/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..d11a06f557 --- /dev/null +++ b/challenge-248/packy-anderson/raku/ch-1.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku +use v6; + +sub shortestDistance($str, $char) { + # split the string into an array of characters + my @strchar = $str.split('', :skip-empty); + # find the positions of the target $char + my @pos = (0 .. @strchar.end).grep: { @strchar[$_] eq $char }; + + my @output; + for 0 .. @strchar.end -> $i { + # find the distances + my @distance = @pos.map: { abs($i - $_) }; + # find the minimum distance + @output.push( @distance.min ); + } + return @output; +} + +sub solution($str, $char) { + say qq{Input: \$str = "$str", \$char = "$char"}; + my @output = shortestDistance($str, $char); + say 'Output: (' ~ @output.join(',') ~ ')'; +} + +say "Example 1:"; +solution("loveleetcode", "e"); + +say "\nExample 2:"; +solution("aaab", "b"); diff --git a/challenge-248/packy-anderson/raku/ch-2.raku b/challenge-248/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..0c47b4f22c --- /dev/null +++ b/challenge-248/packy-anderson/raku/ch-2.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku +use v6; + +sub submatrixSum(@a) { + # subtract 1 because we're 0-indexed + my $M = @a.elems - 1; # rows + my $N = @a[0].elems - 1; # columns + # we are ASSUMING the matrix is consistent with + # each row having the same number of columns + my @b; + for 0 .. $M - 1 -> $i { + for 0 .. $N - 1 -> $k { + @b[$i;$k] = @a[$i; $k] + @a[$i; $k+1] + + @a[$i+1;$k] + @a[$i+1;$k+1]; + } + } + return @b; +} + +sub formatMatrix(@matrix, $indent) { + my @output; + for @matrix -> @row { + my $output_row = q{ } x $indent ~ " ["; + $output_row ~= @row.join(', ') ~ "]"; + @output.push($output_row); + } + return "[\n" + ~ @output.join(",\n") + ~ "\n" + ~ q{ } x $indent ~ "]"; +} + +sub solution(@a) { + say 'Input: $a = ' ~ formatMatrix(@a, 13); + my @b = submatrixSum(@a); + say 'Output: $b = ' ~ formatMatrix(@b, 13); +} + +say "Example 1:"; +solution([ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] + ]); + +say "\nExample 2:"; +solution([ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1] + ]); |
