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