diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2025-08-04 13:47:31 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2025-08-04 14:44:03 +0200 |
| commit | 0977e205540490c43e668474daf99abb688aa6f2 (patch) | |
| tree | 09d1faa953fe10c6f836582c2f5b4bbf0135e114 | |
| parent | ce2f933a023e15e5dac73508e56a9aec0e87fae6 (diff) | |
| download | perlweeklychallenge-club-0977e205540490c43e668474daf99abb688aa6f2.tar.gz perlweeklychallenge-club-0977e205540490c43e668474daf99abb688aa6f2.tar.bz2 perlweeklychallenge-club-0977e205540490c43e668474daf99abb688aa6f2.zip | |
PWC 333
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 PL/Java done
Task 2 PL/Java done
Task 1 Python done
Task 2 Python done
21 files changed, 418 insertions, 3 deletions
diff --git a/challenge-333/luca-ferrari/blog-1.txt b/challenge-333/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..7155cf96d0 --- /dev/null +++ b/challenge-333/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task1 diff --git a/challenge-333/luca-ferrari/blog-10.txt b/challenge-333/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..59692f75f9 --- /dev/null +++ b/challenge-333/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task2pljava diff --git a/challenge-333/luca-ferrari/blog-2.txt b/challenge-333/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..4a82d9c039 --- /dev/null +++ b/challenge-333/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task2 diff --git a/challenge-333/luca-ferrari/blog-3.txt b/challenge-333/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..7444d1e87b --- /dev/null +++ b/challenge-333/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task1plperl diff --git a/challenge-333/luca-ferrari/blog-4.txt b/challenge-333/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..8a57af40be --- /dev/null +++ b/challenge-333/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task2plperl diff --git a/challenge-333/luca-ferrari/blog-5.txt b/challenge-333/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..88a90a7a7c --- /dev/null +++ b/challenge-333/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task1plpgsql diff --git a/challenge-333/luca-ferrari/blog-6.txt b/challenge-333/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..a1b18e5556 --- /dev/null +++ b/challenge-333/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task2plpgsql diff --git a/challenge-333/luca-ferrari/blog-7.txt b/challenge-333/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..9e0833b297 --- /dev/null +++ b/challenge-333/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task1python diff --git a/challenge-333/luca-ferrari/blog-8.txt b/challenge-333/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..69f1af3edc --- /dev/null +++ b/challenge-333/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task2python diff --git a/challenge-333/luca-ferrari/blog-9.txt b/challenge-333/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..2c86f0057b --- /dev/null +++ b/challenge-333/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/08/04/PerlWeeklyChallenge333.html#task1pljava diff --git a/challenge-333/luca-ferrari/pljava/pom.xml b/challenge-333/luca-ferrari/pljava/pom.xml index 677727f42f..353e1821da 100644 --- a/challenge-333/luca-ferrari/pljava/pom.xml +++ b/challenge-333/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC332 + PWC333 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 332 with package PWC332</name> - <description>Implementation of the tasks in PL/Java for PWC 332</description> + <name>Perl Weekly Challenge 333 with package PWC333</name> + <description>Implementation of the tasks in PL/Java for PWC 333</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-333/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-333/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..350a46cf82 --- /dev/null +++ b/challenge-333/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,76 @@ + + + +package PWC333; + +/** + * PL/Java implementation for PWC 333 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> + * + * + * 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/PWC333-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC333-1.jar', 'PWC333', true ); + select sqlj.set_classpath( 'public', 'PWC333' ); + + select pwc333.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC333-1.jar', 'PWC333', 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 java.util.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc333", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task1_pljava( int coordinates[] ) throws SQLException { + logger.log( Level.INFO, "Entering pwc333.task1_pljava" ); + + List<Map<String, Integer>> coords = new LinkedList<Map<String, Integer>>(); + + for ( int i = 0; i < coordinates.length - 1; i++ ) { + Map<String, Integer> current = new HashMap<String, Integer>(); + current.put( "x", coordinates[ i++ ] ); + current.put( "y", coordinates[ i ] ); + coords.add( current ); + } + + Map<String, Integer> first = coords.get( 0 ); + Map<String, Integer> second = coords.get( 1 ); + + for ( int i = 2; i < coords.size(); i++ ) { + Map<String, Integer> current = coords.get( i ); + + if ( ( current.get( "x" ) - first.get( "x" ) ) * ( first.get( "y" ) - second.get( "y" ) ) + != ( current.get( "y" ) - first.get( "y" ) ) * ( first.get( "x" ) - second.get( "x" ) ) ) + return false; + } + + return true; + } +} diff --git a/challenge-333/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-333/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..06365ac6d0 --- /dev/null +++ b/challenge-333/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,76 @@ + + + +package PWC333; + +/** + * PL/Java implementation for PWC 333 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> + * + * + * 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/PWC333-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC333-1.jar', 'PWC333', true ); + select sqlj.set_classpath( 'public', 'PWC333' ); + + select pwc333.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC333-1.jar', 'PWC333', 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 java.util.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc333", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int[] task2_pljava( int[] numbers ) throws SQLException { + logger.log( Level.INFO, "Entering pwc333.task2_pljava" ); + + List<Integer> result = new LinkedList<Integer>(); + int limit = numbers.length; + + for ( int i = 0; i < numbers.length && limit > 0; i++ ) { + result.add( numbers[ i ] ); + limit--; + if ( limit == 0 ) + break; + + if ( numbers[ i ] == 0 ) { + result.add( numbers[ i ] ); + limit--; + } + } + + int[] tmp = new int[ result.size() ]; + for ( int i = 0; i < tmp.length; i++ ) + tmp[ i ] = result.get( i ); + + return tmp; + + } +} diff --git a/challenge-333/luca-ferrari/plperl/ch-1.plperl b/challenge-333/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..9323503e63 --- /dev/null +++ b/challenge-333/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,36 @@ +-- +-- Perl Weekly Challenge 333 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +-- + +CREATE SCHEMA IF NOT EXISTS pwc333; + +CREATE OR REPLACE FUNCTION +pwc333.task1_plperl( int[] ) +RETURNS boolean +AS $CODE$ + + my ( $coordinates ) = @_; + die "Wrong arguments!" if ( $coordinates->@* % 2 != 0 ); + + my @coords; + while ( $coordinates->@* ) { + push @coords, { x => shift( $coordinates->@* ), + y => shift( $coordinates->@* ) }; + } + + my $first = $coords[ 0 ]; + my $second = $coords[ 1 ]; + + for my $i ( 2 .. $#coords ) { + my $current = $coords[ $i ]; + return 0 if ( ( $current->{ x } - $first->{ x } ) * ( $first->{ y } - $second->{ y } ) + != ( $current->{ y } - $first->{ y } ) * ( $first->{ x } - $second->{ x } ) ); + + } + + return 1; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-333/luca-ferrari/plperl/ch-2.plperl b/challenge-333/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..41156b16d4 --- /dev/null +++ b/challenge-333/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,32 @@ +-- +-- Perl Weekly Challenge 333 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +-- + +CREATE SCHEMA IF NOT EXISTS pwc333; + +CREATE OR REPLACE FUNCTION +pwc333.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $numbers ) = @_; + + my $limit = scalar( $numbers->@* ); + my $index = 0; + + while ( $limit ) { + my $current = $numbers->@[ $index++ ]; + return_next( $current ); + $limit--; + last if ( ! $limit ); + return_next( $current ) if ( ! $current ); + $limit-- if ( ! $current ); + + } + + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-333/luca-ferrari/plpgsql/ch-1.sql b/challenge-333/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..26ca767eba --- /dev/null +++ b/challenge-333/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 333 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +-- + +CREATE SCHEMA IF NOT EXISTS pwc333; + +CREATE OR REPLACE FUNCTION +pwc333.task1_plpgsql( c int[] ) +RETURNS boolean +AS $CODE$ + SELECT pwc333.task1_plperl( c ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-333/luca-ferrari/plpgsql/ch-2.sql b/challenge-333/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..363466f824 --- /dev/null +++ b/challenge-333/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,42 @@ +-- +-- Perl Weekly Challenge 333 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +-- + +CREATE SCHEMA IF NOT EXISTS pwc333; + +CREATE OR REPLACE FUNCTION +pwc333.task2_plpgsql( n int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + v int; + l int; +BEGIN + + l := array_length( n, 1 ); + + FOREACH v IN ARRAY n LOOP + IF l = 0 THEN + RETURN; + END IF; + + + RETURN NEXT v; + l := l - 1; + + IF l = 0 THEN + RETURN; + END IF; + + IF v = 0 THEN + RETURN NEXT v; + l := l - 1; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-333/luca-ferrari/python/ch-1.py b/challenge-333/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..7f647fdbec --- /dev/null +++ b/challenge-333/luca-ferrari/python/ch-1.py @@ -0,0 +1,44 @@ +#!python + +# +# Perl Weekly Challenge 333 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + coords = [] + for i in range( 0, len( args ) - 1 ): + if i % 2 != 0: + continue + + x = int( args[ i ] ) + y = int( args[ i + 1 ] ) + + current = {} + current[ 'x' ] = x + current[ 'y' ] = y + + coords.append( current ) + + first = coords[ 0 ] + second = coords[ 1 ] + + + for i in range( 2, len( coords ) ): + current = coords[ i ] + if ( ( current[ 'x' ] - first[ 'x' ] ) * ( first[ 'y' ] - second[ 'y' ] ) ) != ( ( current[ 'y' ] - second[ 'y' ] ) * ( first[ 'x' ] - second[ 'x' ] ) ): + return False + + return True + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-333/luca-ferrari/python/ch-2.py b/challenge-333/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..0d36552024 --- /dev/null +++ b/challenge-333/luca-ferrari/python/ch-2.py @@ -0,0 +1,37 @@ +#!python + +# +# Perl Weekly Challenge 333 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + result = [] + limit = len( args ) + index = 0 + + while limit > 0: + current = int( args[ index ] ) + index = index + 1 + result.append( current ) + limit = limit - 1 + if limit == 0: + break + + if current == 0: + result.append( current ) + limit = limit - 1 + if limit == 0: + break + + return result + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-333/luca-ferrari/raku/ch-1.raku b/challenge-333/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..f750a010ed --- /dev/null +++ b/challenge-333/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!raku + +# +# Perl Weekly Challenge 333 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +# + +sub MAIN( *@coordinates + where { @coordinates.grep( * ~~ Int ).elems == @coordinates.elems && @coordinates.elems %% 2 } ) { + + my @coords; + for @coordinates -> $x, $y { + @coords.push: { x => $x, y => $y }; + } + + my $first = @coords[ 0 ]; + my $second = @coords[ 1 ]; + + for 2 ..^ @coords.elems { + my $current = @coords[ $_ ]; + 'False'.say and exit if ( ( $current<x> - $first<x> ) * ( $first<y> - $second<y> ) + != ( $current<y> - $first<y> ) * ( $first<x> - $second<x> ) ); + } + + 'True'.say; +} diff --git a/challenge-333/luca-ferrari/raku/ch-2.raku b/challenge-333/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..e57937aa1b --- /dev/null +++ b/challenge-333/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 333 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-333> +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems } ) { + my @result; + + for @nums { + @result.push: $_; + @result.push: $_ if ( $_ == 0 ); + } + + @result[ 0 .. @nums.elems - 1 ].join( ', ' ).say; +} |
