diff options
21 files changed, 398 insertions, 0 deletions
diff --git a/challenge-321/luca-ferrari/blog-1.txt b/challenge-321/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..bde4bf5253 --- /dev/null +++ b/challenge-321/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task1 diff --git a/challenge-321/luca-ferrari/blog-10.txt b/challenge-321/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..9c2cf271d8 --- /dev/null +++ b/challenge-321/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task2pljava diff --git a/challenge-321/luca-ferrari/blog-2.txt b/challenge-321/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..56ec5bb8a3 --- /dev/null +++ b/challenge-321/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task2 diff --git a/challenge-321/luca-ferrari/blog-3.txt b/challenge-321/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..469cd50a34 --- /dev/null +++ b/challenge-321/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task1plperl diff --git a/challenge-321/luca-ferrari/blog-4.txt b/challenge-321/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..616fac65ff --- /dev/null +++ b/challenge-321/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task2plperl diff --git a/challenge-321/luca-ferrari/blog-5.txt b/challenge-321/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..6becac0c1e --- /dev/null +++ b/challenge-321/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task1plpgsql diff --git a/challenge-321/luca-ferrari/blog-6.txt b/challenge-321/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..cb1cfc29eb --- /dev/null +++ b/challenge-321/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task2plpgsql diff --git a/challenge-321/luca-ferrari/blog-7.txt b/challenge-321/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..97fb7046c1 --- /dev/null +++ b/challenge-321/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task1python diff --git a/challenge-321/luca-ferrari/blog-8.txt b/challenge-321/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..d3d2f82e1b --- /dev/null +++ b/challenge-321/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task2python diff --git a/challenge-321/luca-ferrari/blog-9.txt b/challenge-321/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..2f7b5a94fe --- /dev/null +++ b/challenge-321/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/12/PerlWeeklyChallenge321.html#task1pljava diff --git a/challenge-321/luca-ferrari/pljava/pom.xml b/challenge-321/luca-ferrari/pljava/pom.xml new file mode 100644 index 0000000000..3a9f67f2be --- /dev/null +++ b/challenge-321/luca-ferrari/pljava/pom.xml @@ -0,0 +1,72 @@ + +<project + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > + <modelVersion>4.0.0</modelVersion> + + <groupId>PWC</groupId> + <artifactId> + PWC321 + </artifactId> + <version> + 1 + </version> + + <name>Perl Weekly Challenge 321 with package PWC321</name> + <description>Implementation of the tasks in PL/Java for PWC 321</description> + + <properties> + <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>pljava-api</artifactId> + <version>1.6.6</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.1</version> + <configuration> + <release>9</release> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>2.6</version> + <configuration> + <archive> + <manifest> + <!-- This identifies and version-stamps the jar. + Not essential, but easy and useful. --> + <addDefaultImplementationEntries> + true + </addDefaultImplementationEntries> + </manifest> + + <manifestSections> + <!-- This identifies a file in the jar named + pljava.ddr as an SQLJDeploymentDescriptor. --> + <manifestSection> + <name>pljava.ddr</name> + <manifestEntries> + <SQLJDeploymentDescriptor> + true + </SQLJDeploymentDescriptor> + </manifestEntries> + </manifestSection> + </manifestSections> + </archive> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/challenge-321/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-321/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..cc9a3a3aeb --- /dev/null +++ b/challenge-321/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,70 @@ + + + +package PWC321; + +/** + * PL/Java implementation for PWC 321 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> + * + * + * 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/PWC321-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC321-1.jar', 'PWC321', true ); + select sqlj.set_classpath( 'public', 'PWC321' ); + + select pwc321.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC321-1.jar', 'PWC321', 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 = "pwc321", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final Float task1_pljava( int[] numbers ) throws SQLException { + logger.log( Level.INFO, "Entering pwc321.task1_pljava" ); + + if ( numbers.length % 2 != 0 ) + throw new SQLException( "Must be even in size" ); + + Float min_average = null; + + List<Integer> sorted = IntStream.of( numbers ).boxed().collect( Collectors.toList() ); + Collections.sort( sorted ); + + for ( int i = 0; i < sorted.size(); i++ ) { + float current = ( sorted.get( i ) + sorted.get( sorted.size() - i - 1 ) ) / (float) 2; + if ( min_average == null || current < min_average ) + min_average = current; + } + + return min_average; + } +} diff --git a/challenge-321/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-321/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..a324bf0ebd --- /dev/null +++ b/challenge-321/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,56 @@ + + + +package PWC321; + +/** + * PL/Java implementation for PWC 321 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> + * + * + * 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/PWC321-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC321-1.jar', 'PWC321', true ); + select sqlj.set_classpath( 'public', 'PWC321' ); + + select pwc321.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC321-1.jar', 'PWC321', 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 = "pwc321", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task2_pljava( String left, String right ) throws SQLException { + logger.log( Level.INFO, "Entering pwc321.task2_pljava" ); + + return left.replaceAll( ".[#]", "" ).equals( right.replaceAll( ".[#]", "" ) ); + } +} diff --git a/challenge-321/luca-ferrari/plperl/ch-1.plperl b/challenge-321/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..e0d463002f --- /dev/null +++ b/challenge-321/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 321 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +-- + +CREATE SCHEMA IF NOT EXISTS pwc321; + +CREATE OR REPLACE FUNCTION +pwc321.task1_plperl( int[] ) +RETURNS numeric +AS $CODE$ + + my ( $numbers ) = @_; + die "Must be even in size!" if ( $numbers->@* % 2 != 0 ); + + $numbers = [ sort $numbers->@* ]; + + my $min_average = undef; + + for ( 0 .. $numbers->@* - 1 ) { + my $current = ( $numbers->@[ $_ ] + $numbers->@[ $numbers->@* - $_ - 1 ] ) / 2; + $min_average = $current if ( ! $min_average || $min_average > $current ); + } + + return $min_average; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-321/luca-ferrari/plperl/ch-2.plperl b/challenge-321/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..1d0458ef97 --- /dev/null +++ b/challenge-321/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,22 @@ +-- +-- Perl Weekly Challenge 321 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +-- + +CREATE SCHEMA IF NOT EXISTS pwc321; + +CREATE OR REPLACE FUNCTION +pwc321.task2_plperl( text, text ) +RETURNS boolean +AS $CODE$ + + my ( $left, $right ) = @_; + + $left =~ s/ . [#]//xg; + $right =~ s/ . [#]//xg; + + return $left eq $right ? 1 : 0; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-321/luca-ferrari/plpgsql/ch-1.sql b/challenge-321/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..9de26c600c --- /dev/null +++ b/challenge-321/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,39 @@ +-- +-- Perl Weekly Challenge 321 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +-- + +CREATE SCHEMA IF NOT EXISTS pwc321; + +CREATE OR REPLACE FUNCTION +pwc321.task1_plpgsql( numbers int[] ) +RETURNS numeric +AS $CODE$ +DECLARE + i int; + result numeric; + current int[]; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS averages( v numeric, mx int, mn int ); + TRUNCATE averages; + + FOR i IN 0 .. array_length( numbers, 1 ) / 2 - 1 LOOP + + INSERT INTO averages( v, mx, mn ) + SELECT ( min( x::int ) + max( x::int ) ) / 2::numeric + , max( x )::int + , min( x )::int + + FROM unnest( numbers[ 1 + i : array_length( numbers, 1 ) - i ] ) x + ; + END LOOP; + + SELECT min( v ) + INTO result + FROM averages; + + RETURN result; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-321/luca-ferrari/plpgsql/ch-2.sql b/challenge-321/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..4ed1200c41 --- /dev/null +++ b/challenge-321/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 321 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +-- + +CREATE SCHEMA IF NOT EXISTS pwc321; + +CREATE OR REPLACE FUNCTION +pwc321.task2_plpgsql( l text, r text ) +RETURNS boolean +AS $CODE$ + SELECT regexp_replace( l, '.[#]', '', 'g' ) = regexp_replace( r, '.[#]', '', 'g' ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-321/luca-ferrari/python/ch-1.py b/challenge-321/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..7b45a26203 --- /dev/null +++ b/challenge-321/luca-ferrari/python/ch-1.py @@ -0,0 +1,28 @@ +#!python + +# +# Perl Weekly Challenge 321 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + numbers = list( sorted( list( map( int, args ) ) ) ) + min_average = None + + for i in range( 0, len( numbers ) ): + current = ( numbers[ i ] + numbers[ len( numbers ) - i - 1 ] ) / 2 + if min_average is None or current < min_average: + min_average = current + + return min_average + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-321/luca-ferrari/python/ch-2.py b/challenge-321/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..bc84da0ce9 --- /dev/null +++ b/challenge-321/luca-ferrari/python/ch-2.py @@ -0,0 +1,27 @@ +#!python + +# +# Perl Weekly Challenge 321 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +# + +import sys +import re + +# task implementation +# the return value will be printed +def task_2( args ): + left = args[ 0 ] + right = args[ 1 ] + + left = re.sub( '.[#]', '', left ) + right = re.sub( '.[#]', '', right ) + + return left == right + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-321/luca-ferrari/raku/ch-1.raku b/challenge-321/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..7b03f1ef52 --- /dev/null +++ b/challenge-321/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,18 @@ +#!raku + +# +# Perl Weekly Challenge 321 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +# + +sub MAIN( *@numbers where { @numbers.elems %% 2 && @numbers.elems == @numbers.grep( * ~~ Int ).elems } ) { + + my @averages; + for 0 ..^ @numbers.elems / 2 { + @averages.push: ( [+] @numbers.sort[ $_, * - 1 - $_ ] ) / 2; + } + + @averages.min.say; +} diff --git a/challenge-321/luca-ferrari/raku/ch-2.raku b/challenge-321/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..ed7cccea2e --- /dev/null +++ b/challenge-321/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 321 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-321> +# + +sub MAIN( Str $left, Str $right ) { + say $left.subst( / . <[#]> /, '', :g ) ~~ $right.subst( / . <[#]> /, '', :g ); +} |
