aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-242/packy-anderson/README.md83
-rw-r--r--challenge-242/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-242/packy-anderson/perl/ch-1.pl58
-rwxr-xr-xchallenge-242/packy-anderson/perl/ch-2.pl32
-rwxr-xr-xchallenge-242/packy-anderson/python/ch-1.py53
-rwxr-xr-xchallenge-242/packy-anderson/python/ch-2.py28
-rwxr-xr-xchallenge-242/packy-anderson/raku/ch-1.raku54
-rwxr-xr-xchallenge-242/packy-anderson/raku/ch-2.raku29
8 files changed, 280 insertions, 58 deletions
diff --git a/challenge-242/packy-anderson/README.md b/challenge-242/packy-anderson/README.md
index f92e561e12..5ade739739 100644
--- a/challenge-242/packy-anderson/README.md
+++ b/challenge-242/packy-anderson/README.md
@@ -8,21 +8,19 @@ Sample output
```
$ raku/ch-1.raku
Example 1:
-Input: @nums = (0, 1, 4, 6, 7, 10)
- $diff = 3
-Output: 2
-
-(1, 2, 4) is an arithmetic triplet because both 7 - 4 = 3 and 4 - 1 = 3
-(2, 4, 5) is an arithmetic triplet because both 10 - 7 = 3 and 7 - 4 = 3
-
+Input: @arr1 = (1, 2, 3)
+ @arr2 = (2, 4, 6)
+Output: ([1, 3], [4, 6])
+(1, 2, 3) has 2 members (1, 3) missing from the array (2, 4, 6)
+(2, 4, 6) has 2 members (4, 6) missing from the array (1, 2, 3)
Example 2:
-Input: @nums = (4, 5, 6, 7, 8, 9)
- $diff = 2
-Output: 2
+Input: @arr1 = (1, 2, 3, 3)
+ @arr2 = (1, 1, 2, 2)
+Output: ([3])
-(0, 2, 4) is an arithmetic triplet because both 8 - 6 = 2 and 6 - 4 = 2
-(1, 3, 5) is an arithmetic triplet because both 9 - 7 = 2 and 7 - 5 = 2
+(1, 2, 3, 3) has 2 members (3, 3) missing from the array (1, 1, 2, 2)
+(1, 1, 2, 2) has 0 members missing from the array (1, 2, 3, 3)
```
* [Task 2](raku/ch-2.raku)
@@ -31,23 +29,12 @@ Sample output
```
$ raku/ch-2.raku
Example 1:
-Input: @int = (11, 8, 27, 4)
-Output: (11, 4, 8, 27)
-
-Prime factors of 11 => 11
-Prime factors of 4 => 2, 2
-Prime factors of 8 => 2, 2, 2
-Prime factors of 27 => 3, 3, 3
+Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0])
+Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1])
Example 2:
-Input: @int = (2, 4, 8, 12, 11)
-Output: (2, 11, 4, 8, 12)
-
-Prime factors of 2 => 2
-Prime factors of 11 => 11
-Prime factors of 4 => 2, 2
-Prime factors of 8 => 2, 2, 2
-Prime factors of 12 => 2, 2, 3
+Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])
+Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])
```
## Perl
@@ -58,46 +45,26 @@ Sample output
```
$ perl/ch-1.pl
Example 1:
-Input: @nums = (0, 1, 4, 6, 7, 10)
- $diff = 3
-Output: 2
-
-(1, 2, 4) is an arithmetic triplet because both 7 - 4 = 3 and 4 - 1 = 3
-(2, 4, 5) is an arithmetic triplet because both 10 - 7 = 3 and 7 - 4 = 3
-
+Input: @arr1 = (1, 2, 3)
+ @arr2 = (2, 4, 6)
+Output: ([1, 3], [4, 6])
+(1, 2, 3) has 2 members (1, 3) missing from the array (2, 4, 6)
+(2, 4, 6) has 2 members (4, 6) missing from the array (1, 2, 3)
Example 2:
-Input: @nums = (4, 5, 6, 7, 8, 9)
- $diff = 2
-Output: 2
+Input: @arr1 = (1, 2, 3, 3)
+ @arr2 = (1, 1, 2, 2)
+Output: ([3])
-(0, 2, 4) is an arithmetic triplet because both 8 - 6 = 2 and 6 - 4 = 2
-(1, 3, 5) is an arithmetic triplet because both 9 - 7 = 2 and 7 - 5 = 2
+(1, 2, 3, 3) has 2 members (3, 3) missing from the array (1, 1, 2, 2)
+(1, 1, 2, 2) has 0 members missing from the array (1, 2, 3, 3)
```
* [Task 2](perl/ch-2.pl)
Sample output
```
-$ perl/ch-2.pl
-Example 1:
-Input: @int = (11, 8, 27, 4)
-Output: (11, 4, 8, 27)
-Prime factors of 11 => 11
-Prime factors of 4 => 2, 2
-Prime factors of 8 => 2, 2, 2
-Prime factors of 27 => 3, 3, 3
-
-Example 2:
-Input: @int = (2, 4, 8, 12, 11)
-Output: (2, 11, 4, 8, 12)
-
-Prime factors of 2 => 2
-Prime factors of 11 => 11
-Prime factors of 4 => 2, 2
-Prime factors of 8 => 2, 2, 2
-Prime factors of 12 => 2, 2, 3
```
## Guest Language: Python
@@ -106,4 +73,4 @@ Prime factors of 12 => 2, 2, 3
## Blog Post
-[Perl Weekly Challenge: Triplets Prime](https://packy.dardan.com/2023/10/30/perl-weekly-challenge-triplets-prime/)
+[Perl Weekly Challenge: Flip the Missing Matrix Members](https://packy.dardan.com/2023/11/05/perl-weekly-challenge-flip-the-missing-matrix-members/)
diff --git a/challenge-242/packy-anderson/blog.txt b/challenge-242/packy-anderson/blog.txt
new file mode 100644
index 0000000000..d677ee518c
--- /dev/null
+++ b/challenge-242/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/2023/11/05/perl-weekly-challenge-flip-the-missing-matrix-members/ \ No newline at end of file
diff --git a/challenge-242/packy-anderson/perl/ch-1.pl b/challenge-242/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..4270114da4
--- /dev/null
+++ b/challenge-242/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl
+use v5.38;
+
+use List::Util qw( uniq );
+
+sub findMissing {
+ my ($source, $target, $output, $explanation) = @_;
+
+ # convert the target into a hash with each element as keys
+ my %targetHash = map { $_ => 1 } @$target;
+
+ # see which elements in the source are not in the target
+ my @missing;
+ foreach my $elem ( @$source ) {
+ if (! exists $targetHash{$elem}) {
+ push @missing, $elem;
+ }
+ }
+
+ # format output explaining what we found
+ $$explanation .= "\n(" . join(', ', @$source) . ") has ";
+ $$explanation .= scalar(@missing);
+ $$explanation .= @missing == 1 ? ' member ' : ' members ';
+ if (scalar(@missing) > 0) {
+ $$explanation .= '(' . join(', ', @missing) . ') ';
+ push @$output, [ uniq @missing ];
+ }
+ $$explanation .= 'missing from the array ';
+ $$explanation .= '(' . join(', ', @$target) . ')';
+}
+
+sub findSolution {
+ my($arr1, $arr2, $output, $explanation) = @_;
+ findMissing($arr1, $arr2, $output, $explanation);
+ findMissing($arr2, $arr1, $output, $explanation);
+}
+
+sub solution {
+ my($arr1, $arr2) = @_;
+ say 'Input: @arr1 = (' . join(', ', @$arr1) . ')';
+ say ' @arr2 = (' . join(', ', @$arr2) . ')';
+
+ my @output = ();
+ my $explanation;
+ findSolution($arr1, $arr2, \@output, \$explanation);
+ my @formatted_subarray;
+ foreach my $subarray ( @output ) {
+ push @formatted_subarray, '[' . join(', ', @$subarray) . ']';
+ }
+ say 'Output: (' . join(', ', @formatted_subarray) . ')';
+ say $explanation;
+}
+
+say "Example 1:";
+solution([1, 2, 3], [2, 4, 6]);
+
+say "Example 2:";
+solution([1, 2, 3, 3], [1, 1, 2, 2]); \ No newline at end of file
diff --git a/challenge-242/packy-anderson/perl/ch-2.pl b/challenge-242/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..cb6a6b7819
--- /dev/null
+++ b/challenge-242/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub flipMatrix {
+ my(@matrix) = @_;
+ foreach my $subarray ( @matrix ) {
+ $subarray = [ map { abs($_ - 1) } reverse @$subarray ];
+ }
+ return @matrix;
+}
+
+sub formatArray {
+ my(@matrix) = @_;
+ my @formatted;
+ foreach my $subarray ( @matrix ) {
+ push @formatted, '[' . join(', ', @$subarray) . ']';
+ }
+ return '(' . join(', ', @formatted) . ')';
+}
+
+sub solution {
+ my(@matrix) = @_;
+ say 'Input: @matrix = ' . formatArray(@matrix);
+ my @output = flipMatrix(@matrix);
+ say 'Output: ' . formatArray(@output);
+}
+
+say "Example 1:";
+solution([1, 1, 0], [1, 0, 1], [0, 0, 0]);
+
+say "\nExample 2:";
+solution([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]); \ No newline at end of file
diff --git a/challenge-242/packy-anderson/python/ch-1.py b/challenge-242/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..8126d38ebf
--- /dev/null
+++ b/challenge-242/packy-anderson/python/ch-1.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def findMissing(source, target, output, explanation):
+ # convert the target into a map with each element as keys
+ targetMap = { x: 1 for x in target }
+
+ # see which elements in the source are not in the target
+ missing = []
+ for elem in source:
+ if not elem in targetMap:
+ missing.append(elem)
+
+ # format output explaining what we found
+ explanation += "\n(" + comma_join(source) + ") has "
+ explanation += str(len(missing))
+ explanation += ' member ' if len(missing) == 1 \
+ else ' members '
+ if (len(missing) > 0):
+ explanation += '(' + comma_join(missing) + ') '
+ output.append(set(missing))
+ explanation += 'missing from the array '
+ explanation += '(' + comma_join(target) + ')'
+ return explanation
+
+
+def findSolution(arr1, arr2, output):
+ explanation = ''
+ explanation = findMissing(arr1, arr2, output, explanation)
+ explanation = findMissing(arr2, arr1, output, explanation)
+ return explanation
+
+
+def solution(arr1, arr2):
+ print(f'Input: @arr1 = ({comma_join(arr1)})')
+ print(f' @arr2 = ({comma_join(arr2)})')
+
+ output = []
+ explanation = findSolution(arr1, arr2, output)
+ formatted_subarray = []
+ for subarray in output:
+ formatted_subarray.append('[' + comma_join(subarray) + ']')
+ print(f'Output: ({comma_join(formatted_subarray)})')
+ print(explanation)
+
+
+print('Example 1:')
+solution([1, 2, 3], [2, 4, 6])
+
+print('\nExample 2:')
+solution([1, 2, 3, 3], [1, 1, 2, 2]) \ No newline at end of file
diff --git a/challenge-242/packy-anderson/python/ch-2.py b/challenge-242/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..dd5dc075ab
--- /dev/null
+++ b/challenge-242/packy-anderson/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+def flipMatrix(matrix):
+ for index in range(0, len(matrix)):
+ matrix[index] = map(
+ lambda i: abs(i - 1), reversed(matrix[index])
+ )
+ return matrix
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def formatArray(matrix):
+ formatted = []
+ for subarray in matrix:
+ formatted.append('[' + comma_join(subarray) + ']')
+ return '(' + comma_join(formatted) + ')'
+
+def solution(matrix):
+ print(f'Input: @matrix = {formatArray(matrix)}')
+ output = flipMatrix(matrix)
+ print(f'Output: {formatArray(output)}')
+
+print('Example 1:')
+solution([[1, 1, 0], [1, 0, 1], [0, 0, 0]])
+
+print('\nExample 2:')
+solution([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]) \ No newline at end of file
diff --git a/challenge-242/packy-anderson/raku/ch-1.raku b/challenge-242/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..0882c9b5a5
--- /dev/null
+++ b/challenge-242/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,54 @@
+#!/usr/bin/env raku
+use v6;
+
+use Lingua::Conjunction;
+
+sub findMissing(@source, @target, @output, $explanation is rw) {
+ # convert the target into a hash with each element as keys
+ my %targetHash = @target.map: * => 1;
+
+ # see which elements in the source are not in the target
+ my @missing;
+ for @source -> $elem {
+ if (%targetHash{$elem}:!exists) {
+ @missing.push($elem);
+ }
+ }
+
+ # format output explaining what we found
+ $explanation ~= "\n(" ~ @source.join(', ') ~ ") has ";
+ $explanation ~= @missing.elems;
+ $explanation ~= conjunction(@missing, :str(' member[|s] '));
+ if (@missing.elems > 0) {
+ $explanation ~= '(' ~ @missing.join(', ') ~ ') ';
+ @output.push(@missing.unique);
+ }
+ $explanation ~= 'missing from the array ';
+ $explanation ~= '(' ~ @target.join(', ') ~ ')';
+}
+
+sub findSolution(@arr1, @arr2, @output, $explanation is rw) {
+ findMissing(@arr1, @arr2, @output, $explanation);
+ findMissing(@arr2, @arr1, @output, $explanation);
+}
+
+sub solution(@arr1, @arr2) {
+ say 'Input: @arr1 = (' ~ @arr1.join(', ') ~ ')';
+ say ' @arr2 = (' ~ @arr2.join(', ') ~ ')';
+
+ my @output = ();
+ my $explanation;
+ findSolution(@arr1, @arr2, @output, $explanation);
+ my @formatted_subarray;
+ for @output -> @subarray {
+ @formatted_subarray.push('[' ~ @subarray.join(', ') ~ ']');
+ }
+ say 'Output: (' ~ @formatted_subarray.join(', ') ~ ')';
+ say $explanation;
+}
+
+say "Example 1:";
+solution([1, 2, 3], [2, 4, 6]);
+
+say "Example 2:";
+solution([1, 2, 3, 3], [1, 1, 2, 2]); \ No newline at end of file
diff --git a/challenge-242/packy-anderson/raku/ch-2.raku b/challenge-242/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..e14700ea8d
--- /dev/null
+++ b/challenge-242/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,29 @@
+#!/usr/bin/env raku
+use v6;
+
+sub flipMatrix(@matrix) {
+ for @matrix -> @subarray {
+ @subarray = @subarray.reverse.map: (* - 1).abs;
+ }
+ return @matrix;
+}
+
+sub formatArray(@matrix) {
+ my @formatted;
+ for @matrix -> @subarray {
+ @formatted.push('[' ~ @subarray.join(', ') ~ ']');
+ }
+ return '(' ~ @formatted.join(', ') ~ ')';
+}
+
+sub solution(**@matrix) {
+ say 'Input: @matrix = ' ~ formatArray(@matrix);
+ my @output = flipMatrix(@matrix);
+ say 'Output: ' ~ formatArray(@output);
+}
+
+say "Example 1:";
+solution([1, 1, 0], [1, 0, 1], [0, 0, 0]);
+
+say "\nExample 2:";
+solution([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]); \ No newline at end of file