diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2025-06-12 12:39:57 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2025-06-12 14:36:24 +0200 |
| commit | d683f6d8d28d1a5ccb05bc47053cb0067151278a (patch) | |
| tree | 1f96375dc2c036dcb224f44a04f13bf8cb9070df | |
| parent | 2b3330bc7c40f557fdacbf78e77d940d03ee6fda (diff) | |
| download | perlweeklychallenge-club-d683f6d8d28d1a5ccb05bc47053cb0067151278a.tar.gz perlweeklychallenge-club-d683f6d8d28d1a5ccb05bc47053cb0067151278a.tar.bz2 perlweeklychallenge-club-d683f6d8d28d1a5ccb05bc47053cb0067151278a.zip | |
PWC 325
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, 354 insertions, 3 deletions
diff --git a/challenge-325/luca-ferrari/blog-1.txt b/challenge-325/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..c20d65fa3d --- /dev/null +++ b/challenge-325/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task1 diff --git a/challenge-325/luca-ferrari/blog-10.txt b/challenge-325/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..db3b462ae7 --- /dev/null +++ b/challenge-325/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task2pljava diff --git a/challenge-325/luca-ferrari/blog-2.txt b/challenge-325/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..61a02e6458 --- /dev/null +++ b/challenge-325/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task2 diff --git a/challenge-325/luca-ferrari/blog-3.txt b/challenge-325/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..111304835e --- /dev/null +++ b/challenge-325/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task1plperl diff --git a/challenge-325/luca-ferrari/blog-4.txt b/challenge-325/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..f88e51c990 --- /dev/null +++ b/challenge-325/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task2plperl diff --git a/challenge-325/luca-ferrari/blog-5.txt b/challenge-325/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..666c542235 --- /dev/null +++ b/challenge-325/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task1plpgsql diff --git a/challenge-325/luca-ferrari/blog-6.txt b/challenge-325/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..bb057a55a9 --- /dev/null +++ b/challenge-325/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task2plpgsql diff --git a/challenge-325/luca-ferrari/blog-7.txt b/challenge-325/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..718aec7c41 --- /dev/null +++ b/challenge-325/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task1python diff --git a/challenge-325/luca-ferrari/blog-8.txt b/challenge-325/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..f1a3cea6d9 --- /dev/null +++ b/challenge-325/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task2python diff --git a/challenge-325/luca-ferrari/blog-9.txt b/challenge-325/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..219e8fae7c --- /dev/null +++ b/challenge-325/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/06/12/PerlWeeklyChallenge325.html#task1pljava diff --git a/challenge-325/luca-ferrari/pljava/pom.xml b/challenge-325/luca-ferrari/pljava/pom.xml index 6603367ccb..25362680ec 100644 --- a/challenge-325/luca-ferrari/pljava/pom.xml +++ b/challenge-325/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC323 + PWC325 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 323 with package PWC323</name> - <description>Implementation of the tasks in PL/Java for PWC 323</description> + <name>Perl Weekly Challenge 325 with package PWC325</name> + <description>Implementation of the tasks in PL/Java for PWC 325</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-325/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-325/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..2cb8924dda --- /dev/null +++ b/challenge-325/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,71 @@ + + + +package PWC325; + +/** + * PL/Java implementation for PWC 325 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> + * + * + * 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/PWC325-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', true ); + select sqlj.set_classpath( 'public', 'PWC325' ); + + select pwc325.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', 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 = "pwc325", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( String bits ) throws SQLException { + logger.log( Level.INFO, "Entering pwc325.task1_pljava" ); + + int max_ones = 0; + int current_max = 0; + + for ( String d : bits.split( "" ) ) { + if ( d.equals( "1" ) ) + current_max++; + else { + if ( current_max > max_ones ) + max_ones = current_max; + + current_max = 0; + + } + } + + return max_ones; + } +} diff --git a/challenge-325/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-325/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..aa4fdedeee --- /dev/null +++ b/challenge-325/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,74 @@ + + + +package PWC325; + +/** + * PL/Java implementation for PWC 325 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> + * + * + * 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/PWC325-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', true ); + select sqlj.set_classpath( 'public', 'PWC325' ); + + select pwc325.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC325-1.jar', 'PWC325', 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 = "pwc325", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int[] task2_pljava( int[] prices ) throws SQLException { + logger.log( Level.INFO, "Entering pwc325.task2_pljava" ); + + List<Integer> new_prices = new LinkedList<Integer>(); + + for ( int i = 0; i < prices.length; i++ ) { + int current = prices[ i ]; + + for ( int j = i + 1; j < prices.length; j++ ) + if ( prices[ j ] < prices[ i ] ) { + current -= prices[ j ]; + break; + } + + new_prices.add( current ); + } + + int result[] = new int[ new_prices.size() ]; + for ( int i = 0; i < new_prices.size(); i++ ) + result[ i ] = new_prices.get( i ); + + return result; + } +} diff --git a/challenge-325/luca-ferrari/plperl/ch-1.plperl b/challenge-325/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..462f7eb470 --- /dev/null +++ b/challenge-325/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,21 @@ +-- +-- Perl Weekly Challenge 325 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +-- + +CREATE SCHEMA IF NOT EXISTS pwc325; + +CREATE OR REPLACE FUNCTION +pwc325.task1_plperl( text ) +RETURNS int +AS $CODE$ + + my ( $bits ) = @_; + + my @found = ( $bits =~ / 1+ /xg ); + return ( sort + map { length( $_ ) } @found )[ -1 ]; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-325/luca-ferrari/plperl/ch-2.plperl b/challenge-325/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..b3f331c487 --- /dev/null +++ b/challenge-325/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,26 @@ +-- +-- Perl Weekly Challenge 325 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +-- + +CREATE SCHEMA IF NOT EXISTS pwc325; + +CREATE OR REPLACE FUNCTION +pwc325.task2_plperl( int[] ) +RETURNS int[] +AS $CODE$ + + my ( $prices ) = @_; + my @final_prices; + + for my $index ( 0 .. $prices->@* - 1 ) { + push @final_prices, $prices->@[ $index ] + - ( ( grep { $_ < $prices->@[ $index ] } $prices->@[ $index + 1 .. $prices->@* - 1 ] )[ 0 ] // 0 ); + + } + + return [ @final_prices ]; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-325/luca-ferrari/plpgsql/ch-1.sql b/challenge-325/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..1402490e39 --- /dev/null +++ b/challenge-325/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 325 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +-- + +CREATE SCHEMA IF NOT EXISTS pwc325; + +CREATE OR REPLACE FUNCTION +pwc325.task1_plpgsql( b text ) +RETURNS int +AS $CODE$ + SELECT max( length( v::text ) - 2 ) + FROM regexp_matches( b, '1+', 'g' ) v; +$CODE$ +LANGUAGE sql; diff --git a/challenge-325/luca-ferrari/plpgsql/ch-2.sql b/challenge-325/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..f97002220b --- /dev/null +++ b/challenge-325/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,35 @@ +-- +-- Perl Weekly Challenge 325 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +-- + +CREATE SCHEMA IF NOT EXISTS pwc325; + +CREATE OR REPLACE FUNCTION +pwc325.task2_plpgsql( prices int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current_price int; +BEGIN + FOR i IN 1 .. array_length( prices, 1 ) LOOP + SELECT prices[ i ] - + ( SELECT v + FROM unnest( prices[ i + 1 : array_length( prices, 1 ) - 1 ] ) v + WHERE v::int < prices[ i ] + LIMIT 1 + ) + INTO current_price; + + IF NOT FOUND THEN + RETURN NEXT prices[ i ]; + ELSE + RETURN NEXT current_price; + END IF; + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-325/luca-ferrari/python/ch-1.py b/challenge-325/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..3e3210e9c0 --- /dev/null +++ b/challenge-325/luca-ferrari/python/ch-1.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 325 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + bits = list( map( int, args ) ) + f_max = 0 + c_max = 0 + + for d in bits: + if d == 1: + c_max += 1 + else: + if c_max > f_max : + f_max = c_max + + c_max = 0 + + return f_max + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-325/luca-ferrari/python/ch-2.py b/challenge-325/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..297da2851e --- /dev/null +++ b/challenge-325/luca-ferrari/python/ch-2.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 325 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + prices = list( map( int, args ) ) + new_prices = [] + + for i in range( 0, len( prices ) ) : + current = prices[ i ] + next_min = list( filter( lambda x: x < current, prices[ i + 1 : ] ) ) + if len( next_min ) > 0 : + next_min = next_min[ 0 ] + else: + next_min = 0 + + new_prices.append( current - next_min ) + + return new_prices + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-325/luca-ferrari/raku/ch-1.raku b/challenge-325/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..709f11eaa8 --- /dev/null +++ b/challenge-325/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!raku + +# +# Perl Weekly Challenge 325 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +# + +sub MAIN( *@bits where { @bits.grep( * ~~ / ^ <[01]>+ $ / ).elems == @bits.elems } ) { + + ( @bits.join ~~ m:g/ 1+ / ).map( *.chars ).max.say; +} diff --git a/challenge-325/luca-ferrari/raku/ch-2.raku b/challenge-325/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..6604923f46 --- /dev/null +++ b/challenge-325/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 325 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-325> +# + +sub MAIN( *@prices where { @prices.grep( * ~~ Int ).elems == @prices.elems } ) { + my @final-prices; + for 0 ..^ @prices.elems -> $index { + @final-prices.push: @prices[ $index ] - ( @prices[ $index + 1 .. * ].grep( * < @prices[ $index ] )[ 0 ] // 0 ); + } + + @final-prices.join( ', ' ).say; +} |
