diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-05-19 11:30:43 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-05-19 12:13:13 +0200 |
| commit | 24698d3c3d5f0cf4ced64f9459b542494faa2db0 (patch) | |
| tree | 9389d7e716cfd23672a611b2ec451aeb6c62e053 | |
| parent | c06ff9fff370eb4e35e077aaa119b3ba5bd1997b (diff) | |
| download | perlweeklychallenge-club-24698d3c3d5f0cf4ced64f9459b542494faa2db0.tar.gz perlweeklychallenge-club-24698d3c3d5f0cf4ced64f9459b542494faa2db0.tar.bz2 perlweeklychallenge-club-24698d3c3d5f0cf4ced64f9459b542494faa2db0.zip | |
PWC 269
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, 400 insertions, 3 deletions
diff --git a/challenge-269/luca-ferrari/blog-1.txt b/challenge-269/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..d2896b72da --- /dev/null +++ b/challenge-269/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task1 diff --git a/challenge-269/luca-ferrari/blog-10.txt b/challenge-269/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..e6f54ebbc5 --- /dev/null +++ b/challenge-269/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge269.html#task2pljava diff --git a/challenge-269/luca-ferrari/blog-2.txt b/challenge-269/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..9b78fd6e0c --- /dev/null +++ b/challenge-269/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task2 diff --git a/challenge-269/luca-ferrari/blog-3.txt b/challenge-269/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..c5ef321557 --- /dev/null +++ b/challenge-269/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task1plperl diff --git a/challenge-269/luca-ferrari/blog-4.txt b/challenge-269/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..ca8acf5854 --- /dev/null +++ b/challenge-269/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task2plperl diff --git a/challenge-269/luca-ferrari/blog-5.txt b/challenge-269/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..d497eec296 --- /dev/null +++ b/challenge-269/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task1plpgsql diff --git a/challenge-269/luca-ferrari/blog-6.txt b/challenge-269/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..12c347990c --- /dev/null +++ b/challenge-269/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task2plpgsql diff --git a/challenge-269/luca-ferrari/blog-7.txt b/challenge-269/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..a3eea2d72a --- /dev/null +++ b/challenge-269/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task1python diff --git a/challenge-269/luca-ferrari/blog-8.txt b/challenge-269/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..6618a8dc00 --- /dev/null +++ b/challenge-269/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task2python diff --git a/challenge-269/luca-ferrari/blog-9.txt b/challenge-269/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..7411233c70 --- /dev/null +++ b/challenge-269/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/19/PerlWeeklyChallenge269.html#task1pljava diff --git a/challenge-269/luca-ferrari/pljava/pom.xml b/challenge-269/luca-ferrari/pljava/pom.xml index 47fefd2b32..b5fe1e2ab4 100644 --- a/challenge-269/luca-ferrari/pljava/pom.xml +++ b/challenge-269/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC268 + PWC269 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 268 with package PWC268</name> - <description>Implementation of the tasks in PL/Java for PWC 268</description> + <name>Perl Weekly Challenge 269 with package PWC269</name> + <description>Implementation of the tasks in PL/Java for PWC 269</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-269/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-269/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..7b70bcd7d6 --- /dev/null +++ b/challenge-269/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,70 @@ + + + +package PWC269; + +/** + * PL/Java implementation for PWC 269 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> + * + * + * 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/PWC269-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC269-1.jar', 'PWC269', true ); + select sqlj.set_classpath( 'public', 'PWC269' ); + + select pwc269.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC269-1.jar', 'PWC269', 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 = "pwc269", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int[] task1_pljava( int[] nums ) throws SQLException { + + List<Integer> couples = new LinkedList<Integer>(); + + for ( int i = 0; i < nums.length; i++ ) + for ( int j = i + 1; j < nums.length; j++ ) + if ( ( nums[ i ] | nums[ j ] ) % 2 == 0 ) { + couples.add( nums[ i ] ); + couples.add( nums[ j ] ); + } + + int result[] = new int[ couples.size() ]; + int i = 0; + for ( int x : couples ) { + result[ i++ ] = x; + } + + return result; + } +} diff --git a/challenge-269/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-269/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..466fcaab5a --- /dev/null +++ b/challenge-269/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,74 @@ + + + +package PWC269; + +/** + * PL/Java implementation for PWC 269 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> + * + * + * 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/PWC269-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC269-1.jar', 'PWC269', true ); + select sqlj.set_classpath( 'public', 'PWC269' ); + + select pwc269.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC269-1.jar', 'PWC269', 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 = "pwc269", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int[] task2_pljava( int[] nums ) throws SQLException { + List<Integer> left = new LinkedList<Integer>(); + List<Integer> right = new LinkedList<Integer>(); + + left.add( nums[ 0 ] ); + right.add( nums[ 1 ] ); + + for ( int i = 2; i < nums.length; i++ ) { + if ( left.get( left.size() - 1 ) > right.get( right.size() - 1 ) ) + left.add( nums[ i++ ] ); + else + right.add( nums[ i++ ] ); + } + + int result[] = new int[ left.size() + right.size() ]; + int i = 0; + for ( int x : left ) + result[ i++ ] = x; + for ( int x : right ) + result[ i++ ] = x; + + return result; + } +} diff --git a/challenge-269/luca-ferrari/plperl/ch-1.plperl b/challenge-269/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..7314573d5f --- /dev/null +++ b/challenge-269/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 269 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +-- + +CREATE SCHEMA IF NOT EXISTS pwc269; + +CREATE OR REPLACE FUNCTION +pwc269.task1_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $nums ) = @_; + + for my $i ( 0 .. $nums->@* - 1 ) { + for my $j ( $i + 1 .. $nums->@* -1 ) { + my $result = $nums->@[ $i ] | $nums->@[ $j ]; + if ( $result % 2 == 0 ) { + return_next( $nums->@[ $i ] ); + return_next( $nums->@[ $j ] ); + } + } + } + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-269/luca-ferrari/plperl/ch-2.plperl b/challenge-269/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..5fecb23eb8 --- /dev/null +++ b/challenge-269/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,36 @@ +-- +-- Perl Weekly Challenge 269 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +-- + +CREATE SCHEMA IF NOT EXISTS pwc269; + +CREATE OR REPLACE FUNCTION +pwc269.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $nums ) = @_; + + my ( @array1, @array2 ); + my @nums = $nums->@*; + + push @array1, shift @nums; + push @array2, shift @nums; + + while ( @nums ) { + + if ( $array1[ $#array1 ] > $array2[ $#array2 ] ) { + push @array1, shift @nums; + } + else { + push @array2, shift @nums; + } + } + + return_next( $_ ) for ( @array1, @array2 ); + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-269/luca-ferrari/plpgsql/ch-1.sql b/challenge-269/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..8288b74040 --- /dev/null +++ b/challenge-269/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,22 @@ +-- +-- Perl Weekly Challenge 269 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +-- + +CREATE SCHEMA IF NOT EXISTS pwc269; + +CREATE OR REPLACE FUNCTION +pwc269.task1_plpgsql( nums int[] ) +RETURNS SETOF int +AS $CODE$ + + SELECT a, b + FROM unnest( nums ) a + , unnest( nums ) b + WHERE + a <> b + AND + mod( a | b, 2 ) = 0; +$CODE$ +LANGUAGE sql; diff --git a/challenge-269/luca-ferrari/plpgsql/ch-2.sql b/challenge-269/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..99a34277f1 --- /dev/null +++ b/challenge-269/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,41 @@ +-- +-- Perl Weekly Challenge 269 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +-- + +CREATE SCHEMA IF NOT EXISTS pwc269; + +CREATE OR REPLACE FUNCTION +pwc269.task2_plpgsql( nums int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + array1 int[]; + array2 int[]; + i int; +BEGIN + + i := 1; + array1 := array_append( array1, nums[ 1 ] ); + array2 := array_append( array2, nums[ 2 ] ); + i := i + 2; + + WHILE i < array_length( nums, 1 ) LOOP + IF array1[ i - 2 ] > array2[ i - 1 ] THEN + array1 := array_append( array1, nums[ i ] ); + ELSE + array2 := array_append( array2, nums[ i ] ); + END IF; + + I := I + 2; + END LOOP; + + RETURN QUERY SELECT v + FROM unnest( array1 ) v + UNION + SELECT v + FROM unnest( array2 ) v; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-269/luca-ferrari/python/ch-1.py b/challenge-269/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..b00b23f6d0 --- /dev/null +++ b/challenge-269/luca-ferrari/python/ch-1.py @@ -0,0 +1,29 @@ +#!python + +# +# Perl Weekly Challenge 269 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + nums = list( map( int, args ) ) + couples = [] + + for i in range( 0, len( nums ) ): + for j in range ( i + 1, len( nums ) ): + if ( nums[ i ] | nums[ j ] ) % 2 == 0 : + couples.append( nums[ i ] ) + couples.append( nums[ j ] ) + + return couples + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-269/luca-ferrari/python/ch-2.py b/challenge-269/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..27f1ef0ce5 --- /dev/null +++ b/challenge-269/luca-ferrari/python/ch-2.py @@ -0,0 +1,36 @@ +#!python + +# +# Perl Weekly Challenge 269 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + nums = list( map( int, args ) ) + + left = [] + right = [] + + left.append( nums[ 0 ] ) + right.append( nums[ 1 ] ) + + i = 2 + while i < len( nums ): + if left[ len( left ) - 1 ] > right[ len( right ) - 1 ] : + left.append( nums[ i ] ) + else: + right.append( nums[ i ] ) + i += 1 + + return ( left, right ) + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-269/luca-ferrari/raku/ch-1.raku b/challenge-269/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..9e4ed941c1 --- /dev/null +++ b/challenge-269/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 269 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( { $_ ~~ Int && $_ > 0 } ).elems } ) { + my @couples; + for 0 ..^ @nums.elems -> $i { + for $i ^..^ @nums.elems -> $j { + @couples.push: [ @nums[ $i ], @nums[ $j ] ] if ( ( @nums[ $i ] +| @nums[ $j ] ) %% 2 ); + } + } + + @couples.join( "\n" ).say; +} diff --git a/challenge-269/luca-ferrari/raku/ch-2.raku b/challenge-269/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..c64159a67f --- /dev/null +++ b/challenge-269/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,32 @@ +#!raku + +# +# Perl Weekly Challenge 269 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-269> +# + +sub MAIN( *@nums + where { @nums.elems == @nums.grep( { $_ ~~ Int && $_ > 0 } ).elems + && @nums.elems > 2 } ) { + + my @array1; + my @array2; + + @array1.push: @nums.shift; + @array2.push: @nums.shift; + + while ( @nums ) { + if ( @array1[ * - 1 ] > @array2[ * - 1 ] ) { + @array1.push: @nums.shift; + } + else { + @array2.push: @nums.shift; + } + } + + + ( @array1, @array2 ).flat.join( ', ' ).say; + +} |
