From 7da271d93fd1cea187d7527948d029ea7e7f6398 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 6 Nov 2023 06:48:37 +0000 Subject: Challenge 242 Solutions (Raku) --- challenge-242/mark-anderson/raku/ch-1.raku | 12 +++++++++++ challenge-242/mark-anderson/raku/ch-2.raku | 32 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 challenge-242/mark-anderson/raku/ch-1.raku create mode 100644 challenge-242/mark-anderson/raku/ch-2.raku diff --git a/challenge-242/mark-anderson/raku/ch-1.raku b/challenge-242/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..145d04bdc5 --- /dev/null +++ b/challenge-242/mark-anderson/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku +use Test; + +is-deeply missing-members([1,2,3], [2,4,6]), ([1,3], [4,6]); +is-deeply missing-members([1,2,3,3], [1,1,2,2]), ([3],); + +sub missing-members(@a, @b) +{ + # @a (^) @b gives the result in 1 list so I'm doing it like this 🤷 + + grep { .Bool }, ((@a (-) @b), (@b (-) @a))>>.keys>>.sort>>.Array +} diff --git a/challenge-242/mark-anderson/raku/ch-2.raku b/challenge-242/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..c83ac20266 --- /dev/null +++ b/challenge-242/mark-anderson/raku/ch-2.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku +use Test; +use Benchy; + +is-deeply flip-matrix(<1 1 0>, <1 0 1>, <0 0 0>), + ((1,0,0), (0,1,0), (1,1,1))>>.Seq; + +is-deeply flip-matrix(<1 1 0 0>, <1 0 0 1>, <0 1 1 1>, <1 0 1 0>), + ((1,1,0,0), (0,1,1,0), (0,0,0,1), (1,0,1,0))>>.Seq; + +benchmark(); + +sub flip-matrix(+@m) +{ + map -> $r { map -> $c { @m[$r;$c] +^ 1 }, (@m.end...0) }, ^@m +} + +sub flip-matrix-slow(+@m) +{ + map { reverse $_ >>+^>> 1 }, @m +} + +sub benchmark +{ + my @m = map { (0,1).roll(100_000) }, ^100_000; + + b 10, { flip-matrix-slow(@m) }, { flip-matrix(@m) } + + # Old: 3.332146938s + # New: 0.8259856s + # NEW version is 4.03x faster +} -- cgit From 6673e09acaa2d068ca044f1edfaf31ced19e4738 Mon Sep 17 00:00:00 2001 From: Matthias Muth Date: Mon, 6 Nov 2023 08:14:17 +0100 Subject: Add blog.txt --- challenge-241/matthias-muth/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-241/matthias-muth/blog.txt diff --git a/challenge-241/matthias-muth/blog.txt b/challenge-241/matthias-muth/blog.txt new file mode 100644 index 0000000000..8db6d3085d --- /dev/null +++ b/challenge-241/matthias-muth/blog.txt @@ -0,0 +1 @@ +https://github.com/MatthiasMuth/perlweeklychallenge-club/tree/muthm-241/challenge-241/matthias-muth#readme -- cgit From 1cabedfda2a54d48a6cf99ab423cd64627c985b3 Mon Sep 17 00:00:00 2001 From: PerlMonk-Athanasius Date: Mon, 6 Nov 2023 17:36:13 +1000 Subject: Fixed week number --- challenge-241/athanasius/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-241/athanasius/perl/ch-2.pl b/challenge-241/athanasius/perl/ch-2.pl index 8dc37497d3..3417309a3b 100644 --- a/challenge-241/athanasius/perl/ch-2.pl +++ b/challenge-241/athanasius/perl/ch-2.pl @@ -64,7 +64,7 @@ BEGIN #------------------------------------------------------------------------------- { $| = 1; - print "\nChallenge 240, Task #2: Prime Order (Perl)\n\n"; + print "\nChallenge 241, Task #2: Prime Order (Perl)\n\n"; } #=============================================================================== -- cgit From d4e6cf23a291a96dbf73f176f62da73bcd727fd7 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 6 Nov 2023 09:58:24 +0000 Subject: w242 - Task 1 & 2 --- challenge-242/perlboy1967/perl/ch1.pl | 52 +++++++++++++++++++++++++++++++++++ challenge-242/perlboy1967/perl/ch2.pl | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100755 challenge-242/perlboy1967/perl/ch1.pl create mode 100755 challenge-242/perlboy1967/perl/ch2.pl diff --git a/challenge-242/perlboy1967/perl/ch1.pl b/challenge-242/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..0506c5524b --- /dev/null +++ b/challenge-242/perlboy1967/perl/ch1.pl @@ -0,0 +1,52 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 242 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-242 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Missing Members +Submitted by: Mohammad S Anwar + +You are given two arrays of integers. + +Write a script to find out the missing members in each other arrays. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use List::MoreUtils qw(uniq); + +sub missingMembers { + my ($n,%n,$r) = ($#_ + 1); + + # Collect all numbers across input + # and store against their input index + for my $i (0 .. $n) { + for (uniq($_[$i]->@*)) { + push(@{$n{$_}},$i); + } + } + + # Find uniq numbers across input + # and create output + for my $i (sort { $a <=> $b } keys %n) { + if ($n{$i}->@* < $n) { + map { push($r->[$_]->@*,$i) } $n{$i}->@* ; + } + } + return $r; +} + +is(missingMembers([1,2,3],[2,4,6]), + [[1,3],[4,6]]); +is(missingMembers([0,1,2,3],[1,2,3,4],[2,3,4,5]), + [[0,1],[1,4],[4,5]]); + +done_testing; diff --git a/challenge-242/perlboy1967/perl/ch2.pl b/challenge-242/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..05698ac8c6 --- /dev/null +++ b/challenge-242/perlboy1967/perl/ch2.pl @@ -0,0 +1,52 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 242 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-242 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Flip Matrix +Submitted by: Mohammad S Anwar + +You are given n x n binary matrix. + +Write a script to flip the given matrix. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use Data::Printer; + +sub flipMatrix { + my @r; + + for (@_) { + push(@r,[map { ++$_ % 2 } reverse @$_]); + } + + [@r]; +} + +is(flipMatrix([1,1,0], + [0,1,1], + [0,0,1]), + [[1,0,0], + [0,0,1], + [0,1,1]]); + +is(flipMatrix([1,1,0,0], + [1,0,0,1], + [0,1,1,1], + [1,0,1,0]), + [[1,1,0,0], + [0,1,1,0], + [0,0,0,1], + [1,0,1,0]]); + +done_testing; -- cgit From 21ea6d03380603d1ca0de2e024d583ef5861d9a9 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 6 Nov 2023 10:16:32 +0000 Subject: Task 2 - Compact code --- challenge-242/perlboy1967/perl/ch2.pl | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/challenge-242/perlboy1967/perl/ch2.pl b/challenge-242/perlboy1967/perl/ch2.pl index 05698ac8c6..3c4db8904a 100755 --- a/challenge-242/perlboy1967/perl/ch2.pl +++ b/challenge-242/perlboy1967/perl/ch2.pl @@ -21,32 +21,13 @@ use common::sense; use Test2::V0; -use Data::Printer; - sub flipMatrix { - my @r; - - for (@_) { - push(@r,[map { ++$_ % 2 } reverse @$_]); - } - - [@r]; + [ map { [map { ++$_ % 2 } reverse @$_] } @_ ]; } -is(flipMatrix([1,1,0], - [0,1,1], - [0,0,1]), - [[1,0,0], - [0,0,1], - [0,1,1]]); - -is(flipMatrix([1,1,0,0], - [1,0,0,1], - [0,1,1,1], - [1,0,1,0]), - [[1,1,0,0], - [0,1,1,0], - [0,0,0,1], - [1,0,1,0]]); +is(flipMatrix([1,1,0],[0,1,1],[0,0,1]), + [[1,0,0],[0,0,1],[0,1,1]]); +is(flipMatrix([1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]), + [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]); done_testing; -- cgit From 6ce6ed055d4fefa75c30a0090c41eb971c5f30b0 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 6 Nov 2023 10:32:11 +0000 Subject: Task 1 - Max index bug fix and add code to return [] on same --- challenge-242/perlboy1967/perl/ch1.pl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/challenge-242/perlboy1967/perl/ch1.pl b/challenge-242/perlboy1967/perl/ch1.pl index 0506c5524b..5954386f3e 100755 --- a/challenge-242/perlboy1967/perl/ch1.pl +++ b/challenge-242/perlboy1967/perl/ch1.pl @@ -28,7 +28,8 @@ sub missingMembers { # Collect all numbers across input # and store against their input index - for my $i (0 .. $n) { + for my $i (0 .. $n - 1) { + $r->[$i] = []; for (uniq($_[$i]->@*)) { push(@{$n{$_}},$i); } @@ -38,15 +39,16 @@ sub missingMembers { # and create output for my $i (sort { $a <=> $b } keys %n) { if ($n{$i}->@* < $n) { - map { push($r->[$_]->@*,$i) } $n{$i}->@* ; + map { push($r->[$_]->@*,$i) } $n{$i}->@*; } } return $r; } is(missingMembers([1,2,3],[2,4,6]), - [[1,3],[4,6]]); + [[1,3],[4,6]]); is(missingMembers([0,1,2,3],[1,2,3,4],[2,3,4,5]), - [[0,1],[1,4],[4,5]]); + [[0,1],[1,4],[4,5]]); +is(missingMembers([1],[1]),[[],[]]); done_testing; -- cgit From bb1e59574570c807c144b5466c580f3dd0461abd Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 6 Nov 2023 11:45:29 +0000 Subject: Challenge 242 part 1 --- challenge-242/simon-proctor/raku/ch-1.raku | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-242/simon-proctor/raku/ch-1.raku diff --git a/challenge-242/simon-proctor/raku/ch-1.raku b/challenge-242/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..83e98a2b51 --- /dev/null +++ b/challenge-242/simon-proctor/raku/ch-1.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +multi sub MAIN('test') is hidden-from-USAGE { + use Test; + is-deeply missing-values((1,2,3), (2,4,6)), [[1,3],[4,6]], 'Got expected missing values'; + is-deeply missing-values((2,4,6), (1,2,3)), [[4,6],[1,3]], 'Got expected missing values'; + is-deeply missing-values((1,2,3,3),(1,1,2,2)), [[3],[]], 'Second test'; + is-deeply missing-values((1,1,2,2),(1,2,3,3)), [[],[3]], 'Second test'; + done-testing; +} + +subset CSV of Str where { $_ ~~ m/^ (\d+) * %% ',' $/ }; + +#| Given two comma seperated lists of integers print the lists of items missing from each list +multi sub MAIN( + CSV $a, #= First list of ints + CSV $b #= Second list of ints +) { + missing-values($a.split(','),$b.split(',')).map( '(' ~ *.join(',') ~ ')' ).join( " " ).say; +} + +sub missing-values( @a, @b ) { + return [ (@a (-) @b).keys.sort.Array, (@b (-) @a).keys.sort.Array]; +} + -- cgit From 8ec93be8b33fe93def163c382c67991d5b65b3a9 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 6 Nov 2023 12:42:52 +0000 Subject: Challenge 242 Solutions (Raku) --- challenge-242/mark-anderson/raku/ch-2.raku | 3 +++ 1 file changed, 3 insertions(+) diff --git a/challenge-242/mark-anderson/raku/ch-2.raku b/challenge-242/mark-anderson/raku/ch-2.raku index c83ac20266..83a75674b6 100644 --- a/challenge-242/mark-anderson/raku/ch-2.raku +++ b/challenge-242/mark-anderson/raku/ch-2.raku @@ -18,6 +18,9 @@ sub flip-matrix(+@m) sub flip-matrix-slow(+@m) { map { reverse $_ >>+^>> 1 }, @m + + # no difference in time + # map { .[.end...0] >>+^>> 1 }, @m } sub benchmark -- cgit From 78cd41d6d74fd05ee5e959235cd8c81520d623f1 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 6 Nov 2023 20:11:38 +0200 Subject: PWC 242 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Task 1 Python done Task 2 Python done Blog references --- challenge-242/luca-ferrari/blog-1.txt | 1 + challenge-242/luca-ferrari/blog-2.txt | 1 + challenge-242/luca-ferrari/blog-3.txt | 1 + challenge-242/luca-ferrari/blog-4.txt | 1 + challenge-242/luca-ferrari/blog-5.txt | 1 + challenge-242/luca-ferrari/blog-6.txt | 1 + challenge-242/luca-ferrari/blog-7.txt | 1 + challenge-242/luca-ferrari/blog-8.txt | 1 + challenge-242/luca-ferrari/postgresql/ch-1.plperl | 37 ++++++++++++++ challenge-242/luca-ferrari/postgresql/ch-1.sql | 35 ++++++++++++++ challenge-242/luca-ferrari/postgresql/ch-2.plperl | 33 +++++++++++++ challenge-242/luca-ferrari/postgresql/ch-2.sql | 59 +++++++++++++++++++++++ challenge-242/luca-ferrari/python/ch-1.py | 45 +++++++++++++++++ challenge-242/luca-ferrari/python/ch-2.py | 51 ++++++++++++++++++++ challenge-242/luca-ferrari/raku/ch-1.p6 | 21 ++++++++ challenge-242/luca-ferrari/raku/ch-2.p6 | 24 +++++++++ 16 files changed, 313 insertions(+) create mode 100644 challenge-242/luca-ferrari/blog-1.txt create mode 100644 challenge-242/luca-ferrari/blog-2.txt create mode 100644 challenge-242/luca-ferrari/blog-3.txt create mode 100644 challenge-242/luca-ferrari/blog-4.txt create mode 100644 challenge-242/luca-ferrari/blog-5.txt create mode 100644 challenge-242/luca-ferrari/blog-6.txt create mode 100644 challenge-242/luca-ferrari/blog-7.txt create mode 100644 challenge-242/luca-ferrari/blog-8.txt create mode 100644 challenge-242/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-242/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-242/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-242/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-242/luca-ferrari/python/ch-1.py create mode 100644 challenge-242/luca-ferrari/python/ch-2.py create mode 100644 challenge-242/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-242/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-242/luca-ferrari/blog-1.txt b/challenge-242/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..3eb8cd76ac --- /dev/null +++ b/challenge-242/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task1 diff --git a/challenge-242/luca-ferrari/blog-2.txt b/challenge-242/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..22f9e80609 --- /dev/null +++ b/challenge-242/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task2 diff --git a/challenge-242/luca-ferrari/blog-3.txt b/challenge-242/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..a7d59aa0f4 --- /dev/null +++ b/challenge-242/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task1plperl diff --git a/challenge-242/luca-ferrari/blog-4.txt b/challenge-242/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..8ab9e88246 --- /dev/null +++ b/challenge-242/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task2plperl diff --git a/challenge-242/luca-ferrari/blog-5.txt b/challenge-242/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..6484d04260 --- /dev/null +++ b/challenge-242/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task1plpgsql diff --git a/challenge-242/luca-ferrari/blog-6.txt b/challenge-242/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..f2d709bdfa --- /dev/null +++ b/challenge-242/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task2plpgsql diff --git a/challenge-242/luca-ferrari/blog-7.txt b/challenge-242/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..c6ed638921 --- /dev/null +++ b/challenge-242/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task1python diff --git a/challenge-242/luca-ferrari/blog-8.txt b/challenge-242/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..293b2cd4c6 --- /dev/null +++ b/challenge-242/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/06/PerlWeeklyChallenge242.html#task2python diff --git a/challenge-242/luca-ferrari/postgresql/ch-1.plperl b/challenge-242/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..652ed6821e --- /dev/null +++ b/challenge-242/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 242 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc242; + +/* + * testdb=> select * from pwc242.task1_plperl( array[1,2,3], array[2,4,6] ); + left | right +-------+------- + {4,6} | {1,3} +*/ +CREATE OR REPLACE FUNCTION +pwc242.task1_plperl( int[], int[] ) +RETURNS TABLE( left int[], right int[] ) +AS $CODE$ + my ( $left, $right ) = @_; + + my $missing_left = []; + my $missing_right = []; + + for my $current ( $left->@* ) { + push $missing_right->@*, $current if ( ! grep( { $_ == $current } $right->@* ) ); + } + + for my $current ( $right->@* ) { + push $missing_left->@*, $current if ( ! grep( { $_ == $current } $left->@* ) ); + } + + return_next( { left => $missing_left, + right => $missing_right } ); + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-242/luca-ferrari/postgresql/ch-1.sql b/challenge-242/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..5a9dfd8aff --- /dev/null +++ b/challenge-242/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,35 @@ +-- +-- Perl Weekly Challenge 242 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc242; + +/* +testdb=> select pwc242.task1_plpgsql( array[1,2,3], array[2,4,6,7] ); + task1_plpgsql +--------------- + (FIRST,1) + (FIRST,3) + (SECOND,4) + (SECOND,6) + (SECOND,7) + +*/ +CREATE OR REPLACE FUNCTION +pwc242.task1_plpgsql( left_array int[], right_array int[] ) +RETURNS TABLE( which_array text, v int ) +AS $CODE$ + SELECT 'FIRST', la + FROM unnest( left_array ) la + WHERE la NOT IN ( SELECT unnest( right_array ) ) + + UNION ALL + + SELECT 'SECOND', ra + FROM unnest( right_array ) ra + WHERE ra NOT IN ( SELECT unnest( left_array ) ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-242/luca-ferrari/postgresql/ch-2.plperl b/challenge-242/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..be1ba9d09d --- /dev/null +++ b/challenge-242/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 242 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc242; + +/* +testdb=> select * from pwc242.task2_plperl( array[ array[1, 0, 0], array[ 0,1,0], array[0,0,1] ]); + task2_plperl +--------------------------- + {{1,1,0},{1,0,1},{0,1,1}} + +*/ +CREATE OR REPLACE FUNCTION +pwc242.task2_plperl( int[] ) +RETURNS int[] +AS $CODE$ + my ( $matrix ) = @_; + + my $output; + + + for my $row ( $matrix->@* ) { + push $output->@*, + [ map { $_ == 0 ? 1 : 0 } split( //, reverse( join( '', $row->@* ) ) ) ]; + + } + + return $output; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-242/luca-ferrari/postgresql/ch-2.sql b/challenge-242/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..7c71c0cb2f --- /dev/null +++ b/challenge-242/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,59 @@ +-- +-- Perl Weekly Challenge 242 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc242; + +/* +estdb=> select pwc242.task2_plpgsql( array[1,0,0,0,0,0,0,1,1]::int[], 3 ); + task2_plpgsql +--------------- + {1,1,0} + {1,1,1} + {0,0,1} +(3 rows) +*/ +CREATE OR REPLACE FUNCTION +pwc242.task2_plpgsql( matrix int[], l int ) +RETURNS SETOF int[] +AS $CODE$ +DECLARE + current_row text; + current int; + index int; + to_return int; + result int[]; +BEGIN + index := 1; + current_row := ''; + + FOREACH current IN ARRAY matrix LOOP + current_row := current_row || current::text; + IF index = l THEN + -- the row is now complete, flip and split + result := array[]::int[]; + FOREACH to_return IN ARRAY regexp_split_to_array( reverse( current_row ), '' ) LOOP + IF to_return = 1 THEN + result := array_append( result, 0 ); + ELSE + result := array_append( result, 1 ); + END IF; + END LOOP; + + RETURN NEXT result; + + -- start over + current_row := ''; + index := 1; + ELSE + index := index + 1; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-242/luca-ferrari/python/ch-1.py b/challenge-242/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..0b5a70cf7c --- /dev/null +++ b/challenge-242/luca-ferrari/python/ch-1.py @@ -0,0 +1,45 @@ +#!python + +# +# Perl Weekly Challenge 242 +# Task 1 +# +# See +# + +import sys + +# task implementation +def main( argv ): + left = [] + right = [] + is_left = True + for current in argv: + if current != '|': + if ( is_left ): + left.append( int( current ) ) + else: + right.append( int( current ) ) + else: + is_left = False + + missing_left = [] + missing_right = [] + + for current in left: + if not current in right: + missing_left.append( current ) + + for current in right: + if not current in left: + missing_right.append( current ) + + print( ",".join( map( str, missing_left ) ) ) + print( ",".join( map( str, missing_right ) ) ) + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-242/luca-ferrari/python/ch-2.py b/challenge-242/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..12778d1821 --- /dev/null +++ b/challenge-242/luca-ferrari/python/ch-2.py @@ -0,0 +1,51 @@ +#!python + +# +# Perl Weekly Challenge 242 +# Task 2 +# +# See +# +# $ python3 ch-2.py 1 0 0 '|' 0 0 1 '|' 1 1 1 +# 1,1,0 +# 0,1,1 +# 0,0,0 + + +import sys + + + +# task implementation +def main( argv ): + matrix = [] + current_row = 0 + + matrix.append( [] ) + for current in argv: + if current != '|': + matrix[ current_row ].append( int( current ) ) + else: + current_row += 1 + matrix.append( [] ) + + # inner function to use with map + # returns a string to make join happy! + def task2(n): + if n == 1: + return "0" + else: + return "1" + + + for current_row in matrix: + print( ",".join( map( task2, reversed( current_row ) ) ) ) + + + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-242/luca-ferrari/raku/ch-1.p6 b/challenge-242/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..aa0afcfaff --- /dev/null +++ b/challenge-242/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#!raku + +# +# Perl Weekly Challenge 242 +# Task 1 +# +# See +# + +sub MAIN( Str $left, Str $right ) { + + my @left = $left.split( ',' ).map( *.Int ); + my @right = $right.split( ',' ).map( *.Int ); + + my ( @missing-left, @missing-right ); + + @missing-left.push: $_ if ( ! @left.grep( $_ ) ) for @right; + @missing-right.push: $_ if ( ! @right.grep( $_ ) ) for @left; + + ( @missing-left, @missing-right ).say; +} diff --git a/challenge-242/luca-ferrari/raku/ch-2.p6 b/challenge-242/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..f7c6b7c0ae --- /dev/null +++ b/challenge-242/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,24 @@ +#!raku + +# +# Perl Weekly Challenge 242 +# Task 2 +# +# See +# + +sub MAIN() { + + my @matrix = [ 1, 1, 0 ], + [ 0, 1, 1 ], + [ 0, 0, 1 ], + ; + + my @output; + for @matrix -> $row { + my $new-row = $row.join.flip.comb.map( { $_ == 0 ?? 1 !! 0 } ); + @output.push: [ $new-row ]; + } + + @output.join( "\n" ).say; +} -- cgit From 52d9817d77c3d699702e36868234ff8d34ab2437 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 6 Nov 2023 09:29:12 -0500 Subject: Challenge 242 solutions by Packy Anderson * Raku * Perl * Python 1 Blog post --- challenge-242/packy-anderson/README.md | 83 +++++++++-------------------- challenge-242/packy-anderson/blog.txt | 1 + challenge-242/packy-anderson/perl/ch-1.pl | 58 ++++++++++++++++++++ challenge-242/packy-anderson/perl/ch-2.pl | 32 +++++++++++ challenge-242/packy-anderson/python/ch-1.py | 53 ++++++++++++++++++ challenge-242/packy-anderson/python/ch-2.py | 28 ++++++++++ challenge-242/packy-anderson/raku/ch-1.raku | 54 +++++++++++++++++++ challenge-242/packy-anderson/raku/ch-2.raku | 29 ++++++++++ 8 files changed, 280 insertions(+), 58 deletions(-) create mode 100644 challenge-242/packy-anderson/blog.txt create mode 100755 challenge-242/packy-anderson/perl/ch-1.pl create mode 100755 challenge-242/packy-anderson/perl/ch-2.pl create mode 100755 challenge-242/packy-anderson/python/ch-1.py create mode 100755 challenge-242/packy-anderson/python/ch-2.py create mode 100755 challenge-242/packy-anderson/raku/ch-1.raku create mode 100755 challenge-242/packy-anderson/raku/ch-2.raku 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 -- cgit From e2f6494e06a0afc9218edb956db0c896c84d0816 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 6 Nov 2023 09:51:47 -0500 Subject: Week 242 --- challenge-242/zapwai/perl/ch-1.pl | 33 +++++++++++++++++++++++++++++++++ challenge-242/zapwai/perl/ch-2.pl | 20 ++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-242/zapwai/perl/ch-1.pl create mode 100644 challenge-242/zapwai/perl/ch-2.pl diff --git a/challenge-242/zapwai/perl/ch-1.pl b/challenge-242/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..bb4d03f084 --- /dev/null +++ b/challenge-242/zapwai/perl/ch-1.pl @@ -0,0 +1,33 @@ +use v5.30; +no warnings; +my @arr1 = (1,2,3); +my @arr2 = (2,4,6); + +@arr1 = (1,2,3,3); +@arr2 = (1,1,2,2); + +say "\@arr1 = @arr1"; +say "\@arr2 = @arr2"; + +my @diff1; +my @diff2; + +for my $v (@arr1) { + push @diff1, $v unless ($v ~~ @arr2); +} + +for my $v (@arr2) { + push @diff2, $v unless ($v ~~ @arr1); +} + +my %d1; +my %d2; + +$d1{$_} = 1 for (@diff1); +$d2{$_} = 1 for (@diff2); + +my @d1 = keys %d1; +my @d2 = keys %d2; + +say "Output: (" . join(", ", @d1) . ")"; +say "\t(" . join(", ", @d2) . ")"; diff --git a/challenge-242/zapwai/perl/ch-2.pl b/challenge-242/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..a127ac56fc --- /dev/null +++ b/challenge-242/zapwai/perl/ch-2.pl @@ -0,0 +1,20 @@ +use v5.30; +my @matrix = ([1,1,0], [1,0,1], [0,0,0]); +my @new; + +foreach my $r (@matrix) { + my $row = join "", @$r; + my $rev = reverse $row; + my @dig = split("",$rev); + my @current; + foreach my $val (@dig) { + push @current, ($val == 0) ? 1 : 0; + } + push @new, \@current; +} +say "Input:"; +say @$_ for (@matrix); +say "\nOutput:"; +say @$_ foreach (@new) ; + + -- cgit From 159011b9e84b5bf4ca9f837e6b51e6e563882a6d Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 6 Nov 2023 10:12:12 -0600 Subject: Solve PWC242 --- challenge-242/wlmb/blog.txt | 1 + challenge-242/wlmb/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-242/wlmb/perl/ch-2.pl | 16 ++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 challenge-242/wlmb/blog.txt create mode 100755 challenge-242/wlmb/perl/ch-1.pl create mode 100755 challenge-242/wlmb/perl/ch-2.pl diff --git a/challenge-242/wlmb/blog.txt b/challenge-242/wlmb/blog.txt new file mode 100644 index 0000000000..be4855b029 --- /dev/null +++ b/challenge-242/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/11/06/PWC242/ diff --git a/challenge-242/wlmb/perl/ch-1.pl b/challenge-242/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..5d2b14b4c3 --- /dev/null +++ b/challenge-242/wlmb/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 242 +# Task 1: Missing Members +# +# See https://wlmb.github.io/2023/11/06/PWC242/#task-1-missing-members +use v5.36; +die <<~"FIN" unless @ARGV==2; + Usage: $0 S0 S1 + to find the space-separated elements of S0 missing from S1 and viceversa. + FIN +my @bits=(1,2); +my %bitmasks; +for(0,1){ + my $bit=$bits[$_]; + $bitmasks{$_}|=$bit for split " ", $ARGV[$_]; +} +my @results; +for(0,1){ + my $bit=$bits[$_]; + @{$results[$_]}=sort {$a <=> $b} grep{$bitmasks{$_}==$bit}keys %bitmasks; +} +say "[$ARGV[0]],[$ARGV[1]] -> [@{$results[0]}], [@{$results[1]}]"; diff --git a/challenge-242/wlmb/perl/ch-2.pl b/challenge-242/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..0533898d5c --- /dev/null +++ b/challenge-242/wlmb/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +# Perl weekly challenge 242 +# Task 2: Flip Matrix +# +# See https://wlmb.github.io/2023/11/06/PWC242/#task-2-flip-matrix +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV; + Usage: $0 M0 [M1...] + to reverse the rows and invert the elements of the matrices M0 M1... + provided as strings like "[[m00 m01...],[m10 m11...]...]" + FIN +for(@ARGV){ + my $x=pdl($_); + say "Input${x}Output", !$x->slice([-1,0]); +} -- cgit From 16a794c74c24c7ca6a9a04ac2a883a643bd41599 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 6 Nov 2023 19:11:15 +0100 Subject: Add solution 242 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-242/jeanluc2020/blog-1.txt | 1 + challenge-242/jeanluc2020/blog-2.txt | 1 + challenge-242/jeanluc2020/perl/ch-1.pl | 62 ++++++++++++++++++++++++++ challenge-242/jeanluc2020/perl/ch-2.pl | 74 ++++++++++++++++++++++++++++++++ challenge-242/jeanluc2020/python/ch-1.py | 66 ++++++++++++++++++++++++++++ challenge-242/jeanluc2020/python/ch-2.py | 71 ++++++++++++++++++++++++++++++ 6 files changed, 275 insertions(+) create mode 100644 challenge-242/jeanluc2020/blog-1.txt create mode 100644 challenge-242/jeanluc2020/blog-2.txt create mode 100755 challenge-242/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-242/jeanluc2020/perl/ch-2.pl create mode 100755 challenge-242/jeanluc2020/python/ch-1.py create mode 100755 challenge-242/jeanluc2020/python/ch-2.py diff --git a/challenge-242/jeanluc2020/blog-1.txt b/challenge-242/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..3609be466d --- /dev/null +++ b/challenge-242/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-242-1.html diff --git a/challenge-242/jeanluc2020/blog-2.txt b/challenge-242/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..97547946a0 --- /dev/null +++ b/challenge-242/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-242-2.html diff --git a/challenge-242/jeanluc2020/perl/ch-1.pl b/challenge-242/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..d3b7aaf045 --- /dev/null +++ b/challenge-242/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK1 +# +# Task 1: Missing Members +# ======================= +# +# You are given two arrays of integers. +# +# Write a script to find out the missing members in each other arrays. +# +## Example 1 +## +## Input: @arr1 = (1, 2, 3) +## @arr2 = (2, 4, 6) +## Output: ([1, 3], [4, 6]) +## +## (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +## (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +# +## Example 2 +## +## Input: @arr1 = (1, 2, 3, 3) +## @arr2 = (1, 1, 2, 2) +## Output: ([3]) +## +## (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +## (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). +# +############################################################ +## +## discussion +## +############################################################ +# +# For each element of each array, if it is not in the other array, +# save it for the output. +# Example 2 has a caveat though, by not returning an empty array in +# case of no missing elements it is unclear in which of the arrays +# the element was originally. It's better to return an empty array +# here, so let's just do that instead. + +missing_members( [ 1, 2, 3 ], [ 2, 4, 6 ] ); +missing_members( [ 1, 2, 3, 3 ], [ 1, 1, 2, 2 ] ); + +sub missing_members { + my ($arr1, $arr2) = @_; + print "Input: (" . join(", ", @$arr1) . "), (" . join(", ", @$arr2) . ")\n"; + my (@res1, @res2, %keys1, %keys2); + map { $keys1{$_} = 1; } @$arr1; + map { $keys2{$_} = 1; } @$arr2; + my %seen = (); + foreach my $elem (@$arr1) { + push @res1, $elem unless $keys2{$elem} or $seen{$elem}; + $seen{$elem} = 1; + } + %seen = (); + foreach my $elem (@$arr2) { + push @res2, $elem unless $keys1{$elem} or $seen{$elem}; + $seen{$elem} = 1; + } + print "Output: ([" . join(", ", @res1) . "], [" . join(", ", @res2) . "])\n"; +} diff --git a/challenge-242/jeanluc2020/perl/ch-2.pl b/challenge-242/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ed69f9eab7 --- /dev/null +++ b/challenge-242/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK2 +# +# Task 2: Flip Matrix +# =================== +# +# You are given n x n binary matrix. +# +# Write a script to flip the given matrix as below. +# +# 1 1 0 +# 0 1 1 +# 0 0 1 +# +# a) Reverse each row +# +# 0 1 1 +# 1 1 0 +# 1 0 0 +# +# b) Invert each member +# +# 1 0 0 +# 0 0 1 +# 0 1 1 +# +# +## Example 1 +## +## Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]) +## Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) +# +## Example 2 +## +## 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]) +# +############################################################ +## +## discussion +## +############################################################ +# +# Just reverse and invert. Nothing complicated. + +flip_matrix([1, 1, 0], [1, 0, 1], [0, 0, 0]); +flip_matrix([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]); + +sub flip_matrix { + my @rows = @_; + my @result; + print "Input: ("; + foreach my $row (@rows) { + print "[" . join(", ", @$row) . "],"; + } + print ")\n"; + foreach my $row (@rows) { + push @result, [ inverted( reverse @$row ) ]; + } + print "Ouput: ("; + foreach my $row (@result) { + print "[" . join(", ", @$row) . "],"; + } + print ")\n"; +} + +sub inverted { + my @array = @_; + my @result = (); + foreach my $elem (@array) { + push @result, $elem == 0 ? 1 : 0; + } + return @result; +} diff --git a/challenge-242/jeanluc2020/python/ch-1.py b/challenge-242/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..f17e000f6f --- /dev/null +++ b/challenge-242/jeanluc2020/python/ch-1.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK1 +# +# Task 1: Missing Members +# ======================= +# +# You are given two arrays of integers. +# +# Write a script to find out the missing members in each other arrays. +# +## Example 1 +## +## Input: @arr1 = (1, 2, 3) +## @arr2 = (2, 4, 6) +## Output: ([1, 3], [4, 6]) +## +## (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +## (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +# +## Example 2 +## +## Input: @arr1 = (1, 2, 3, 3) +## @arr2 = (1, 1, 2, 2) +## Output: ([3]) +## +## (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +## (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). +# +############################################################ +## +## discussion +## +############################################################ +# +# For each element of each array, if it is not in the other array, +# save it for the output. +# Example 2 has a caveat though, by not returning an empty array in +# case of no missing elements it is unclear in which of the arrays +# the element was originally. It's better to return an empty array +# here, so let's just do that instead. + +def missing_members(arr1: list, arr2: list): + seen: dict = {} + res1: list = [] + res2: list = [] + print("Input: ([", ", ".join(str(x) for x in arr1), "], [", ", ".join(str(x) for x in arr2), "])") + for elem in arr1: + if elem in seen: + next + else: + if elem not in arr2: + seen[elem] = 1 + res1.append(elem) + seen = {} + for elem in arr2: + if elem in seen: + next + else: + if elem not in arr1: + seen[elem] = 1 + res2.append(elem) + print("Output: ([", ", ".join(str(x) for x in res1), "], [", ", ".join(str(x) for x in res2), "])") + +missing_members( [ 1, 2, 3 ], [ 2, 4, 6 ] ); +missing_members( [ 1, 2, 3, 3 ], [ 1, 1, 2, 2 ] ); + diff --git a/challenge-242/jeanluc2020/python/ch-2.py b/challenge-242/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..df3a533199 --- /dev/null +++ b/challenge-242/jeanluc2020/python/ch-2.py @@ -0,0 +1,71 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK2 +# +# Task 2: Flip Matrix +# =================== +# +# You are given n x n binary matrix. +# +# Write a script to flip the given matrix as below. +# +# 1 1 0 +# 0 1 1 +# 0 0 1 +# +# a) Reverse each row +# +# 0 1 1 +# 1 1 0 +# 1 0 0 +# +# b) Invert each member +# +# 1 0 0 +# 0 0 1 +# 0 1 1 +# +# +## Example 1 +## +## Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]) +## Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) +# +## Example 2 +## +## 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]) +# +############################################################ +## +## discussion +## +############################################################ +# +# Just reverse and invert. Nothing complicated. + +def invert_reverse(array: list) -> list: + result: list = [] + for x in reversed(array): + if x == 0: + result.append(1) + else: + result.append(0) + return result + +def flip_matrix(rows: list): + print("Input: (", end='') + for row in rows: + print("[", ", ".join(str(x) for x in row), "],", end='', sep='') + print(")") + result: list = [] + for row in rows: + result.append(invert_reverse(row)) + print("Output: (", end='') + for row in result: + print("[", ", ".join(str(x) for x in row), "],", end='', sep='') + print(")") + +flip_matrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]) +flip_matrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]) + + -- cgit From e1f41774deab9b65fe994538698bdd4d973846f7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 6 Nov 2023 23:00:37 +0000 Subject: Add Perl solution --- challenge-242/paulo-custodio/Makefile | 2 + challenge-242/paulo-custodio/perl/ch-1.pl | 63 +++++++++++++++++++++++++++++ challenge-242/paulo-custodio/perl/ch-2.pl | 65 ++++++++++++++++++++++++++++++ challenge-242/paulo-custodio/t/test-1.yaml | 10 +++++ challenge-242/paulo-custodio/t/test-2.yaml | 10 +++++ 5 files changed, 150 insertions(+) create mode 100644 challenge-242/paulo-custodio/Makefile create mode 100644 challenge-242/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-242/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-242/paulo-custodio/t/test-1.yaml create mode 100644 challenge-242/paulo-custodio/t/test-2.yaml diff --git a/challenge-242/paulo-custodio/Makefile b/challenge-242/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-242/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-242/paulo-custodio/perl/ch-1.pl b/challenge-242/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..148f427abd --- /dev/null +++ b/challenge-242/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +# Challenge 242 +# +# Task 1: Missing Members +# Submitted by: Mohammad S Anwar +# You are given two arrays of integers. +# +# Write a script to find out the missing members in each other arrays. +# +# Example 1 +# Input: @arr1 = (1, 2, 3) +# @arr2 = (2, 4, 6) +# Output: ([1, 3], [4, 6]) +# +# (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +# (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +# Example 2 +# Input: @arr1 = (1, 2, 3, 3) +# @arr2 = (1, 1, 2, 2) +# Output: ([3]) +# +# (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +# (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). + +use Modern::Perl; +use List::Util 'uniq'; + +@ARGV==2 or die "usage: $0 '(a,b,c)' '(d,e,f)'\n"; +my @arr1 = parse(shift); +my @arr2 = parse(shift); + +my @not_in1 = not_in(\@arr1, \@arr2); +my @not_in2 = not_in(\@arr2, \@arr1); + +say output(\@not_in1, \@not_in2); + + +sub parse { + my($text) = @_; + my @arr = split ' ', $text =~ s/\D/ /gr; + return @arr; +} + +sub output { + my($arr1, $arr2) = @_; + my @elems; + for ($arr1, $arr2) { + if (@$_) { + push @elems, "[".join(", ", @$_)."]" + } + } + return "(".join(", ", @elems).")"; +} + +sub not_in { + my($arr1, $arr2) = @_; + my %arr2; + $arr2{$_}=1 for @$arr2; + my @not_in; + push @not_in, $_ for uniq grep {!$arr2{$_}} @$arr1; + return @not_in; +} diff --git a/challenge-242/paulo-custodio/perl/ch-2.pl b/challenge-242/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..530091d6b5 --- /dev/null +++ b/challenge-242/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +# Challenge 242 +# +# Task 1: Missing Members +# Submitted by: Mohammad S Anwar +# You are given two arrays of integers. +# +# Write a script to find out the missing members in each other arrays. +# +# Example 1 +# Input: @arr1 = (1, 2, 3) +# @arr2 = (2, 4, 6) +# Output: ([1, 3], [4, 6]) +# +# (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +# (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +# Example 2 +# Input: @arr1 = (1, 2, 3, 3) +# @arr2 = (1, 1, 2, 2) +# Output: ([3]) +# +# (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +# (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). + +use Modern::Perl; + +@ARGV==1 or die "usage: $0 '([a, b, c], [d, e, f], [g, h, i])'\n"; +my @m = parse(shift); + +# a) Reverse each row +$_ = [reverse @$_] for @m; + +# b) Invert each member +for (@m) { + for (@$_) { + $_ = 1-$_; + } +} + + +say output(@m); + + +sub parse { + my($text) = @_; + my @m; + my @rows = split /\]\s*,\s*\[/, $text; + for (@rows) { + s/\D/ /g; + my @cols = split ' ', $_; + push @m, \@cols; + } + return @m; +} + +sub output { + my(@m) = @_; + my @elems; + for (@m) { + push @elems, "[".join(", ", @$_)."]" + } + return "(".join(", ", @elems).")"; +} + diff --git a/challenge-242/paulo-custodio/t/test-1.yaml b/challenge-242/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..930832046a --- /dev/null +++ b/challenge-242/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: '"(1, 2, 3)" "(2, 4, 6)"' + input: + output: ([1, 3], [4, 6]) +- setup: + cleanup: + args: '"(1, 2, 3, 3)" "(1, 1, 2, 2)"' + input: + output: ([3]) diff --git a/challenge-242/paulo-custodio/t/test-2.yaml b/challenge-242/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..08a0b6cd29 --- /dev/null +++ b/challenge-242/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: '"([1, 1, 0], [1, 0, 1], [0, 0, 0])"' + input: + output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) +- setup: + cleanup: + args: '"([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])"' + input: + output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) -- cgit From f63c30d4675142225e18b1c38900247091eab189 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Mon, 6 Nov 2023 18:54:58 -0500 Subject: new file: challenge-242/mattneleigh/perl/ch-1.pl new file: challenge-242/mattneleigh/perl/ch-2.pl --- challenge-242/mattneleigh/perl/ch-1.pl | 100 ++++++++++++++++++++++++++++++++ challenge-242/mattneleigh/perl/ch-2.pl | 103 +++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100755 challenge-242/mattneleigh/perl/ch-1.pl create mode 100755 challenge-242/mattneleigh/perl/ch-2.pl diff --git a/challenge-242/mattneleigh/perl/ch-1.pl b/challenge-242/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..acb131c44f --- /dev/null +++ b/challenge-242/mattneleigh/perl/ch-1.pl @@ -0,0 +1,100 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @array_pairs = ( + [ + [ 1, 2, 3 ], + [ 2, 4, 6 ] + ], + [ + [ 1, 2, 3, 3 ], + [ 1, 1, 2, 2 ] + ] +); + +print("\n"); +foreach my $array_pair (@array_pairs){ + printf( + "Input: \@arr1 = (%s)\n" . + " \@arr2 = (%s)\n" . + "Output: (%s)\n\n", + join(", ", @{$array_pair->[0]}), + join(", ", @{$array_pair->[1]}), + join( + ", ", + map( + "[" . join(", ", @{$_}) . "]", + find_missing_numbers_in_array_pair($array_pair) + ) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given two arrays of integers, find those that are present in each array but +# missing in the other +# Takes one argument: +# * A ref to a pair of arrays of integers (e.g. +# [ +# [ 1, 2, 3, 3 ], +# [ 1, 1, 2, 2 ] +# ] +# ) +# Returns: +# * A list of two arrays, each containing the integers from the corresponding +# input array that were missing from the other array. If a missing number +# appeared more than once in its input array, it will only appear once in the +# output array; if no numbers were missing, the output array will be empty +# (e.g. +# ( +# [ 3 ], +# [ ] +# ) +# ) +################################################################################ +sub find_missing_numbers_in_array_pair{ + + my @missing = ([], []); + + # Loop over two pairs of indices + for my $indices ([0, 1], [1, 0]){ + # Make a lookup table so we can easily see + # what's in one of the arrays + my %lookup = map( + { $_ => 1; } + @{$ARG[0][$indices->[0]]} + ); + my %seen = (); + + # Loop over the contents of the 'other' array + foreach my $num (@{$ARG[0][$indices->[1]]}){ + # If this number isn't in the first array and + # it hasn't been seen, store it + push(@{$missing[$indices->[1]]}, $num) + unless($lookup{$num} || $seen{$num}); + + # Mark this number as seen + $seen{$num} = 1; + } + } + + return(@missing); + +} + + + diff --git a/challenge-242/mattneleigh/perl/ch-2.pl b/challenge-242/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..55e9f17fbd --- /dev/null +++ b/challenge-242/mattneleigh/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @matrices = ( + [ + [ 1, 1, 0 ], + [ 1, 0, 1 ], + [ 0, 0, 0 ] + ], + [ + [ 1, 1, 0, 0 ], + [ 1, 0, 0, 1 ], + [ 0, 1, 1, 1 ], + [ 1, 0, 1, 0 ] + ] +); + +print("\n"); +foreach my $matrix (@matrices){ + printf( + "Input: \@matrix = (%s)\nOutput: (%s)\n\n", + join( + ", ", + map( + "[" . join(", ", @{$_}) . "]", + @{$matrix} + ) + ), + join( + ", ", + map( + "[" . join(", ", @{$_}) . "]", + @{flip_binary_matrix($matrix)} + ) + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Flip a binary matrix (made of ones and zeros only) according to the following +# procedure: +# 1) Invert each member of the matrix, changing 0 to 1 and vice-versa +# 2) Reverse the order of members within each row of the matrix +# Takes one argument: +# * A ref to an array of arrays the represents the matrix (e.g. +# [ +# [ 1, 1, 0 ], +# [ 1, 0, 1 ], +# [ 0, 0, 0 ] +# ] +# ) +# Returns: +# * A ref to a flipped copy of the matrix (e.g. +# [ +# [ 1, 0, 0 ], +# [ 0, 1, 0 ], +# [ 1, 1, 1 ] +# ] +# ) +################################################################################ +sub flip_binary_matrix{ + + return( + # 4: Get a ref to the matrix we've made + [ + # 3: Combine all new rows into a new matrix + # array + map( + [ + # 2: Reverse the order of members within the + # row + reverse( + # 1: Make a copy of each row with the members + # inverted + map( + $_ ? 0 : 1, + @{$_} + ) + ) + ], + @{$ARG[0]} + ) + ] + ); + +} + + + -- cgit From 6f877c5c2cf919fcd759f0e45adccdea67f45ac8 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Mon, 6 Nov 2023 17:20:38 -0800 Subject: Robbie Hatley's solutions in Perl for The Weekly Challenge #242. --- challenge-242/robbie-hatley/blog.txt | 1 + challenge-242/robbie-hatley/perl/ch-1.pl | 142 +++++++++++++++++++++++++++++++ challenge-242/robbie-hatley/perl/ch-2.pl | 133 +++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 challenge-242/robbie-hatley/blog.txt create mode 100755 challenge-242/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-242/robbie-hatley/perl/ch-2.pl diff --git a/challenge-242/robbie-hatley/blog.txt b/challenge-242/robbie-hatley/blog.txt new file mode 100644 index 0000000000..5d4a917dba --- /dev/null +++ b/challenge-242/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/11/robbie-hatleys-solutions-to-weekly.html \ No newline at end of file diff --git a/challenge-242/robbie-hatley/perl/ch-1.pl b/challenge-242/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..6adfe22605 --- /dev/null +++ b/challenge-242/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,142 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +COLOPHON: +This is a