diff options
21 files changed, 374 insertions, 3 deletions
diff --git a/challenge-268/luca-ferrari/blog-1.txt b/challenge-268/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..bbeedaf24b --- /dev/null +++ b/challenge-268/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task1 diff --git a/challenge-268/luca-ferrari/blog-10.txt b/challenge-268/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..028f3c0f87 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge268.html#task2pljava diff --git a/challenge-268/luca-ferrari/blog-2.txt b/challenge-268/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e210a306c4 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task2 diff --git a/challenge-268/luca-ferrari/blog-3.txt b/challenge-268/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..ca333cad39 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task1plperl diff --git a/challenge-268/luca-ferrari/blog-4.txt b/challenge-268/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..52c5a57591 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task2plperl diff --git a/challenge-268/luca-ferrari/blog-5.txt b/challenge-268/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..e3b1c460e1 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task1plpgsql diff --git a/challenge-268/luca-ferrari/blog-6.txt b/challenge-268/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..dcfa71995e --- /dev/null +++ b/challenge-268/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task2plpgsql diff --git a/challenge-268/luca-ferrari/blog-7.txt b/challenge-268/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..ee790d8a27 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task1python diff --git a/challenge-268/luca-ferrari/blog-8.txt b/challenge-268/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..a732291431 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task2python diff --git a/challenge-268/luca-ferrari/blog-9.txt b/challenge-268/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..271f45d443 --- /dev/null +++ b/challenge-268/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/08/PerlWeeklyChallenge268.html#task1pljava diff --git a/challenge-268/luca-ferrari/pljava/pom.xml b/challenge-268/luca-ferrari/pljava/pom.xml index f010e9517a..47fefd2b32 100644 --- a/challenge-268/luca-ferrari/pljava/pom.xml +++ b/challenge-268/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC267 + PWC268 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 267 with package PWC267</name> - <description>Implementation of the tasks in PL/Java for PWC 267</description> + <name>Perl Weekly Challenge 268 with package PWC268</name> + <description>Implementation of the tasks in PL/Java for PWC 268</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-268/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-268/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..d9cb80b717 --- /dev/null +++ b/challenge-268/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,79 @@ + + + +package PWC268; + +/** + * PL/Java implementation for PWC 268 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> + * + * + * 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/PWC268-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC268-1.jar', 'PWC268', true ); + select sqlj.set_classpath( 'public', 'PWC268' ); + + select pwc268.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC268-1.jar', 'PWC268', 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 = "pwc268", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final Integer task1_pljava( int[] left, int[] right ) throws SQLException { + logger.log( Level.INFO, "Entering pwc268.task1_pljava" ); + + List<Integer> l = new LinkedList<Integer>(); + List<Integer> r = new LinkedList<Integer>(); + + for ( int i : left ) + l.add( i ); + for ( int i : right ) + r.add( i ); + + Collections.sort( l ); + Collections.sort( r ); + List<Integer> diffs = new LinkedList<Integer>(); + + for ( int i = 0; i < l.size(); i++ ) { + int current = l.get( i ) - r.get( i ); + if ( ! diffs.contains( current ) ) + diffs.add( current ); + } + + if ( diffs.size() == 1 ) + return diffs.get( 0 ); + else + return null; + + + } +} diff --git a/challenge-268/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-268/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..6e2a1e1fcc --- /dev/null +++ b/challenge-268/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,72 @@ + + + +package PWC268; + +/** + * PL/Java implementation for PWC 268 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> + * + * + * 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/PWC268-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC268-1.jar', 'PWC268', true ); + select sqlj.set_classpath( 'public', 'PWC268' ); + + select pwc268.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC268-1.jar', 'PWC268', 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 = "pwc268", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int[] task2_pljava( int[] nums ) throws SQLException { + logger.log( Level.INFO, "Entering pwc268.task2_pljava" ); + + List<Integer> sorted = new LinkedList<Integer>(); + for ( int i : nums ) + sorted.add( i ); + Collections.sort( nums ); + + int[] result = new int[ nums.length ]; + int index = 0; + + for ( int i = 0; i < sorted.size(); i++ ) { + int left = sorted.get( i ); + int right = sorted.get( ++i ); + + result[ index++ ] = ( left > right ? left : right ); + result[ index++ ] = ( left > right ? right : left ); + } + + return result; + } +} diff --git a/challenge-268/luca-ferrari/plperl/ch-1.plperl b/challenge-268/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..cc9f08c727 --- /dev/null +++ b/challenge-268/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 268 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +-- + +CREATE SCHEMA IF NOT EXISTS pwc268; + +CREATE OR REPLACE FUNCTION +pwc268.task1_plperl( int[], int[] ) +RETURNS int +AS $CODE$ + + my ( $left, $right ) = @_; + + die 'Array must have the same length!' if ( $left->@* != $right->@* ); + + my @diffs; + for my $index ( 0 .. $left->@* - 1 ) { + my $d = $left->@[ $index ] - $right->@[ $index ]; + push @diffs, $d if ( ! grep( { $_ == $d } @diffs ) ); + } + + return $diffs[ 0 ] if ( @diffs == 1 ); + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-268/luca-ferrari/plperl/ch-2.plperl b/challenge-268/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..4a5b9823c6 --- /dev/null +++ b/challenge-268/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,26 @@ +-- +-- Perl Weekly Challenge 268 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +-- + +CREATE SCHEMA IF NOT EXISTS pwc268; + +CREATE OR REPLACE FUNCTION +pwc268.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $nums ) = @_; + + $nums = [ sort $nums->@* ]; + my @sorted; + for my $index ( 0 .. $nums->@* - 1 ) { + next if $index % 2 != 0; + push @sorted, reverse sort( $nums->@[ $index ], $nums->@[ $index + 1 ] ); + } + + return [ @sorted ] ; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-268/luca-ferrari/plpgsql/ch-1.sql b/challenge-268/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..d812800d96 --- /dev/null +++ b/challenge-268/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 268 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +-- + +CREATE SCHEMA IF NOT EXISTS pwc268; + +CREATE OR REPLACE FUNCTION +pwc268.task1_plpgsql( l int[], r int[] ) +RETURNS int +AS $CODE$ + + WITH sorted_left AS ( SELECT v, row_number() OVER ( order by 1 ) as r FROM unnest( l ) v ) + , sorted_right AS ( SELECT v, row_number() OVER ( order by 1 ) as r FROM unnest( r ) v ) + , diffs AS ( + SELECT l.v - r.v as d + FROM sorted_left l, sorted_right r + WHERE l.r = r.r + ) + SELECT d + FROM diffs d + GROUP BY d + HAVING count(*) = ( SELECT array_length( l, 1 ) ); + + +$CODE$ +LANGUAGE sql; diff --git a/challenge-268/luca-ferrari/plpgsql/ch-2.sql b/challenge-268/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..503a60a2bf --- /dev/null +++ b/challenge-268/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 268 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +-- + +CREATE SCHEMA IF NOT EXISTS pwc268; + +CREATE OR REPLACE FUNCTION +pwc268.task2_plpgsql( nums int[] ) +RETURNS SETOF int +AS $CODE$ + SELECT pwc268.task2_plperl( nums ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-268/luca-ferrari/python/ch-1.py b/challenge-268/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..c977d2a464 --- /dev/null +++ b/challenge-268/luca-ferrari/python/ch-1.py @@ -0,0 +1,39 @@ +#!python + +# +# Perl Weekly Challenge 268 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + left = [] + right = [] + + left = list( map( int, args[ 0 : args.index( "|" ) ] ) ) + right = list( map( int, args[ args.index( "|" ) + 1 : ] ) ) + + left.sort() + right.sort() + + diffs = [] + for index in range( 0, len( left ) ): + d = left[ index ] - right[ index ] + if not d in diffs: + diffs.append( d ) + + if len( diffs ) == 1: + return diffs[ 0 ] + else: + return None + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-268/luca-ferrari/python/ch-2.py b/challenge-268/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..4a2c56a0d9 --- /dev/null +++ b/challenge-268/luca-ferrari/python/ch-2.py @@ -0,0 +1,35 @@ +#!python + +# +# Perl Weekly Challenge 268 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + sorted = list( map( int, args ) ) + sorted.sort() + + result = [] + + for i in range( 0, len( sorted ) ): + if i % 2 != 0: + continue + + left = sorted[ i ] + right = sorted[ i + 1 ] + + result.append( left if left > right else right ) + result.append( left if right > left else right ) + + return result + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-268/luca-ferrari/raku/ch-1.raku b/challenge-268/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..d5a996c853 --- /dev/null +++ b/challenge-268/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,22 @@ +#!raku + +# +# Perl Weekly Challenge 268 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +# + +sub MAIN( :@left, :@right where { @left.elems == @right.elems } ) { + my @sorted-left = @left.sort; + my @sorted-right = @right.sort; + + my @diffs; + for 0 ..^ @sorted-left.elems -> $i { + my $current = @sorted-left[ $i ] - @sorted-right[ $i ]; + @diffs.push: $current if ! @diffs.grep( $current ); + } + + @diffs[ 0 ].say and exit if @diffs.elems == 1; + 'Impossible'.say; +} diff --git a/challenge-268/luca-ferrari/raku/ch-2.raku b/challenge-268/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..983d2cf867 --- /dev/null +++ b/challenge-268/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 268 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-268> +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems && @nums.elems %% 2 } ) { + my @sorted; + for @nums.sort.rotor( 2 ) -> @couple { + @sorted.push: @couple.reverse.flat; + } + + @sorted.join( " " ).say; +} |
