diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-12 17:50:02 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-12 17:50:02 +0000 |
| commit | 6526fb7a0d08959eb1b17d5deb966b9d2ddb67ec (patch) | |
| tree | 49f6f709fbc3a5169a05d179a5ccf0d8422c91ba | |
| parent | 8c6ff6e97f8b65ee3cade730a97f2964e4213dcd (diff) | |
| parent | f13f1e6f026217be9a4196f9b4b8b453c43d3390 (diff) | |
| download | perlweeklychallenge-club-6526fb7a0d08959eb1b17d5deb966b9d2ddb67ec.tar.gz perlweeklychallenge-club-6526fb7a0d08959eb1b17d5deb966b9d2ddb67ec.tar.bz2 perlweeklychallenge-club-6526fb7a0d08959eb1b17d5deb966b9d2ddb67ec.zip | |
Merge pull request #9034 from jo-37/contrib
Solutions to challenge 242
| -rw-r--r-- | challenge-242/jo-37/Blog.md | 44 | ||||
| -rw-r--r-- | challenge-242/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-242/jo-37/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-242/jo-37/perl/ch-2.pl | 66 |
4 files changed, 173 insertions, 0 deletions
diff --git a/challenge-242/jo-37/Blog.md b/challenge-242/jo-37/Blog.md new file mode 100644 index 0000000000..f3ae3e7b96 --- /dev/null +++ b/challenge-242/jo-37/Blog.md @@ -0,0 +1,44 @@ +# Missing Reversed Inversions +## Task 1: Missing Members +> You are given two arrays of integers. +> Write a script to find out the missing members in each other arrays. + +In set theory, given two sets A and B, this task asks for the difference sets A \ B and B \ A. + +There are many approches to solve this with Perl. +The most basic way would be to use hashes as representations of sets. +Hash slices come *very* handy when an array shall be convertet to as set: +``` +my @a = (...); +my %ha; +@ha{@a} = (); +``` +Now `%h` is a hash with all members of `@a` as keys. +Taking the set difference A \ B to a second array `@b` is as easy as: +``` +@b = (...); +delete @ha{@b}; +``` +However, I prefer a more functional approach. +Using PDL ndarrays of unique sorted numbers as set representations. +Then we may use PDL set operation in a natural way to solve the task: +``` +$m1 = long(...)->uniq; +``` +makes `$m1` a 1-d ndarray representing a set and +``` +setdiff_sorted($m1, $m2); +``` +gives M1 \ M2. +Same result, but better readable. +## Task 2: Flip Matrix +> You are given `n x n` binary matrix. +> Write a script to flip the given matrix as below. + +Again, with PDL this task is almost trivial. +Using `PDL::NiceSlice` we may reverse a dimension using the notation `(-1:0)`. +Reverting a value is done with the unary negation operator `!`. +Performing the "flip matrix" operation on a 2-d ndarray `$m` thus is as easy as: +``` +!$m->(-1:0) +```
\ No newline at end of file diff --git a/challenge-242/jo-37/blog.txt b/challenge-242/jo-37/blog.txt new file mode 100644 index 0000000000..42da099a21 --- /dev/null +++ b/challenge-242/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-242/jo-37/Blog.md diff --git a/challenge-242/jo-37/perl/ch-1.pl b/challenge-242/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..8b55510a69 --- /dev/null +++ b/challenge-242/jo-37/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [-tests] [--] [ARR1 ARR2] + +-examples + run the examples from the challenge + +-tests + run some tests + +ARR1 ARR2 + two comma separated lists of integers + +EOS + + +### Input and Output + +say "(@{[missing_members(map [split /,/, $_], @ARGV)]})"; + + +### Implementation + +# Convert input arrays to unique, sorted piddles and build both set +# differences. Return the two difference sets as piddles in list +# context or a ref to an AoA in scalar context. + +sub missing_members { + my @a = map long($_)->uniq, @_; + my @diff = map scalar setdiff_sorted($a[$_], $a[!$_]), 0, 1; + wantarray ? @diff : [map $_->unpdl, @diff]; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is scalar missing_members([1, 2, 3], [2, 4, 6]), + [[1, 3], [4, 6]], 'example 1'; + is scalar missing_members([1, 2, 3, 3], [1, 1, 2, 2]), + [[3], []], 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-242/jo-37/perl/ch-2.pl b/challenge-242/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..09af191283 --- /dev/null +++ b/challenge-242/jo-37/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 1; +usage: $0 [-examples] [-tests] [M] + +-examples + run the examples from the challenge + +-tests + run some tests + +M + matrix in a representation as accepted by the PDL string + constructor, e.g. '1 1 0; 0 1 1; 0 0 1' + +EOS + + +### Input and Output + +say flip_matrix(@ARGV); + + +### Implementation + +# Reverse rows and invert values. +sub flip_matrix { + !long(@_)->(-1:0); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is flip_matrix([1, 1, 0], [0, 1, 1], [0, 0, 1])->unpdl, + [[1, 0, 0], [0, 0, 1], [0, 1, 1]], 'intro'; + is flip_matrix([1, 1, 0], [1, 0, 1], [0, 0, 0])->unpdl, + [[1, 0, 0], [0, 1, 0], [1, 1, 1]], 'example 1'; + is flip_matrix([1, 1, 0, 0], [1, 0, 0, 1], + [0, 1, 1, 1], [1, 0, 1, 0])->unpdl, + [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], + 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is flip_matrix([0, 0, 1], [1, 1, 0])->unpdl, + [[0, 1, 1], [1, 0, 0]], 'non quadratic matrix'; + } + + done_testing; + exit; +} |
