From 8a3c526f23a8e173fa9a6d8344643ea4fa7d5738 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 10:41:41 +0200 Subject: Task 1 done --- challenge-211/luca-ferrari/raku/ch-1.p6 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 challenge-211/luca-ferrari/raku/ch-1.p6 (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/raku/ch-1.p6 b/challenge-211/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..6c506c4842 --- /dev/null +++ b/challenge-211/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!raku + +# +# Perl Weekly Challenge 211 +# Task 1 +# +# See +# + +sub MAIN() { + + my @matrix = [ [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3], + ]; + + my %diag; + %diag{ @matrix[ $_ ][ $_ ] }++ for 0 ..^ @matrix.elems; + 'False'.say if ( %diag.keys.elems != 1 || %diag{ @matrix[ 0 ][ 0 ] } != @matrix.elems ); + 'True'.say; + +} -- cgit From 01b9182fbd76e758dc13ea9706ea714612cdf98a Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 11:40:59 +0200 Subject: Task 2 done --- challenge-211/luca-ferrari/raku/ch-2.p6 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-211/luca-ferrari/raku/ch-2.p6 (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/raku/ch-2.p6 b/challenge-211/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..e7a81ce359 --- /dev/null +++ b/challenge-211/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,20 @@ +#!raku + +# +# Perl Weekly Challenge 211 +# Task 2 +# +# See +# + +sub MAIN( *@list where{ @list.elems == @list.grep( * ~~ Int ).elems } ) { + + for @list.permutations -> @current { + for 0 ..^ @current.elems { + my ($left, $right) = @current[ 0 .. $_ ], @current[ $_ + 1 .. * - 1 ]; + if ( ( $left.sum / $left.elems ) == ( $right.sum / $right.elems ) ) { + exit; + } + } + } +} -- cgit From 2773e3648994354fc169f59fa8dd148aacf96100 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 11:51:31 +0200 Subject: Task 1 plperl done --- challenge-211/luca-ferrari/postgresql/ch-1.plperl | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-211/luca-ferrari/postgresql/ch-1.plperl (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/postgresql/ch-1.plperl b/challenge-211/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..d2085bdc4c --- /dev/null +++ b/challenge-211/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 211 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc211; + +/* +testdb=> select pwc211.task1_plperl( array[ array[4,3,2,1], array[ 5,4,3,2], array[6,5,4,3] ] ); + task1_plperl +-------------- + t +(1 row) + +*/ +CREATE OR REPLACE FUNCTION +pwc211.task1_plperl( int[][] ) +RETURNS bool +AS $CODE$ + my ( $array ) = @_; + + my $diag; + $diag->{ $array->[ $_ ]->[ $_ ] }++ for ( 0 .. scalar( $array->@* ) - 1 ); + return 0 if ( keys( $diag->%* ) != 1 ); + return 0 if ( $diag->{ $array->[ 0 ]->[ 0 ] } != scalar( $array->@* ) ); + return 1; +$CODE$ +LANGUAGE plperl; -- cgit From 1bab1d6247beb4ed475992cc978e74cff4b654f7 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 12:10:59 +0200 Subject: Task 2 done with equally sized arrays --- challenge-211/luca-ferrari/raku/ch-2.p6 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/raku/ch-2.p6 b/challenge-211/luca-ferrari/raku/ch-2.p6 index e7a81ce359..aa425afa73 100644 --- a/challenge-211/luca-ferrari/raku/ch-2.p6 +++ b/challenge-211/luca-ferrari/raku/ch-2.p6 @@ -10,11 +10,21 @@ sub MAIN( *@list where{ @list.elems == @list.grep( * ~~ Int ).elems } ) { for @list.permutations -> @current { - for 0 ..^ @current.elems { - my ($left, $right) = @current[ 0 .. $_ ], @current[ $_ + 1 .. * - 1 ]; - if ( ( $left.sum / $left.elems ) == ( $right.sum / $right.elems ) ) { - exit; - } + # for 0 ..^ @current.elems { + # # find the first couple that gives the same average + # my ($left, $right) = @current[ 0 .. $_ ], @current[ $_ + 1 .. * ]; + # if ( ( $left.sum / $left.elems ) == ( $right.sum / $right.elems ) ) { + # say "{ $left.join( ',' ) } = { $left.sum / $left.elems } and { $right.join( ',' ) } = { $right.sum / $right.elems } "; + # exit; + # } + # } + + my $split-at = ( @current.elems - 1 ) / 2; + my ($left, $right) = @current[ 0 .. $split-at ], @current[ $split-at + 1 .. * ]; + if ( ( $left.sum / $left.elems ) == ( $right.sum / $right.elems ) ) { + say "{ $left.join( ',' ) } = { $left.sum / $left.elems } and { $right.join( ',' ) } = { $right.sum / $right.elems } "; + exit; } + } } -- cgit From c523fbd673bd6651db49e4c1a97908687a9fa035 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 12:18:58 +0200 Subject: Task 2 plperl done --- challenge-211/luca-ferrari/postgresql/ch-2.plperl | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-211/luca-ferrari/postgresql/ch-2.plperl (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/postgresql/ch-2.plperl b/challenge-211/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..6513b34abf --- /dev/null +++ b/challenge-211/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,40 @@ +-- +-- Perl Weekly Challenge 211 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc211; + +/* +estdb=# select pwc211.task2_plperl( array[ 1,2,3,4,5,6,7,8] ); + task2_plperl +-------------- + {1,2,7,8} + {3,4,5,6} +(2 rows) + +*/ +CREATE OR REPLACE FUNCTION +pwc211.task2_plperl( int[] ) +RETURNS SETOF int[] +AS $CODE$ + use List::Permutor; + use List::Util qw/sum/; + my ( $array ) = @_; + + my $permutator = List::Permutor->new( $array->@* ); + while ( my @current = $permutator->next ) { + my ( $split_at ) = ( $#current + 1) / 2; + my ( $left, $right ) = ( [ @current[ 0 .. ( $split_at - 1 ) ] ], [ @current[ $split_at .. $#current ] ] ); + + if ( ( sum( $left->@* ) / scalar( $left->@* ) ) == ( sum( $right->@* ) / scalar( $right->@* ) ) ) { + return_next( $left ); + return_next( $right ); + return undef; + } + } + +return undef; +$CODE$ +LANGUAGE plperlu; -- cgit From ff22c74e122711797e658d51ff3e5bd17ad66a8d Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 12:32:25 +0200 Subject: Task 1 plpgsql --- challenge-211/luca-ferrari/postgresql/ch-1.sql | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 challenge-211/luca-ferrari/postgresql/ch-1.sql (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/postgresql/ch-1.sql b/challenge-211/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..23a33447de --- /dev/null +++ b/challenge-211/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,36 @@ +-- +-- Perl Weekly Challenge 211 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc211; + +CREATE OR REPLACE FUNCTION +pwc211.task1_plpgsql( a int[][]) +RETURNS bool +AS $CODE$ +DECLARE + current_row int := 1; + current_col int := 1; + previous_val int := NULL; +BEGIN + WHILE current_row <= array_length( a, 1 ) LOOP + IF current_row > array_length( a, 2 ) THEN + RETURN false; + END IF; + + IF previous_val IS NULL THEN + previous_val := a[ current_row ][ current_row ]; + ELSIF previous_val <> a[ current_row ][ current_row ] THEN + RETURN false; + END IF; + + current_row := current_row + 1; + END LOOP; + + RETURN true; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 765e35de7cb90dab45fea9c6a5c15c66e99a934f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 13:24:53 +0200 Subject: Task 2 plpgsql done --- challenge-211/luca-ferrari/postgresql/ch-2.sql | 65 ++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 challenge-211/luca-ferrari/postgresql/ch-2.sql (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/postgresql/ch-2.sql b/challenge-211/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..802a8f6932 --- /dev/null +++ b/challenge-211/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,65 @@ +-- +-- Perl Weekly Challenge 211 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc211; + +-- see +CREATE FUNCTION pwc211.permute(anyarray) + RETURNS SETOF anyarray + LANGUAGE SQL IMMUTABLE +AS $f$ + SELECT (WITH RECURSIVE r(n,p,a,b) + AS (SELECT i, $1[1:0], $1, array_upper($1,1) + UNION ALL + SELECT n / b, p || a[n % b + 1], a[1:n % b] || a[n % b + 2:b], b-1 + FROM r + WHERE b > 0) + SELECT p FROM r WHERE b=0) + FROM generate_series(0,factorial( (array_upper($1,1)) )::integer-1) i; +$f$; + + + + +CREATE OR REPLACE FUNCTION +pwc211.task2_plpgsql( a int[] ) +RETURNS SETOF int[] +AS $CODE$ +DECLARE + split_at int := 0; + current_array int[]; + l int[]; + r int []; + avg_l numeric; + avg_r numeric; +BEGIN + split_at := array_length( a, 1 ) / 2; + + FOR current_array IN SELECT * FROM pwc211.permute( a ) LOOP + l := current_array[ 1:split_at ]; + r := current_array[ (split_at + 1): array_length( a, 1 ) ]; + + + SELECT avg( v ) + INTO avg_l + FROM unnest( l ) v; + + SELECT avg( v ) + INTO avg_r + FROM unnest( r ) v; + + IF avg_r = avg_l THEN + RETURN NEXT l; + RETURN NEXT r; + RETURN; + END IF; + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From c3fd6bcfdd91b1b27e25b5949c82d0bfd2303d00 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 3 Apr 2023 13:38:26 +0200 Subject: Blog references --- challenge-211/luca-ferrari/blog-1.txt | 1 + challenge-211/luca-ferrari/blog-2.txt | 1 + challenge-211/luca-ferrari/blog-3.txt | 1 + challenge-211/luca-ferrari/blog-4.txt | 1 + challenge-211/luca-ferrari/blog-5.txt | 1 + challenge-211/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-211/luca-ferrari/blog-1.txt create mode 100644 challenge-211/luca-ferrari/blog-2.txt create mode 100644 challenge-211/luca-ferrari/blog-3.txt create mode 100644 challenge-211/luca-ferrari/blog-4.txt create mode 100644 challenge-211/luca-ferrari/blog-5.txt create mode 100644 challenge-211/luca-ferrari/blog-6.txt (limited to 'challenge-211') diff --git a/challenge-211/luca-ferrari/blog-1.txt b/challenge-211/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..20aa3248b4 --- /dev/null +++ b/challenge-211/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/03/PerlWeeklyChallenge211.html#task1 diff --git a/challenge-211/luca-ferrari/blog-2.txt b/challenge-211/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..af2ebde9dd --- /dev/null +++ b/challenge-211/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/03/PerlWeeklyChallenge211.html#task2 diff --git a/challenge-211/luca-ferrari/blog-3.txt b/challenge-211/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..26e77011ea --- /dev/null +++ b/challenge-211/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/03/PerlWeeklyChallenge211.html#task1plperl diff --git a/challenge-211/luca-ferrari/blog-4.txt b/challenge-211/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..87794d6a99 --- /dev/null +++ b/challenge-211/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/03/PerlWeeklyChallenge211.html#task2plperl diff --git a/challenge-211/luca-ferrari/blog-5.txt b/challenge-211/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..45531804ca --- /dev/null +++ b/challenge-211/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/03/PerlWeeklyChallenge211.html#task1plpgsql diff --git a/challenge-211/luca-ferrari/blog-6.txt b/challenge-211/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..534fca5816 --- /dev/null +++ b/challenge-211/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/03/PerlWeeklyChallenge211.html#task2plpgsql -- cgit From b413d247da323881e60bf1c45506ba9273194874 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 3 Apr 2023 13:04:10 +0000 Subject: Challenge 211 Solutions (Raku) --- challenge-211/mark-anderson/raku/ch-1.raku | 31 ++++++++++++++++++++++++++++++ challenge-211/mark-anderson/raku/ch-2.raku | 22 +++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-211/mark-anderson/raku/ch-1.raku create mode 100644 challenge-211/mark-anderson/raku/ch-2.raku (limited to 'challenge-211') diff --git a/challenge-211/mark-anderson/raku/ch-1.raku b/challenge-211/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..43c6f09b02 --- /dev/null +++ b/challenge-211/mark-anderson/raku/ch-1.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku +use Test; + +ok toeplitz([4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3]); + +nok toeplitz([1, 2, 3], + [3, 2, 1]); + +ok toeplitz([4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3], + [7, 6, 5, 4], + [8, 7, 6, 5]); + +ok toeplitz([1,2], + [2,1], + [3,2], + [4,3]); + +sub toeplitz(+@m) +{ + until @m.elems == 1 + { + return False unless @m[0;^@m[0].end] eqv @m[1;1..@m[1].end]; + shift @m + } + + return True +} diff --git a/challenge-211/mark-anderson/raku/ch-2.raku b/challenge-211/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..768d0a4f09 --- /dev/null +++ b/challenge-211/mark-anderson/raku/ch-2.raku @@ -0,0 +1,22 @@ +#!/usr/bin/env raku +use Test; + +ok split-same-avg(1,2,3,4,5,6,7,8); # [1 8] [5 4 6 3 7 2] +nok split-same-avg(1,3); +ok split-same-avg(3,3,5,5,5,2,2,1); # [3 3 5 2] [2 5 5 1] +nok split-same-avg(5,5,5,2,2,1); + +sub split-same-avg(*@nums) +{ + for (0..@nums.end).combinations(1..(@nums.elems div 2)) -> @a + { + my @b = ((0..@nums.end) (-) @a).keys; + + my @c = @nums[@a]; + my @d = @nums[@b]; + + return True if (@c.sum / @c.elems) == (@d.sum / @d.elems) + } + + return False +} -- cgit From ed49e15822bccd580f4a09da9ead6d52bc5d5d2a Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 3 Apr 2023 14:35:09 +0000 Subject: Challenge 211 Solutions (Raku) --- challenge-211/mark-anderson/raku/ch-1.raku | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'challenge-211') diff --git a/challenge-211/mark-anderson/raku/ch-1.raku b/challenge-211/mark-anderson/raku/ch-1.raku index 43c6f09b02..2b86bde846 100644 --- a/challenge-211/mark-anderson/raku/ch-1.raku +++ b/challenge-211/mark-anderson/raku/ch-1.raku @@ -21,11 +21,5 @@ ok toeplitz([1,2], sub toeplitz(+@m) { - until @m.elems == 1 - { - return False unless @m[0;^@m[0].end] eqv @m[1;1..@m[1].end]; - shift @m - } - - return True + @m[^@m.end;^@m[0].end] eqv @m[1..@m.end;1..@m[1].end] } -- cgit From 7fa49d501285c4b19d15736339bb405993d5a6ee Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:05:25 +0000 Subject: Challenge 211 Solutions (Raku) --- challenge-211/mark-anderson/raku/ch-2.raku | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'challenge-211') diff --git a/challenge-211/mark-anderson/raku/ch-2.raku b/challenge-211/mark-anderson/raku/ch-2.raku index 768d0a4f09..b6563db667 100644 --- a/challenge-211/mark-anderson/raku/ch-2.raku +++ b/challenge-211/mark-anderson/raku/ch-2.raku @@ -8,14 +8,14 @@ nok split-same-avg(5,5,5,2,2,1); sub split-same-avg(*@nums) { - for (0..@nums.end).combinations(1..(@nums.elems div 2)) -> @a + for (^@nums).combinations(1..(@nums.elems div 2)) -> @a is copy { - my @b = ((0..@nums.end) (-) @a).keys; + my @b = ((^@nums) (-) @a).keys; - my @c = @nums[@a]; - my @d = @nums[@b]; + @a = @nums[@a]; + @b = @nums[@b]; - return True if (@c.sum / @c.elems) == (@d.sum / @d.elems) + return True if (@a.sum / @a.elems) == (@b.sum / @b.elems) } return False -- cgit From ae5883dd2325dc38e265798748779fdd19fe1195 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 3 Apr 2023 21:17:23 -0400 Subject: Week 211 --- challenge-211/zapwai/perl/ch-1.pl | 43 +++++++++++++++++++++++++++++++++++++++ challenge-211/zapwai/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 challenge-211/zapwai/perl/ch-1.pl create mode 100644 challenge-211/zapwai/perl/ch-2.pl (limited to 'challenge-211') diff --git a/challenge-211/zapwai/perl/ch-1.pl b/challenge-211/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..13352b3e29 --- /dev/null +++ b/challenge-211/zapwai/perl/ch-1.pl @@ -0,0 +1,43 @@ +use v5.30.0; +my @matrix = ( [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3], ); +# my @matrix = ( [1, 2, 3], +# [3, 2, 1], +# ); +my $m = $#matrix + 1; +my $n = $#{$matrix[0]} + 1; +say "Input: \@matrix is"; +for my $i (0 .. $m - 1) { + print " "; + for my $j (0 .. $n - 1) { + print $matrix[$i][$j]; + } + print "\n"; +} +my $flag = 1; +for my $i (0 .. $m - 2) { + if ($i == 0) { + for my $j (0 .. $n - 2) { + $flag = 0 unless (const_diag($i,$j)); + } + } else { + $flag = 0 unless (const_diag($i,0)); + } +} +print "\nOutput: "; +if ($flag) { + say "True, the matrix is Toeplitz."; +} else { + say "False, the matrix is not Toeplitz."; +} +sub const_diag { + my ($i,$j) = @_; + my $val = $matrix[$i][$j]; + for my $s ($i .. $m - 1) { + last if (!defined $matrix[$s][$j]); + return 0 unless ($matrix[$s][$j] == $val); + $j++; + } + 1 +} diff --git a/challenge-211/zapwai/perl/ch-2.pl b/challenge-211/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..c08d3f84b0 --- /dev/null +++ b/challenge-211/zapwai/perl/ch-2.pl @@ -0,0 +1,41 @@ +use v5.30.0; +my @nums = (1 .. 8); +#my @nums = (1, 3); +say "Input: \@nums = (@nums)"; +my $ans; +my @subarrays; +my $exp = $#nums + 1; +for (1 .. (2**$exp) - 2) { + my $binary_number = sprintf '%'.$exp.'b', $_; + my @pips = split "", $binary_number; + my (@part1, @part2); + for my $i (0 .. $#nums) { + if ($pips[$i] == 1) { + push @part1, $nums[$i]; + } else { + push @part2, $nums[$i]; + } + } + my ($avg1, $avg2); + for my $elem (@part1) { + $avg1 += $elem; + } + for my $elem (@part2) { + $avg2 += $elem; + } + $avg1 = $avg1 / ($#part1 + 1); + $avg2 = $avg2 / ($#part2 + 1); + # say "@part1 - @part2 ~ $avg1 - $avg2"; + if ($avg1 - $avg2 == 0) { + $ans = 1; + my $str = "(@part1)" . ' & ' . "(@part2) with avg $avg1"; + push @subarrays, $str; + } +} +print "Output: "; +if ($ans) { + say "true"; + say "e.g. $subarrays[0]"; +} else { + say "false"; +} -- cgit From 5235f58bcc290126d1d6974e85e6dfd13f4c2a37 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 3 Apr 2023 19:19:08 -0600 Subject: Solve PWC211 --- challenge-211/wlmb/blog.txt | 2 ++ challenge-211/wlmb/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-211/wlmb/perl/ch-2.pl | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 challenge-211/wlmb/blog.txt create mode 100755 challenge-211/wlmb/perl/ch-1.pl create mode 100755 challenge-211/wlmb/perl/ch-2.pl (limited to 'challenge-211') diff --git a/challenge-211/wlmb/blog.txt b/challenge-211/wlmb/blog.txt new file mode 100644 index 0000000000..5d17477df3 --- /dev/null +++ b/challenge-211/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/04/03/PWC211/ + diff --git a/challenge-211/wlmb/perl/ch-1.pl b/challenge-211/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..0e2873fda2 --- /dev/null +++ b/challenge-211/wlmb/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 211 +# Task 1: Toeplitz Matrix +# +# See https://wlmb.github.io/2023/04/03/PWC211/#task-1-toeplitz-matrix +use v5.36; +use List::Util qw(max min uniq); +my @matrix; +my $N=0; # number of rows +while(<>){ # read the matrix, a space separated row at a time + $matrix[$N++]=[split " "]; +} +my $M=@{$matrix[0]}; # Number of columns +@{$matrix[$_]}==$M || die "Not rectangular" for(1..$N-1); +my $largest=max($N,$M); +my $toeplitz=1; # matrix is toeplitz +for my $diagonal(-$M+1..$N-1){ + $toeplitz &&= # unless it is not + 1==uniq + map {$matrix[$diagonal+$_][$_]} + max(0, -$diagonal)..min($M-1,$N-$diagonal-1); +} +say "@{$matrix[$_]}" for 0..$N-1; +say " -> ", $toeplitz?"true":"false"; diff --git a/challenge-211/wlmb/perl/ch-2.pl b/challenge-211/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..ae25ceea91 --- /dev/null +++ b/challenge-211/wlmb/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 211 +# Task 2: Split Same Average +# +# See https://wlmb.github.io/2023/04/03/PWC211/#task-2-split-same-average +use v5.36; +use Algorithm::Combinatorics qw(subsets); +use List::Util qw(sum); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to test if the set N1 N2... may be split into two proper subsets + with the same average + FIN +my $avg=sum(@ARGV)/@ARGV; +my $subsets=subsets(\@ARGV); +$subsets->next; # Throw away the complete set +my $candidate; +while($candidate=$subsets->next){ + next if @$candidate==0; # Throw away the empty set + last if sum(@$candidate)==$avg*@$candidate; # success +} +say("@ARGV -> ", $candidate && @$candidate? "True: @$candidate" : "False") -- cgit From 8ab37815f55b019f73272256613e2e432fa0da7c Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Mon, 3 Apr 2023 21:05:13 -0700 Subject: Robbie Hatley's Perl solutions to The Weekly Challenge 211 --- challenge-211/robbie-hatley/blog.txt | 1 + challenge-211/robbie-hatley/ch-1.pl | 90 ++++++++++++++++++++++++++ challenge-211/robbie-hatley/ch-2.pl | 118 +++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 challenge-211/robbie-hatley/blog.txt create mode 100755 challenge-211/robbie-hatley/ch-1.pl create mode 100755 challenge-211/robbie-hatley/ch-2.pl (limited to 'challenge-211') diff --git a/challenge-211/robbie-hatley/blog.txt b/challenge-211/robbie-hatley/blog.txt new file mode 100644 index 0000000000..03447b9b02 --- /dev/null +++ b/challenge-211/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/04/robbie-hatleys-perl-solutions-to-weekly.html \ No newline at end of file diff --git a/challenge-211/robbie-hatley/ch-1.pl b/challenge-211/robbie-hatley/ch-1.pl new file mode 100755 index 0000000000..be58407553 --- /dev/null +++ b/challenge-211/robbie-hatley/ch-1.pl @@ -0,0 +1,90 @@ +#! /bin/perl +# Robbie Hatley's Perl solutions to The Weekly Challenge #211-1 + +# ====================================================================== +# PROBLEM DESCRIPTION: + +=pod + +Task 1: Toeplitz Matrix +Submitted by: Mohammad S Anwar +You are given a matrix m x n. Write a script to find out if the given +matrix is Toeplitz Matrix. A matrix is Toeplitz if every diagonal from +top-left to bottom-right has the same elements. + +Example 1: +Input: [[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]] +Output: true + +Example 2: +Input: [[1, 2, 3], [3, 2, 1]] +Output: false + +=cut + +# ====================================================================== +# INPUT / OUTPUT NOTES: +# Input is from built-in array-of-arrays or from @ARGV. If using @ARGV, +# input should be one 'single-quoted' string expressing an array of +# arrays of arrays of integers in valid Perl syntax, with each array +# containing only arrays of the same size, like so: +# ./ch-1.pl '([[1,2],[2,3]], [[1,3,5],[3,5,1],[5,1,3]])' +# +# Output is to STDOUT and will be the input array followed by +# "Matrix IS Toeplitz" or "Matrix is NOT Toeplitz" + +# ====================================================================== +# PRELIMINARIES: +use v5.36; +use strict; +use warnings; + +# ====================================================================== +# SUBROUTINES: + +# Say whether-or-not a referred-to rectangular 2-d matrix is Toeplitz, +# without altering the original matrix: +sub is_toeplitz($mref){ + # Make a deep copy of @$aref (simple copy is NOT deep copy!!!): + my $height = scalar(@$mref); + my $width = scalar(@{$mref->[0]}); + say "Height = $height"; + say "Width = $width"; + # Test length 2+ diagonals starting from top: + for ( my $i = 0 ; $i <= $width-2 ; ++$i ){ + for ( my $j = $i+1, my $k = 1 ; $j < $width && $k < $height ; ++$j, ++$k ){ + return 0 if $mref->[$k]->[$j] != $mref->[0]->[$i]; + } + } + # Test length 2+ diagonals starting from left: + for ( my $i = 1 ; $i <= $height-2 ; ++$i ){ + for ( my $j = 1, my $k = $i+1 ; $j < $width && $k < $height ; ++$j, ++$k ){ + return 0 if $mref->[$k]->[$j] != $mref->[$i]->[0]; + } + } + return 1; +} + +# ====================================================================== +# DEFAULT INPUTS: +my @matrices = +( + [[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]], + [[1, 2, 3], [3, 2, 1]], + [[1,2],[2,1]], + [[1,3,5],[3,5,1],[5,1,3]], + [[1,5],[4,1],[2,4]], +); + +# ====================================================================== +# NON-DEFAULT INPUTS: +if (@ARGV) {@matrices = eval($ARGV[0])} + +# ====================================================================== +# MAIN BODY OF SCRIPT: +for my $matrix (@matrices){ + say ''; + say 'Matrix:'; + say "@$_" for @$matrix; + say is_toeplitz($matrix) ? "Matrix IS Toeplitz" : "Matrix is NOT Toeplitz"; +} diff --git a/challenge-211/robbie-hatley/ch-2.pl b/challenge-211/robbie-hatley/ch-2.pl new file mode 100755 index 0000000000..13325d3912 --- /dev/null +++ b/challenge-211/robbie-hatley/ch-2.pl @@ -0,0 +1,118 @@ +#! /bin/perl +# Robbie Hatley's Perl solutions to The Weekly Challenge #211-2 + +# ====================================================================== +# PROBLEM DESCRIPTION: + +=pod + +Task 2: Split Same Average +Submitted by: Mohammad S Anwar +You are given an array of integers. Write a script to find out if the +given can be split into two separate arrays whose average are the same. + +Example 1: Input=(1, 2, 3, 4, 5, 6, 7, 8) Output=true +Example 2: Input=(1, 3) Output=false + +=cut + +# ====================================================================== +# INPUT / OUTPUT NOTES: +# Input is from built-in array-of-arrays or from @ARGV. If using @ARGV, +# input should be one 'single-quoted' string expressing an array of +# arrays of integers in valid Perl syntax. +# +# Output is to STDOUT and will be input array followed by either +# a split with equal averages or a notice that no such split exists. + +# ====================================================================== +# PRELIMINARIES: +use v5.36; +use strict; +use warnings; +use Set::Partition; +use List::AllUtils 'sum0'; +$"=', '; + +# ====================================================================== +# VARIABLES: +my $db = 0; # Debug? + +# ====================================================================== +# SUBROUTINES: + +# Obtain an array of all partitions of a given set into two non-empty +# parts with the size of the first part not-greater-than the size of the +# second part (to avoid duplicate partitions): +sub two_non_empty ($aref, $partref){ + # How big is the original array? + my $size = scalar(@{$aref}); + # No need to allow the first part to be more than half the size + # of the array, else we'd get duplicate partitions: + my $limit = int($size/2); + for ( my $n = 1 ; $n <= $limit ; ++$n ){ + my $parts = Set::Partition->new( + list => $aref, + partition => [$n, $size - $n], + ); + while (my $part = $parts->next) { + push @{$partref}, $part; + } + } +} + +# What is the average of the real numbers in a referred-to array? +sub average ($aref) {return sum0(@$aref)/scalar(@$aref);} + +# Are two floating-point numbers "equal" to within one millionth? +sub equal ($x, $y) {abs($x-$y) < 0.000001 ? return 1 : return 0;} + +# ====================================================================== +# DEFAULT INPUTS: +my @arrays = +( + [1, 2, 3, 4, 5, 6, 7, 8], + [1, 3], + [3, 6, -2.3, 8.64, 5.36], + [3, 6, -2, 8, 5], + [4, 6, 8, 9] +); + +# ====================================================================== +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = eval($ARGV[0])} + +# ====================================================================== +# MAIN BODY OF SCRIPT: + +ARRAY: for (@arrays){ + say ''; + say "array = (@$_)"; + my $partitions = []; + two_non_empty($_, $partitions); + my $equal_average_flag = 0; + # If debugging, print lots of extra diagnostics: + if ($db) { + say 'Partitions:'; + for (@$partitions){ + my $a1 = average($_->[0]); + my $a2 = average($_->[1]); + my $e = equal($a1, $a2) ? 'EQUAL!!!' : ''; + say("(@{$_->[0]}) (@{$_->[1]}) $a1 $a2 $e"); + } + } + # Otherwise, just print the basics: + else { + for (@$partitions){ + my $a1 = average($_->[0]); + my $a2 = average($_->[1]); + if (equal($a1, $a2)) { + say 'array can be split into ' + . "(@{$_->[0]}) and (@{$_->[1]}), " + . "both with average $a1"; + next ARRAY; + } + } + say 'No equal-average split exists.'; + } +} -- cgit From 21d3311f8353c8423f56bab95e4910190d8633f3 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 4 Apr 2023 05:35:00 +0000 Subject: Challenge 211 Solutions (Raku) --- challenge-211/mark-anderson/raku/ch-1.raku | 2 +- challenge-211/mark-anderson/raku/ch-2.raku | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'challenge-211') diff --git a/challenge-211/mark-anderson/raku/ch-1.raku b/challenge-211/mark-anderson/raku/ch-1.raku index 2b86bde846..4519f643b6 100644 --- a/challenge-211/mark-anderson/raku/ch-1.raku +++ b/challenge-211/mark-anderson/raku/ch-1.raku @@ -21,5 +21,5 @@ ok toeplitz([1,2], sub toeplitz(+@m) { - @m[^@m.end;^@m[0].end] eqv @m[1..@m.end;1..@m[1].end] + @m[ ^@m.end; ^@m[0].end ] eqv @m[ 1..@m.end; 1..@m[1].end ] } diff --git a/challenge-211/mark-anderson/raku/ch-2.raku b/challenge-211/mark-anderson/raku/ch-2.raku index b6563db667..255ada41f4 100644 --- a/challenge-211/mark-anderson/raku/ch-2.raku +++ b/challenge-211/mark-anderson/raku/ch-2.raku @@ -1,21 +1,21 @@ #!/usr/bin/env raku use Test; -ok split-same-avg(1,2,3,4,5,6,7,8); # [1 8] [5 4 6 3 7 2] +ok split-same-avg(1,2,3,4,5,6,7,8); # [1 8] [2 3 4 5 6 7] nok split-same-avg(1,3); -ok split-same-avg(3,3,5,5,5,2,2,1); # [3 3 5 2] [2 5 5 1] +ok split-same-avg(3,3,5,5,5,2,2,1); # [2 3 3 5] [1 2 5 5] nok split-same-avg(5,5,5,2,2,1); sub split-same-avg(*@nums) { - for (^@nums).combinations(1..(@nums.elems div 2)) -> @a is copy + for (^@nums).combinations(1..@nums.elems div 2) -> @a is copy { - my @b = ((^@nums) (-) @a).keys; + my @b = (^@nums (-) @a).keys; @a = @nums[@a]; @b = @nums[@b]; - return True if (@a.sum / @a.elems) == (@b.sum / @b.elems) + return True if @a.sum / @a.elems == @b.sum / @b.elems } return False -- cgit From a3d0f8555cad823c79011a4a0fecd71c4d17f063 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Wed, 5 Apr 2023 06:25:31 -0700 Subject: fixed some errors --- challenge-211/robbie-hatley/ch-1.pl | 90 ----------------------- challenge-211/robbie-hatley/ch-2.pl | 118 ------------------------------- challenge-211/robbie-hatley/perl/ch-1.pl | 89 +++++++++++++++++++++++ challenge-211/robbie-hatley/perl/ch-2.pl | 118 +++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 208 deletions(-) delete mode 100755 challenge-211/robbie-hatley/ch-1.pl delete mode 100755 challenge-211/robbie-hatley/ch-2.pl create mode 100755 challenge-211/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-211/robbie-hatley/perl/ch-2.pl (limited to 'challenge-211') diff --git a/challenge-211/robbie-hatley/ch-1.pl b/challenge-211/robbie-hatley/ch-1.pl deleted file mode 100755 index be58407553..0000000000 --- a/challenge-211/robbie-hatley/ch-1.pl +++ /dev/null @@ -1,90 +0,0 @@ -#! /bin/perl -# Robbie Hatley's Perl solutions to The Weekly Challenge #211-1 - -# ====================================================================== -# PROBLEM DESCRIPTION: - -=pod - -Task 1: Toeplitz Matrix -Submitted by: Mohammad S Anwar -You are given a matrix m x n. Write a script to find out if the given -matrix is Toeplitz Matrix. A matrix is Toeplitz if every diagonal from -top-left to bottom-right has the same elements. - -Example 1: -Input: [[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]] -Output: true - -Example 2: -Input: [[1, 2, 3], [3, 2, 1]] -Output: false - -=cut - -# ====================================================================== -# INPUT / OUTPUT NOTES: -# Input is from built-in array-of-arrays or from @ARGV. If using @ARGV, -# input should be one 'single-quoted' string expressing an array of -# arrays of arrays of integers in valid Perl syntax, with each array -# containing only arrays of the same size, like so: -# ./ch-1.pl '([[1,2],[2,3]], [[1,3,5],[3,5,1],[5,1,3]])' -# -# Output is to STDOUT and will be the input array followed by -# "Matrix IS Toeplitz" or "Matrix is NOT Toeplitz" - -# ====================================================================== -# PRELIMINARIES: -use v5.36; -use strict; -use warnings; - -# ====================================================================== -# SUBROUTINES: - -# Say whether-or-not a referred-to rectangular 2-d matrix is Toeplitz, -# without altering the original matrix: -sub is_toeplitz($mref){ - # Make a deep copy of @$aref (simple copy is NOT deep copy!!!): - my $height = scalar(@$mref); - my $width = scalar(@{$mref->[0]}); - say "Height = $height"; - say "Width = $width"; - # Test length 2+ diagonals starting from top: - for ( my $i = 0 ; $i <= $width-2 ; ++$i ){ - for ( my $j = $i+1, my $k = 1 ; $j < $width && $k < $height ; ++$j, ++$k ){ - return 0 if $mref->[$k]->[$j] != $mref->[0]->[$i]; - } - } - # Test length 2+ diagonals starting from left: - for ( my $i = 1 ; $i <= $height-2 ; ++$i ){ - for ( my $j = 1, my $k = $i+1 ; $j < $width && $k < $height ; ++$j, ++$k ){ - return 0 if $mref->[$k]->[$j] != $mref->[$i]->[0]; - } - } - return 1; -} - -# ====================================================================== -# DEFAULT INPUTS: -my @matrices = -( - [[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]], - [[1, 2, 3], [3, 2, 1]], - [[1,2],[2,1]], - [[1,3,5],[3,5,1],[5,1,3]], - [[1,5],[4,1],[2,4]], -); - -# ====================================================================== -# NON-DEFAULT INPUTS: -if (@ARGV) {@matrices = eval($ARGV[0])} - -# ====================================================================== -# MAIN BODY OF SCRIPT: -for my $matrix (@matrices){ - say ''; - say 'Matrix:'; - say "@$_" for @$matrix; - say is_toeplitz($matrix) ? "Matrix IS Toeplitz" : "Matrix is NOT Toeplitz"; -} diff --git a/challenge-211/robbie-hatley/ch-2.pl b/challenge-211/robbie-hatley/ch-2.pl deleted file mode 100755 index 13325d3912..0000000000 --- a/challenge-211/robbie-hatley/ch-2.pl +++ /dev/null @@ -1,118 +0,0 @@ -#! /bin/perl -# Robbie Hatley's Perl solutions to The Weekly Challenge #211-2 - -# ====================================================================== -# PROBLEM DESCRIPTION: - -=pod - -Task 2: Split Same Average -Submitted by: Mohammad S Anwar -You are given an array of integers. Write a script to find out if the -given can be split into two separate arrays whose average are the same. - -Example 1: Input=(1, 2, 3, 4, 5, 6, 7, 8) Output=true -Example 2: Input=(1, 3) Output=false - -=cut - -# ====================================================================== -# INPUT / OUTPUT NOTES: -# Input is from built-in array-of-arrays or from @ARGV. If using @ARGV, -# input should be one 'single-quoted' string expressing an array of -# arrays of integers in valid Perl syntax. -# -# Output is to STDOUT and will be input array followed by either -# a split with equal averages or a notice that no such split exists. - -# ====================================================================== -# PRELIMINARIES: -use v5.36; -use strict; -use warnings; -use Set::Partition; -use List::AllUtils 'sum0'; -$"=', '; - -# ====================================================================== -# VARIABLES: -my $db = 0; # Debug? - -# ====================================================================== -# SUBROUTINES: - -# Obtain an array of all partitions of a given set into two non-empty -# parts with the size of the first part not-greater-than the size of the -# second part (to avoid duplicate partitions): -sub two_non_empty ($aref, $partref){ - # How big is the original array? - my $size = scalar(@{$aref}); - # No need to allow the first part to be more than half the size - # of the array, else we'd get duplicate partitions: - my $limit = int($size/2); - for ( my $n = 1 ; $n <= $limit ; ++$n ){ - my $parts = Set::Partition->new( - list => $aref, - partition => [$n, $size - $n], - ); - while (my $part = $parts->next) { - push @{$partref}, $part; - } - } -} - -# What is the average of the real numbers in a referred-to array? -sub average ($aref) {return sum0(@$aref)/scalar(@$aref);} - -# Are two floating-point numbers "equal" to within one millionth? -sub equal ($x, $y) {abs($x-$y) < 0.000001 ? return 1 : return 0;} - -# ====================================================================== -# DEFAULT INPUTS: -my @arrays = -( - [1, 2, 3, 4, 5, 6, 7, 8], - [1, 3], - [3, 6, -2.3, 8.64, 5.36], - [3, 6, -2, 8, 5], - [4, 6, 8, 9] -); - -# ====================================================================== -# NON-DEFAULT INPUTS: -if (@ARGV) {@arrays = eval($ARGV[0])} - -# ====================================================================== -# MAIN BODY OF SCRIPT: - -ARRAY: for (@arrays){ - say ''; - say "array = (@$_)"; - my $partitions = []; - two_non_empty($_, $partitions); - my $equal_average_flag = 0; - # If debugging, print lots of extra diagnostics: - if ($db) { - say 'Partitions:'; - for (@$partitions){ - my $a1 = average($_->[0]); - my $a2 = average($_->[1]); - my $e = equal($a1, $a2) ? 'EQUAL!!!' : ''; - say("(@{$_->[0]}) (@{$_->[1]}) $a1 $a2 $e"); - } - } - # Otherwise, just print the basics: - else { - for (@$partitions){ - my $a1 = average($_->[0]); - my $a2 = average($_->[1]); - if (equal($a1, $a2)) { - say 'array can be split into ' - . "(@{$_->[0]}) and (@{$_->[1]}), " - . "both with average $a1"; - next ARRAY; - } - } - say 'No equal-average split exists.'; - } -} diff --git a/challenge-211/robbie-hatley/perl/ch-1.pl b/challenge-211/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..3fffd3cd1a --- /dev/null +++ b/challenge-211/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,89 @@ +#! /bin/perl +# Robbie Hatley's Perl solutions to The Weekly Challenge #211-1 + +# ====================================================================== +# PROBLEM DESCRIPTION: + +=pod + +Task 1: Toeplitz Matrix +Submitted by: Mohammad S Anwar +You are given a matrix m x n. Write a script to find out if the given +matrix is Toeplitz Matrix. A matrix is Toeplitz if every diagonal from +top-left to bottom-right has the same elements. + +Example 1: +Input: [[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]] +Output: true + +Example 2: +Input: [[1, 2, 3], [3, 2, 1]] +Output: false + +=cut + +# ====================================================================== +# INPUT / OUTPUT NOTES: +# Input is from built-in array-of-arrays or from @ARGV. If using @ARGV, +# input should be one 'single-quoted' string expressing an array of +# arrays of arrays of integers in valid Perl syntax, with each array +# containing only arrays of the same size, like so: +# ./ch-1.pl '([[1,2],[2,3]], [[1,3,5],[3,5,1],[5,1,3]])' +# +# Output is to STDOUT and will be the input array followed by +# "Matrix IS Toeplitz" or "Matrix is NOT Toeplitz" + +# ====================================================================== +# PRELIMINARIES: +use v5.36; +use strict; +use warnings; + +# ====================================================================== +# SUBROUTINES: + +# Say whether-or-not a referred-to rectangular 2-d matrix is Toeplitz, +# without altering the original matrix: +sub is_toeplitz($mref){ + my $height = scalar(@$mref); + my $width = scalar(@{$mref->[0]}); + say "Height = $height"; + say "Width = $width"; + # Test length 2+ diagonals starting from top: + for ( my $i = 0 ; $i <= $width-2 ; ++$i ){ + for ( my $j = $i+1, my $k = 1 ; $j < $width && $k < $height ; ++$j, ++$k ){ + return 0 if $mref->[$k]->[$j] != $mref->[0]->[$i]; + } + } + # Test length 2+ diagonals starting from left: + for ( my $i = 1 ; $i <= $height-2 ; ++$i ){ + for ( my $j = 1, my $k = $i+1 ; $j < $width && $k < $height ; ++$j, ++$k ){ + return 0 if $mref->[$k]->[$j] != $mref->[$i]->[0]; + } + } + return 1; +} + +# ====================================================================== +# DEFAULT INPUTS: +my @matrices = +( + [[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]], + [[1, 2, 3], [3, 2, 1]], + [[1,2],[2,1]], + [[1,3,5],[3,5,1],[5,1,3]], + [[1,5],[4,1],[2,4]], +); + +# ====================================================================== +# NON-DEFAULT INPUTS: +if (@ARGV) {@matrices = eval($ARGV[0])} + +# ====================================================================== +# MAIN BODY OF SCRIPT: +for my $matrix (@matrices){ + say ''; + say 'Matrix:'; + say "@$_" for @$matrix; + say is_toeplitz($matrix) ? "Matrix IS Toeplitz" : "Matrix is NOT Toeplitz"; +} diff --git a/challenge-211/robbie-hatley/perl/ch-2.pl b/challenge-211/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..13325d3912 --- /dev/null +++ b/challenge-211/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,118 @@ +#! /bin/perl +# Robbie Hatley's Perl solutions to The Weekly Challenge #211-2 + +# ====================================================================== +# PROBLEM DESCRIPTION: + +=pod + +Task 2: Split Same Average +Submitted by: Mohammad S Anwar +You are given an array of integers. Write a script to find out if the +given can be split into two separate arrays whose average are the same. + +Example 1: Input=(1, 2, 3, 4, 5, 6, 7, 8) Output=true +Example 2: Input=(1, 3) Output=false + +=cut + +# ====================================================================== +# INPUT / OUTPUT NOTES: +# Input is from built-in array-of-arrays or from @ARGV. If using @ARGV, +# input should be one 'single-quoted' string expressing an array of +# arrays of integers in valid Perl syntax. +# +# Output is to STDOUT and will be input array followed by either +# a split with equal averages or a notice that no such split exists. + +# ====================================================================== +# PRELIMINARIES: +use v5.36; +use strict; +use warnings; +use Set::Partition; +use List::AllUtils 'sum0'; +$"=', '; + +# ====================================================================== +# VARIABLES: +my $db = 0; # Debug? + +# ====================================================================== +# SUBROUTINES: + +# Obtain an array of all partitions of a given set into two non-empty +# parts with the size of the first part not-greater-than the size of the +# second part (to avoid duplicate partitions): +sub two_non_empty ($aref, $partref){ + # How big is the original array? + my $size = scalar(@{$aref}); + # No need to allow the first part to be more than half the size + # of the array, else we'd get duplicate partitions: + my $limit = int($size/2); + for ( my $n = 1 ; $n <= $limit ; ++$n ){ + my $parts = Set::Partition->new( + list => $aref, + partition => [$n, $size - $n], + ); + while (my $part = $parts->next) { + push @{$partref}, $part; + } + } +} + +# What is the average of the real numbers in a referred-to array? +sub average ($aref) {return sum0(@$aref)/scalar(@$aref);} + +# Are two floating-point numbers "equal" to within one millionth? +sub equal ($x, $y) {abs($x-$y) < 0.000001 ? return 1 : return 0;} + +# ====================================================================== +# DEFAULT INPUTS: +my @arrays = +( + [1, 2, 3, 4, 5, 6, 7, 8], + [1, 3], + [3, 6, -2.3, 8.64, 5.36], + [3, 6, -2, 8, 5], + [4, 6, 8, 9] +); + +# ====================================================================== +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = eval($ARGV[0])} + +# ====================================================================== +# MAIN BODY OF SCRIPT: + +ARRAY: for (@arrays){ + say ''; + say "array = (@$_)"; + my $partitions = []; + two_non_empty($_, $partitions); + my $equal_average_flag = 0; + # If debugging, print lots of extra diagnostics: + if ($db) { + say 'Partitions:'; + for (@$partitions){ + my $a1 = average($_->[0]); + my $a2 = average($_->[1]); + my $e = equal($a1, $a2) ? 'EQUAL!!!' : ''; + say("(@{$_->[0]}) (@{$_->[1]}) $a1 $a2 $e"); + } + } + # Otherwise, just print the basics: + else { + for (@$partitions){ + my $a1 = average($_->[0]); + my $a2 = average($_->[1]); + if (equal($a1, $a2)) { + say 'array can be split into ' + . "(@{$_->[0]}) and (@{$_->[1]}), " + . "both with average $a1"; + next ARRAY; + } + } + say 'No equal-average split exists.'; + } +} -- cgit From d1a80636474c530b07c08886a8a6b896c9421418 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Wed, 5 Apr 2023 06:44:14 -0700 Subject: made some improvements --- challenge-211/robbie-hatley/perl/ch-2.pl | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'challenge-211') diff --git a/challenge-211/robbie-hatley/perl/ch-2.pl b/challenge-211/robbie-hatley/perl/ch-2.pl index 13325d3912..6daeb97d97 100755 --- a/challenge-211/robbie-hatley/perl/ch-2.pl +++ b/challenge-211/robbie-hatley/perl/ch-2.pl @@ -44,21 +44,24 @@ my $db = 0; # Debug? # Obtain an array of all partitions of a given set into two non-empty # parts with the size of the first part not-greater-than the size of the # second part (to avoid duplicate partitions): -sub two_non_empty ($aref, $partref){ +sub two_non_empty ($aref){ # How big is the original array? my $size = scalar(@{$aref}); + # Make an array to hold partitions: + my @partitions; # No need to allow the first part to be more than half the size # of the array, else we'd get duplicate partitions: my $limit = int($size/2); for ( my $n = 1 ; $n <= $limit ; ++$n ){ - my $parts = Set::Partition->new( + my $size_n_partitions = Set::Partition->new( list => $aref, partition => [$n, $size - $n], ); - while (my $part = $parts->next) { - push @{$partref}, $part; + while (my $partition = $size_n_partitions->next) { + push @partitions, $partition; } } + return \@partitions; } # What is the average of the real numbers in a referred-to array? @@ -88,8 +91,7 @@ if (@ARGV) {@arrays = eval($ARGV[0])} ARRAY: for (@arrays){ say ''; say "array = (@$_)"; - my $partitions = []; - two_non_empty($_, $partitions); + my $partitions = two_non_empty($_); my $equal_average_flag = 0; # If debugging, print lots of extra diagnostics: if ($db) { -- cgit From 6e98239f9d7df539606d6f6412d9aae8002851ae Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 3 Apr 2023 23:06:58 +0200 Subject: Solution to task 1 --- challenge-211/jo-37/perl/ch-1.pl | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 challenge-211/jo-37/perl/ch-1.pl (limited to 'challenge-211') diff --git a/challenge-211/jo-37/perl/ch-1.pl b/challenge-211/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..586b91905e --- /dev/null +++ b/challenge-211/jo-37/perl/ch-1.pl @@ -0,0 +1,85 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0 '!float'; +use PDL; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < 1) has N + M - 3 diagonals having more than one +# element. Creating a set of NxN matrices where each of them have one +# of the orignal matrix' diagonals as its main diagonal. Then take the +# diagonal of these matrices and re-arrange them into a new matrix +# having the main diagonals of the matrix series as rows. Taking minimum +# and maximum over the rows. If min and max equals for every row, the +# matrix is Toeplitz. +# Note: Utilizing BAD values in incomplete diagonals that do not account +# for minimum or maximum. +sub is_toeplitz { + (my $m = pdl @_)->badflag(1); + my ($min, $max) = ( + cat map $_->diagonal(0, 1), + $m->range($m->dim(0) - 2 - sequence(indx, 1, $m->shape->sum - 3), + $m->dim(1), 't') + ->reorder(1,2,0)->dog + )->minmaximum; + + all $min == $max; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok is_toeplitz( + [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3]), 'example 1'; + + ok !is_toeplitz( + [1, 2, 3], + [3, 2, 1]), 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + ok is_toeplitz(sequence(4) + 5 - sequence(5)->dummy(0)), '4 x 5'; + ok is_toeplitz(sequence(5) + 4 - sequence(4)->dummy(0)), '5 x 4'; + + my $nt = sequence(4) + 5 - sequence(5)->dummy(0); + $nt->set(1, 4, 0); + ok !is_toeplitz($nt), 'one element failing'; + } + + done_testing; + exit; +} -- cgit From 2bcb7ffce5bfbf0c9f1e23ef714c678f9f992a8a Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 4 Apr 2023 22:31:34 +0200 Subject: Solution to task 2 --- challenge-211/jo-37/perl/ch-2.pl | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 challenge-211/jo-37/perl/ch-2.pl (limited to 'challenge-211') diff --git a/challenge-211/jo-37/perl/ch-2.pl b/challenge-211/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..dd2c6b03fb --- /dev/null +++ b/challenge-211/jo-37/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util qw(forcomb lastfor); +use List::Util qw(sum); +use experimental qw(signatures); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < Date: Thu, 6 Apr 2023 11:36:26 +0100 Subject: RogerBW solutions for challenge no. 211 --- challenge-211/roger-bell-west/javascript/ch-1.js | 44 +++++++++ challenge-211/roger-bell-west/javascript/ch-2.js | 69 ++++++++++++++ challenge-211/roger-bell-west/kotlin/ch-1.kt | 44 +++++++++ challenge-211/roger-bell-west/kotlin/ch-2.kt | 69 ++++++++++++++ challenge-211/roger-bell-west/lua/ch-1.lua | 46 +++++++++ challenge-211/roger-bell-west/lua/ch-2.lua | 78 ++++++++++++++++ challenge-211/roger-bell-west/perl/ch-1.pl | 38 ++++++++ challenge-211/roger-bell-west/perl/ch-2.pl | 35 +++++++ challenge-211/roger-bell-west/postscript/ch-1.ps | 77 +++++++++++++++ challenge-211/roger-bell-west/postscript/ch-2.ps | 113 +++++++++++++++++++++++ challenge-211/roger-bell-west/python/ch-1.py | 34 +++++++ challenge-211/roger-bell-west/python/ch-2.py | 32 +++++++ challenge-211/roger-bell-west/raku/ch-1.p6 | 36 ++++++++ challenge-211/roger-bell-west/raku/ch-2.p6 | 29 ++++++ challenge-211/roger-bell-west/ruby/ch-1.rb | 43 +++++++++ challenge-211/roger-bell-west/ruby/ch-2.rb | 39 ++++++++ challenge-211/roger-bell-west/rust/ch-1.rs | 42 +++++++++ challenge-211/roger-bell-west/rust/ch-2.rs | 39 ++++++++ challenge-211/roger-bell-west/tests.yaml | 49 ++++++++++ 19 files changed, 956 insertions(+) create mode 100755 challenge-211/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-211/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-211/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-211/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-211/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-211/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-211/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-211/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-211/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-211/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-211/roger-bell-west/python/ch-1.py create mode 100755 challenge-211/roger-bell-west/python/ch-2.py create mode 100755 challenge-211/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-211/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-211/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-211/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-211/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-211/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-211/roger-bell-west/tests.yaml (limited to 'challenge-211') diff --git a/challenge-211/roger-bell-west/javascript/ch-1.js b/challenge-211/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..c400a681b4 --- /dev/null +++ b/challenge-211/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,44 @@ +#! /usr/bin/node + +"use strict" + +function toeplitzmatrix(a) { + let ym = a.length - 1; + let xm = a[0].length - 1; + let toeplitz = true; + for (let xb = 1 - xm; xb <= ym - 1; xb++) { + let init = true; + let tv = 0; + for (let x = xb; x <= xb + xm; x++) { + if (x >= 0 && x <= xm) { + let y = x - xb; + if (y >= 0 && y <= ym) { + if (init) { + init = false; + tv = a[y][x]; + } else if (a[y][x] != tv) { + toeplitz = false; + break; + } + } + } + } + if (!toeplitz) { + break; + } + } + return toeplitz +} + +if (toeplitzmatrix([[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!toeplitzmatrix([[1, 2, 3], [3, 2, 1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-211/roger-bell-west/javascript/ch-2.js b/challenge-211/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..75870a79b1 --- /dev/null +++ b/challenge-211/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,69 @@ +#! /usr/bin/node + +"use strict" + +function combinations(arr, k) { + let c = []; + for (let i = 0; i < k; i++) { + c.push(i); + } + c.push(arr.length); + c.push(0); + let out = []; + while (true) { + let inner = []; + for (let i = k-1; i >= 0; i--) { + inner.push(arr[c[i]]); + } + out.push(inner); + let j = 0; + while (c[j] + 1 == c[j + 1]) { + c[j] = j; + j += 1; + } + if (j >= k) { + break; + } + c[j] += 1; + } + return out; +} + +function splitsameaverage(a) { + let ss = a.reduce((x, y) => x + y, 0); + let ml = a.length; + let mx = Math.floor(ml / 2); + let ssa = false; + for (let n = 1; n <= mx; n++) { + for (let c of combinations(a, n)) { + let ca = c.reduce((x, y) => x + y, 0); + if (ca / n == (ss - ca) / (ml - n)) { + ssa = true; + break; + } + } + if (ssa) { + break; + } + } + return ssa; +} + +if (splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!splitsameaverage([1, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (splitsameaverage([1, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-211/roger-bell-west/kotlin/ch-1.kt b/challenge-211/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..96d2c4f559 --- /dev/null +++ b/challenge-211/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,44 @@ +fun toeplitzmatrix(a: List>): Boolean { + val ym = a.size - 1 + val xm = a[0].size - 1 + var toeplitz = true + for (xb in 1 - xm .. ym - 1) { + var init = true + var tv = 0 + for (x in xb .. xb + xm) { + if (x >= 0 && x <= xm) { + val y = x - xb + if (y >= 0 && y <= ym) { + if (init) { + init = false + tv = a[y][x] + } else if (a[y][x] != tv) { + toeplitz = false + break + } + } + } + } + if (!toeplitz) { + break + } + } + return toeplitz +} + +fun main() { + + if (toeplitzmatrix(listOf(listOf(4, 3, 2, 1), listOf(5, 4, 3, 2), listOf(6, 5, 4, 3)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!toeplitzmatrix(listOf(listOf(1, 2, 3), listOf(3, 2, 1)))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-211/roger-bell-west/kotlin/ch-2.kt b/challenge-211/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..9145cbe67f --- /dev/null +++ b/challenge-211/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,69 @@ +fun combinations(arr: List, k: Int): List> { + var c = ArrayList() + for (i in 0 .. k-1) { + c.add(i) + } + c.add(arr.size) + c.add(0) + var out = ArrayList>() + while (true) { + var inner = ArrayList() + for (i in k-1 downTo 0) { + inner.add(arr[c[i]]) + } + out.add(inner.toList()) + var j = 0 + while (c[j] + 1 == c[j + 1]) { + c[j] = j + j += 1 + } + if (j >= k) { + break + } + c[j] += 1 + } + return out.toList(); +} + +fun splitsameaverage(a: List): Boolean { + val ss = a.sum() + val ml = a.size + val mx = ml / 2 + var ssa = false + for (n in 1 .. mx) { + for (c in combinations(a, n)) { + var ca = c.sum() + if (ca.toFloat() / n.toFloat() == (ss - ca).toFloat() / (ml - n).toFloat()) { + ssa = true + break + } + } + if (ssa) { + break + } + } + return ssa +} + +fun main() { + + if (splitsameaverage(listOf(1, 2, 3, 4, 5, 6, 7, 8))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!splitsameaverage(listOf(1, 3))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (splitsameaverage(listOf(1, 2, 3))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-211/roger-bell-west/lua/ch-1.lua b/challenge-211/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..50dfb58b1c --- /dev/null +++ b/challenge-211/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,46 @@ +#! /usr/bin/lua + +function toeplitzmatrix(a) + local ym = #a - 1 + local xm = #(a[1]) - 1 + local toeplitz = true + for xb = (1 - xm), (ym - 1) do + local init = true + local tv = 0 + for xi = xb, xb + xm do + if xi >= 0 and xi <= xm then + local x = xi + 1 + local yi = xi - xb + if yi >= 0 and yi <= ym then + local y = yi + 1 + if init then + init = false + tv = a[y][x] + elseif a[y][x] ~= tv then + toeplitz = false + break + end + end + end + end + if not toeplitz then + break + end + end + return toeplitz +end + +if toeplitzmatrix({{4, 3, 2, 1}, {5, 4, 3, 2}, {6, 5, 4, 3}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not toeplitzmatrix({{1, 2, 3}, {3, 2, 1}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-211/roger-bell-west/lua/ch-2.lua b/challenge-211/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..c524e76c6d --- /dev/null +++ b/challenge-211/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,78 @@ +#! /usr/bin/lua + +function combinations(arr, k) + local c = {} + for i = 1, k do + table.insert(c, i) + end + table.insert(c, #arr + 1) + table.insert(c, 0) + local out = {} + while true do + local inner = {} + for i = k, 1, -1 do + table.insert(inner, arr[c[i]]) + end + table.insert(out, inner) + local j = 1 + while c[j] + 1 == c[j + 1] do + c[j] = j + j = j + 1 + end + if j > k then +