diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-02-19 08:54:04 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-02-19 14:05:32 +0100 |
| commit | 6d1a19d9005fa4d39338b66c00c16527236d19fa (patch) | |
| tree | 46cb0cc538bcad19d16564f9a053744bcd8aef28 | |
| parent | d56f5846adcf3864f7b9dd2426d85ae68579729e (diff) | |
| download | perlweeklychallenge-club-6d1a19d9005fa4d39338b66c00c16527236d19fa.tar.gz perlweeklychallenge-club-6d1a19d9005fa4d39338b66c00c16527236d19fa.tar.bz2 perlweeklychallenge-club-6d1a19d9005fa4d39338b66c00c16527236d19fa.zip | |
PWC 257
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
Task 1 PL/Java done
Task2 PL/java done
21 files changed, 486 insertions, 3 deletions
diff --git a/challenge-257/luca-ferrari/blog-1.txt b/challenge-257/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..8a00312c43 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task1 diff --git a/challenge-257/luca-ferrari/blog-10.txt b/challenge-257/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..f579c1d471 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge257.html#task2pljava diff --git a/challenge-257/luca-ferrari/blog-2.txt b/challenge-257/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..704d8eaa14 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task2 diff --git a/challenge-257/luca-ferrari/blog-3.txt b/challenge-257/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..8703580f7f --- /dev/null +++ b/challenge-257/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task1plperl diff --git a/challenge-257/luca-ferrari/blog-4.txt b/challenge-257/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..ddf34db218 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task2plperl diff --git a/challenge-257/luca-ferrari/blog-5.txt b/challenge-257/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..ed61ce5418 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task1plpgsql diff --git a/challenge-257/luca-ferrari/blog-6.txt b/challenge-257/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..b883396dfc --- /dev/null +++ b/challenge-257/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task2plpgsql diff --git a/challenge-257/luca-ferrari/blog-7.txt b/challenge-257/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..7cd80518b3 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task1python diff --git a/challenge-257/luca-ferrari/blog-8.txt b/challenge-257/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..7aacad87f4 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task2python diff --git a/challenge-257/luca-ferrari/blog-9.txt b/challenge-257/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..00a1afde82 --- /dev/null +++ b/challenge-257/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/19/PerlWeeklyChallenge257.html#task1pljava diff --git a/challenge-257/luca-ferrari/pljava/pom.xml b/challenge-257/luca-ferrari/pljava/pom.xml index eec35352dc..6fe5f45834 100644 --- a/challenge-257/luca-ferrari/pljava/pom.xml +++ b/challenge-257/luca-ferrari/pljava/pom.xml @@ -5,11 +5,11 @@ <modelVersion>4.0.0</modelVersion> <groupId>PWC</groupId> - <artifactId>PWC256</artifactId> + <artifactId>PWC257</artifactId> <version>1</version> - <name>Perl Weekly Challenge 256</name> - <description>Implementation of the tasks in PL/Java for PWC 256</description> + <name>Perl Weekly Challenge 257</name> + <description>Implementation of the tasks in PL/Java for PWC 257</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-257/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-257/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..bfbdaf121b --- /dev/null +++ b/challenge-257/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,83 @@ +package PWC257; + +/** + * PL/Java implementation for PWC 257 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC257.jar luca@venkman:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC257.jar', 'PWC257', true ); + + select 257.task1_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC257.jar', 'PWC257', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import org.postgresql.pljava.ResultSetProvider; +import java.sql.ResultSet; + +import java.util.*; +import java.sql.SQLException; +import java.util.logging.*; + +public class Task1 implements ResultSetProvider { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc257", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final ResultSetProvider task1_pljava( int[] numbers ) throws SQLException { + logger.log( Level.INFO, "Entering task1_pljava" ); + return new Task1( numbers ); + + } + + public Task1( int[] nums ) { + numbers = nums; + } + + private int[] numbers; + + @Override + public boolean assignRowValues(ResultSet receiver, int currentRow) + throws SQLException { + + if ( currentRow >= numbers.length ) + return false; + + int current = numbers[ currentRow ]; + + int count = 0; + for ( int other : numbers ) + if ( other < current ) + count++; + + receiver.updateInt( 1, count ); + return true; + + } + + + + @Override + public void close() { } + +} diff --git a/challenge-257/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-257/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..d92f3d5ad4 --- /dev/null +++ b/challenge-257/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,93 @@ +package PWC257; + +/** + * PL/Java implementation for PWC 257 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC257.jar luca@venkman:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC257.jar', 'PWC257', true ); + + select 257.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC257.jar', 'PWC257', true ); + + +Example of invocation: +select pwc257.task2_pljava( array[ 1,0,0,0,1,0,0,0,1 ], 3 ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.sql.SQLException; +import java.util.logging.*; + +public class Task2 { + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc257", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task2_pljava( int[] matrix, int cols ) throws SQLException { + logger.log( Level.INFO, "Entering task2_pljava" ); + + boolean ok = false; + List<Integer> zero_rows = new LinkedList<Integer>(); + Integer current = null; + List<int[]> leadings = new LinkedList<int[]>(); + + for ( int row = 0; row < matrix.length / cols ; row++ ) { + current = null; + + for ( int col = 0; col < cols; col++ ) { + + int element = matrix[ row * cols + col ]; + if ( element == 0 || current != null ) + continue; + + current = element; + if ( current != 1 ) + return false; + + leadings.add( new int[]{ row, col } ); + + if ( leadings.size() <= 1 ) + continue; + + if ( leadings.get( leadings.size() - 2 )[ 0 ] == ( leadings.get( leadings.size() - 1 )[ 0 ] - 1 ) + && leadings.get( leadings.size() - 2 )[ 1 ] >= leadings.get( leadings.size() - 1 )[ 1 ] ) + return false; + } + + if ( current == null ) { + // all zero row + zero_rows.add( row ); + + if ( zero_rows.size() <= 1 ) + continue; + + if ( zero_rows.get( zero_rows.size() - 2 ) != ( zero_rows.get( zero_rows.size() - 1) - 1 ) ) + return false; + } + } + + return true; + + } +} diff --git a/challenge-257/luca-ferrari/plperl/ch-1.plperl b/challenge-257/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..cd90b8c23a --- /dev/null +++ b/challenge-257/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,23 @@ +-- +-- Perl Weekly Challenge 257 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +-- + +CREATE SCHEMA IF NOT EXISTS pwc257; + +CREATE OR REPLACE FUNCTION +pwc257.task1_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $numbers ) = @_; + + for my $current ( $numbers->@* ) { + return_next( scalar( grep( { $_ < $current } $numbers->@* ) ) ); + } + + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-257/luca-ferrari/plperl/ch-2.plperl b/challenge-257/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..bd4a802dd3 --- /dev/null +++ b/challenge-257/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,57 @@ +-- +-- Perl Weekly Challenge 257 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +-- + +CREATE SCHEMA IF NOT EXISTS pwc257; + +CREATE OR REPLACE FUNCTION +pwc257.task2_plperl( int[][] ) +RETURNS bool +AS $CODE$ + + my ( $matrix ) = @_; + + my $ok = 1; + my @zero_rows; + my @leadings; + + my $row ( 0 .. $matrix->@* - 1 ) { + my $first_in_row = undef; + + for my $col ( 0 .. $matrix->@* - 1 ) { + next if $matrix->[ $row ][ $col ] == 0; + next if $first_in_row; + + $first_in_row = $matrix->[ $row ][ $col ] if ( ! $first_in_row ); + + # the first non-zero value of every row must be a one + $ok = 0 and last if ( $first_in_row != 1 ); + + # leading one + push @leadings, { row => $row, col => $col }; + + } + + if ( ! $first_in_row ) { + # the row was filled with zeros + push @zero_rows, $row; + + next if @zero_rows <= 1; + $ok = 0 and last if ( @zero_rows[ -1 ] != ( $row - 1 ) ); + } + elsif ( $first_in_row == 1 ) { + next if @leadings <= 1; + $ok = 0 and last if ( @leadings[ -2 ]->{ col } >= @leadings[ -1 ]->{ col } + || @leadings[ -2 ]->{ row } != @leadings[ -1 ]->{ row } - 1 ); + } + + return $ok if ! $ok; + } + + return $ok; + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-257/luca-ferrari/plpgsql/ch-1.sql b/challenge-257/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..6f6b967c8d --- /dev/null +++ b/challenge-257/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,19 @@ +-- +-- Perl Weekly Challenge 257 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +-- + +CREATE SCHEMA IF NOT EXISTS pwc257; + +CREATE OR REPLACE FUNCTION +pwc257.task1_plpgsql( numbers int[] ) +RETURNS SETOF int +AS $CODE$ + select z + from unnest( numbers ) v, + lateral ( select count(x) + from unnest( numbers ) x + WHERE x < v ) as y(z); +$CODE$ +LANGUAGE sql; diff --git a/challenge-257/luca-ferrari/plpgsql/ch-2.sql b/challenge-257/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..cb70f5b58c --- /dev/null +++ b/challenge-257/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 257 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +-- + +CREATE SCHEMA IF NOT EXISTS pwc257; + +CREATE OR REPLACE FUNCTION +pwc257.task2_plpgsql( matrix int[] ) +RETURNS bool +AS $CODE$ + select pwc257.task2_plperl( matrix ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-257/luca-ferrari/python/ch-1.py b/challenge-257/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..29f331e81f --- /dev/null +++ b/challenge-257/luca-ferrari/python/ch-1.py @@ -0,0 +1,25 @@ +#!python + +# +# Perl Weekly Challenge 257 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + output = [] + numbers = list( map( int, args ) ) + for current in numbers: + output.append( len( list( filter( lambda x: x < current, numbers ) ) ) ) + + return output + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-257/luca-ferrari/python/ch-2.py b/challenge-257/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..141fbd3da3 --- /dev/null +++ b/challenge-257/luca-ferrari/python/ch-2.py @@ -0,0 +1,61 @@ +#!python + +# +# Perl Weekly Challenge 257 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( numbers ): + + ok = False + zero_rows = [] + leadings = [] + + for row in range( 0, len( numbers ) ): + first_in_row = None + + for col in range( 0, len( numbers[ row ] ) ): + if numbers[ row ][ col ] == 0: + continue + + if first_in_row != None: + continue + + first_in_row = numbers[ row ][ col ] + + + if first_in_row != 1: + return False + + leadings.append( { 'row' : row, 'col' : col } ) + if len(leadings) <= 1: + continue + + if leadings[ -2 ][ 'row' ] != ( row - 1 ) and leadings[ -2 ][ 'col' ] >= col: + return False + + if first_in_row == None: + zero_rows.append( row ) + + if len( zero_rows ) <= 1: + continue + if zero_rows[ -2 ] != ( row - 1 ): + return False + elif first_in_row == 1: + if len( leadings ) <= 1: + continue + if leadings[ -2 ][ 'row' ] != leadings[ -1 ][ 'row' ] -1 and leadings[ -2 ][ 'col' ] >= leadings[ -1 ][ 'col' ]: + return False + + return True + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( [ [0,1,0], [0,0,1], [0,0,0] ] ) ) diff --git a/challenge-257/luca-ferrari/raku/ch-1.raku b/challenge-257/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..9c4a9b672d --- /dev/null +++ b/challenge-257/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 257 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +# + +sub MAIN( *@numbers where { @numbers.elems == @numbers.grep( * ~~ Int ).elems } ) { + @numbers.map( -> $current { @numbers.grep( * < $current ).elems } ).join( ', ' ).say; +} diff --git a/challenge-257/luca-ferrari/raku/ch-2.raku b/challenge-257/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..a393c68939 --- /dev/null +++ b/challenge-257/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,85 @@ +#!raku + +# +# Perl Weekly Challenge 257 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-257> +# + +sub MAIN() { +# 1. If a row does not consist entirely of zeros, then the first +# nonzero number in the row is a 1. We call this the leading 1. +# 2. If there are any rows that consist entirely of zeros, then +# they are grouped together at the bottom of the matrix. +# 3. In any two successive rows that do not consist entirely of zeros, +# the leading 1 in the lower row occurs farther to the right than +# the leading 1 in the higher row. +# 4. Each column that contains a leading 1 has zeros everywhere else + # in that column. + + my $M = [ + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] + ]; + + # $M = [ + # [0, 1,-2, 0, 1], + # [0, 0, 0, 1, 3], + # [0, 0, 0, 0, 0], + # [0, 0, 0, 0, 0] + # ]; + + # $M = [ + # [0, 1,-2, 0, 1], + # [0, 0, 0, 0, 0], + # [0, 0, 0, 1, 3], + # [0, 0, 0, 0, 0] + # ]; + + # $M = [ + # [1, 0, 0, 4], + # [0, 1, 0, 7], + # [0, 0, 1,-1] + # ]; + my $ok = True; + my @zero-rows; + my @leadings; + for 0 ..^ $M.elems -> $row { + my $first-in-row = Nil; + + + for 0 ..^ $M[ $row ].elems -> $col { + next if $M[ $row ][ $col ] == 0; + next if $first-in-row; + + $first-in-row = $M[ $row ][ $col ] if ! $first-in-row; + + if ( $first-in-row == 1 ) { + # leading one + @leadings.push: { row => $row, col => $col }; + # if not other leadings, skip any check + next if @leadings.elems <= 1; + + $ok = False and last if ( @leadings[ * - 2 ]<col> >= $col && @leadings[ * - 2 ]<row> != ( $row - 1 ) ); + } + } + + if ! $first-in-row { + # the row was made by all zeros + @zero-rows.push: $row; + # if this is the first zero row, cannot check anything + next if @zero-rows.elems <= 1; + # this is not the first zero row, so the previous row must be also zero! + $ok = False if @zero-rows[ * - 2 ] != ( $row - 1 ); + } + elsif $first-in-row != 1 { + $ok = False; + } + + '0'.say and exit if ( ! $ok ); + } + + '1'.say; # here everything is fine +} |
