diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-24 14:46:03 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-24 14:46:03 +0000 |
| commit | b64b97c214120ddd79c9d1fced238e971424a913 (patch) | |
| tree | db442690646ba3b3ffca883b2107dec3c05f6ffa | |
| parent | 81e30b5d200ca4f8aa09d9e4b838b8b35a5d874f (diff) | |
| parent | 11f26422344d614fe26cf1eabbc601d9f9c00f20 (diff) | |
| download | perlweeklychallenge-club-b64b97c214120ddd79c9d1fced238e971424a913.tar.gz perlweeklychallenge-club-b64b97c214120ddd79c9d1fced238e971424a913.tar.bz2 perlweeklychallenge-club-b64b97c214120ddd79c9d1fced238e971424a913.zip | |
Merge pull request #13082 from fluca1978/PWC349
PWC 349
21 files changed, 386 insertions, 3 deletions
diff --git a/challenge-349/luca-ferrari/blog-1.txt b/challenge-349/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..b8800ab290 --- /dev/null +++ b/challenge-349/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task1 diff --git a/challenge-349/luca-ferrari/blog-10.txt b/challenge-349/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..69716663c6 --- /dev/null +++ b/challenge-349/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task2pljava diff --git a/challenge-349/luca-ferrari/blog-2.txt b/challenge-349/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..4dac3a7cdd --- /dev/null +++ b/challenge-349/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task2 diff --git a/challenge-349/luca-ferrari/blog-3.txt b/challenge-349/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..de2c31f1c2 --- /dev/null +++ b/challenge-349/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task1plperl diff --git a/challenge-349/luca-ferrari/blog-4.txt b/challenge-349/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..bee7036be4 --- /dev/null +++ b/challenge-349/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task2plperl diff --git a/challenge-349/luca-ferrari/blog-5.txt b/challenge-349/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..3e5b4296da --- /dev/null +++ b/challenge-349/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task1plpgsql diff --git a/challenge-349/luca-ferrari/blog-6.txt b/challenge-349/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..b08a538e1d --- /dev/null +++ b/challenge-349/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task2plpgsql diff --git a/challenge-349/luca-ferrari/blog-7.txt b/challenge-349/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..6d085333ef --- /dev/null +++ b/challenge-349/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task1python diff --git a/challenge-349/luca-ferrari/blog-8.txt b/challenge-349/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..efaf3b4dbb --- /dev/null +++ b/challenge-349/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task2python diff --git a/challenge-349/luca-ferrari/blog-9.txt b/challenge-349/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..e5b181f498 --- /dev/null +++ b/challenge-349/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/11/24/PerlWeeklyChallenge349.html#task1pljava diff --git a/challenge-349/luca-ferrari/pljava/pom.xml b/challenge-349/luca-ferrari/pljava/pom.xml index d1e54ecdf9..a775e4d8f3 100644 --- a/challenge-349/luca-ferrari/pljava/pom.xml +++ b/challenge-349/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC348 + PWC349 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 348 with package PWC348</name> - <description>Implementation of the tasks in PL/Java for PWC 348</description> + <name>Perl Weekly Challenge 349 with package PWC349</name> + <description>Implementation of the tasks in PL/Java for PWC 349</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-349/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-349/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..bce37b3884 --- /dev/null +++ b/challenge-349/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,78 @@ + + + +package PWC349; + +/** + * PL/Java implementation for PWC 349 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> + * + * + * 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/PWC349-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC349-1.jar', 'PWC349', true ); + select sqlj.set_classpath( 'public', 'PWC349' ); + + select pwc349.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC349-1.jar', 'PWC349', 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 = "pwc349", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( String string ) throws SQLException { + logger.log( Level.INFO, "Entering pwc349.task1_pljava" ); + + int result = 0; + String previous = null; + int max = 0; + + for ( String current : string.split( "" ) ) { + if ( previous == null || ! previous.equals( current ) ) { + previous = current; + + if ( result > max ) + max = result; + + result = 0; + } + + + result++; + } + + if ( result > max ) + max = result; + + + return max; + } +} diff --git a/challenge-349/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-349/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..36f3550df8 --- /dev/null +++ b/challenge-349/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,72 @@ + + + +package PWC349; + +/** + * PL/Java implementation for PWC 349 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> + * + * + * 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/PWC349-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC349-1.jar', 'PWC349', true ); + select sqlj.set_classpath( 'public', 'PWC349' ); + + select pwc349.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC349-1.jar', 'PWC349', 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 = "pwc349", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task2_pljava( String directions ) throws SQLException { + logger.log( Level.INFO, "Entering pwc349.task2_pljava" ); + + int left = 0, right = 0, up = 0, down = 0; + + for ( String m : directions.split( "" ) ) { + if ( m.equals( "L" ) ) + left++; + else if ( m.equals( "R" ) ) + right++; + else if ( m.equals( "U" ) ) + up++; + else if ( m.equals( "D" ) ) + down++; + } + + + return left == right && up == down; + + + } +} diff --git a/challenge-349/luca-ferrari/plperl/ch-1.plperl b/challenge-349/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..a16bb7369e --- /dev/null +++ b/challenge-349/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 349 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +-- + +CREATE SCHEMA IF NOT EXISTS pwc349; + +CREATE OR REPLACE FUNCTION +pwc349.task1_plperl( text ) +RETURNS int +AS $CODE$ + + my ( $string ) = @_; + my $longest = 0; + + while ( $string =~ / (.) \1+ /xg ) { + $longest = length( $& ) if ( length( $& ) > $longest ); + } + + + return $longest; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-349/luca-ferrari/plperl/ch-2.plperl b/challenge-349/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..89b1c85f5a --- /dev/null +++ b/challenge-349/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,26 @@ +-- +-- Perl Weekly Challenge 349 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +-- + +CREATE SCHEMA IF NOT EXISTS pwc349; + +CREATE OR REPLACE FUNCTION +pwc349.task2_plperl( text ) +RETURNS boolean +AS $CODE$ + + my ( $directions ) = @_; + + my $moves = {}; + for ( split //, $directions ) { + $moves->{ $_ }++; + } + + return 1 if ( $moves->{ 'D' } == $moves->{ 'U' } + && $moves->{ 'L' } == $moves->{ 'R' } ); + return 0; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-349/luca-ferrari/plpgsql/ch-1.sql b/challenge-349/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..a037c2cbfd --- /dev/null +++ b/challenge-349/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,32 @@ +-- +-- Perl Weekly Challenge 349 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +-- + +CREATE SCHEMA IF NOT EXISTS pwc349; + +CREATE OR REPLACE FUNCTION +pwc349.task1_plpgsql( s text ) +RETURNS int +AS $CODE$ +DECLARE + c text; + r int := 0; + ss text; + cc record; +BEGIN + + FOREACH c IN ARRAY regexp_split_to_array( s, '' ) LOOP + FOR cc IN SELECT v FROM regexp_matches( s, c || '+' ) v LOOP + IF length( cc.v::text ) - 2 > r THEN + r = length( cc.v::text ) - 2; + END IF; + END LOOP; + END LOOP; + + RETURN r; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-349/luca-ferrari/plpgsql/ch-2.sql b/challenge-349/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..8a167b6453 --- /dev/null +++ b/challenge-349/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,34 @@ +-- +-- Perl Weekly Challenge 349 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +-- + +CREATE SCHEMA IF NOT EXISTS pwc349; + +CREATE OR REPLACE FUNCTION +pwc349.task2_plpgsql( directions text ) +RETURNS boolean +AS $CODE$ + + WITH moves AS ( + SELECT v, count( * ) as c + FROM regexp_split_to_table( directions, '' ) v + GROUP BY 1 + ) + , summary AS ( + SELECT m1.c - m2.c AS h, m3.c - m4.c AS v + FROM moves m1, moves m2, moves m3, moves m4 + WHERE + m1.v = 'L' AND m2.v = 'R' + AND m3.v = 'U' AND m4.v = 'D' + ) + SELECT exists ( + SELECT * + FROM summary + WHERE h = 0 AND v = 0 + ); + + +$CODE$ +LANGUAGE sql; diff --git a/challenge-349/luca-ferrari/python/ch-1.py b/challenge-349/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..a5bb88372e --- /dev/null +++ b/challenge-349/luca-ferrari/python/ch-1.py @@ -0,0 +1,39 @@ +#!python + +# +# Perl Weekly Challenge 349 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + string = args[ 0 ] + previous = '' + result = 0 + count = 0 + + for current in string : + if previous is None or previous != current : + if count > result : + result = count + + count = 0 + previous = current + + count += 1 + + if count > result : + result = count + + return result + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-349/luca-ferrari/python/ch-2.py b/challenge-349/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..04437f0718 --- /dev/null +++ b/challenge-349/luca-ferrari/python/ch-2.py @@ -0,0 +1,33 @@ +#!python + +# +# Perl Weekly Challenge 349 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + h = 0 + v = 0 + + for move in args[ 0 ]: + if move == 'L' : + h -= 1 + elif move == 'R': + h += 1 + elif move == 'U': + v += 1 + elif move == 'D': + v -= 1 + + return v == 0 and h == 0 + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-349/luca-ferrari/raku/ch-1.raku b/challenge-349/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..6adad444b9 --- /dev/null +++ b/challenge-349/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 349 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +# + +sub MAIN( Str $string ) { + + my %powers; + for $string.comb -> $l { + %powers{ ( $string ~~ / $l+ / ).Str.chars } = $l; + } + + %power.keys.max.say; + #(%powers{ %powers.keys.max } xx %powers.keys.max).join.say; +} diff --git a/challenge-349/luca-ferrari/raku/ch-2.raku b/challenge-349/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..5450661028 --- /dev/null +++ b/challenge-349/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!raku + +# +# Perl Weekly Challenge 349 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-349> +# + +sub MAIN( Str $directions ) { + my %moves; + %moves{ $_ }++ for ( $directions.comb ); + + 'True'.say and exit if ( %moves<U> == %moves<D> && %moves<R> == %moves<L> ); + 'False'.say; +} |
