aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-21 09:44:47 +0000
committerGitHub <noreply@github.com>2023-12-21 09:44:47 +0000
commit3f6a4dce3fdbe28fdb83c5e1ede37f9cacfc1faf (patch)
tree3f54f9f125ac6dd58f540e08800c8302514e7c8a
parentf89e715e4679d28318e8660608d44840a6468cbe (diff)
parent42d94c4d037146a9de2471b4ad51569eb0296e26 (diff)
downloadperlweeklychallenge-club-3f6a4dce3fdbe28fdb83c5e1ede37f9cacfc1faf.tar.gz
perlweeklychallenge-club-3f6a4dce3fdbe28fdb83c5e1ede37f9cacfc1faf.tar.bz2
perlweeklychallenge-club-3f6a4dce3fdbe28fdb83c5e1ede37f9cacfc1faf.zip
Merge pull request #9271 from packy/master
Challenge 248 solutions by Packy Anderson
-rw-r--r--challenge-248/packy-anderson/README.md118
-rw-r--r--challenge-248/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-248/packy-anderson/perl/ch-1.pl32
-rwxr-xr-xchallenge-248/packy-anderson/perl/ch-2.pl48
-rwxr-xr-xchallenge-248/packy-anderson/python/ch-1.py29
-rwxr-xr-xchallenge-248/packy-anderson/python/ch-2.py49
-rwxr-xr-xchallenge-248/packy-anderson/raku/ch-1.raku30
-rwxr-xr-xchallenge-248/packy-anderson/raku/ch-2.raku52
8 files changed, 290 insertions, 69 deletions
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]
+ ]);