diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-02 15:38:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-02 15:38:59 +0100 |
| commit | a0e8e4043d4e1e7696daaeacf4a52f14a6ceda89 (patch) | |
| tree | 287955506588b7fb136f66df769ae44b5254fd16 | |
| parent | a2318c529e1e11bcb600ce7b757ad402c12a0538 (diff) | |
| parent | c87d78c276f91280ec4e1e29e99f786f53e31488 (diff) | |
| download | perlweeklychallenge-club-a0e8e4043d4e1e7696daaeacf4a52f14a6ceda89.tar.gz perlweeklychallenge-club-a0e8e4043d4e1e7696daaeacf4a52f14a6ceda89.tar.bz2 perlweeklychallenge-club-a0e8e4043d4e1e7696daaeacf4a52f14a6ceda89.zip | |
Merge pull request #12774 from fluca1978/PWC341
PWC 341
21 files changed, 372 insertions, 3 deletions
diff --git a/challenge-341/luca-ferrari/blog-1.txt b/challenge-341/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..2fa473d077 --- /dev/null +++ b/challenge-341/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task1 diff --git a/challenge-341/luca-ferrari/blog-10.txt b/challenge-341/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..482fb5c6cc --- /dev/null +++ b/challenge-341/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task2pljava diff --git a/challenge-341/luca-ferrari/blog-2.txt b/challenge-341/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..2478b191d6 --- /dev/null +++ b/challenge-341/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task2 diff --git a/challenge-341/luca-ferrari/blog-3.txt b/challenge-341/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..8249ff790e --- /dev/null +++ b/challenge-341/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task1plperl diff --git a/challenge-341/luca-ferrari/blog-4.txt b/challenge-341/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..78d9dcc704 --- /dev/null +++ b/challenge-341/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task2plperl diff --git a/challenge-341/luca-ferrari/blog-5.txt b/challenge-341/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..86efca83bb --- /dev/null +++ b/challenge-341/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task1plpgsql diff --git a/challenge-341/luca-ferrari/blog-6.txt b/challenge-341/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..14f40b3af9 --- /dev/null +++ b/challenge-341/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task2plpgsql diff --git a/challenge-341/luca-ferrari/blog-7.txt b/challenge-341/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..7f7d1b01bb --- /dev/null +++ b/challenge-341/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task1python diff --git a/challenge-341/luca-ferrari/blog-8.txt b/challenge-341/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..de5aa1d3d5 --- /dev/null +++ b/challenge-341/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task2python diff --git a/challenge-341/luca-ferrari/blog-9.txt b/challenge-341/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..9ca442ab45 --- /dev/null +++ b/challenge-341/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/10/02/PerlWeeklyChallenge341.html#task1pljava diff --git a/challenge-341/luca-ferrari/pljava/pom.xml b/challenge-341/luca-ferrari/pljava/pom.xml index 353e1821da..674fcf6c4d 100644 --- a/challenge-341/luca-ferrari/pljava/pom.xml +++ b/challenge-341/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC333 + PWC341 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 333 with package PWC333</name> - <description>Implementation of the tasks in PL/Java for PWC 333</description> + <name>Perl Weekly Challenge 341 with package PWC341</name> + <description>Implementation of the tasks in PL/Java for PWC 341</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-341/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-341/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..284db05e0b --- /dev/null +++ b/challenge-341/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,77 @@ + + + +package PWC341; + +/** + * PL/Java implementation for PWC 341 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> + * + * + * 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/PWC341-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC341-1.jar', 'PWC341', true ); + select sqlj.set_classpath( 'public', 'PWC341' ); + + select pwc341.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC341-1.jar', 'PWC341', 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 = "pwc341", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String[] task1_pljava( String sentence, String[] keys ) throws SQLException { + logger.log( Level.INFO, "Entering pwc341.task1_pljava" ); + + List<String> words = new LinkedList<String>(); + for ( String w : sentence.split( "\\s+" ) ) + words.add( w ); + + Iterator<String> iter = words.iterator(); + while ( iter.hasNext() ) { + String current_word = iter.next(); + boolean ok = true; + for ( String k : keys ) + if ( current_word.contains( k ) ) + ok = false; + + if ( ! ok ) + iter.remove(); + } + + String result[] = new String[ words.size() ]; + int i = 0; + for ( String w : words ) + result[ i++ ] = w; + + return result; + } +} diff --git a/challenge-341/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-341/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..d7463aabe0 --- /dev/null +++ b/challenge-341/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,62 @@ + + + +package PWC341; + +/** + * PL/Java implementation for PWC 341 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> + * + * + * 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/PWC341-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC341-1.jar', 'PWC341', true ); + select sqlj.set_classpath( 'public', 'PWC341' ); + + select pwc341.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC341-1.jar', 'PWC341', 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 = "pwc341", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String task2_pljava( String word, String prefix ) throws SQLException { + logger.log( Level.INFO, "Entering pwc341.task2_pljava" ); + + int index = word.indexOf( prefix, 1 ); + StringBuilder builder = new StringBuilder(); + + builder.append( word.substring( 0, index ) ); + builder.reverse(); + return builder.toString() + word.substring( index + 1 ); + + } +} diff --git a/challenge-341/luca-ferrari/plperl/ch-1.plperl b/challenge-341/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..54cf12b5f4 --- /dev/null +++ b/challenge-341/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 341 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +-- + +CREATE SCHEMA IF NOT EXISTS pwc341; + +CREATE OR REPLACE FUNCTION +pwc341.task1_plperl( text, text[] ) +RETURNS SETOF text +AS $CODE$ + + my ( $text, $keys ) = @_; + + for my $word ( split /\s+/, $text ) { + my $found = 0; + for my $key ( $keys->@* ) { + $found += scalar grep { $_ eq $key } split( //, $word ); + last if ( $found ); + } + + return_next( $word ) unless ( $found ); + } + +return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-341/luca-ferrari/plperl/ch-2.plperl b/challenge-341/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..6d40bb266e --- /dev/null +++ b/challenge-341/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,25 @@ +-- +-- Perl Weekly Challenge 341 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +-- + +CREATE SCHEMA IF NOT EXISTS pwc341; + +CREATE OR REPLACE FUNCTION +pwc341.task2_plperl( text, text ) +RETURNS text +AS $CODE$ + + my ( $text, $prefix ) = @_; + + my $index = index( $text, $prefix ); + return $index unless( $index ); + + my @chars = split //, $text; + return join( '', + reverse( @chars[ 0 .. $index ] ), + @chars[ $index + 1 .. $#chars ] ); + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-341/luca-ferrari/plpgsql/ch-1.sql b/challenge-341/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..41270eaabe --- /dev/null +++ b/challenge-341/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,36 @@ +-- +-- Perl Weekly Challenge 341 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +-- + +CREATE SCHEMA IF NOT EXISTS pwc341; + +CREATE OR REPLACE FUNCTION +pwc341.task1_plpgsql( sentence text, keys text[] ) +RETURNS SETOF text +AS $CODE$ +DECLARE + current_word text; + ko int; + k text; +BEGIN + FOR current_word IN SELECT w FROM regexp_split_to_table( sentence, '\s+' ) w LOOP + ko := 0; + + FOREACH k IN ARRAY keys LOOP + IF current_word ~ k THEN + ko := ko + 1; + END IF; + END LOOP; + + IF ko = 0 THEN + RETURN NEXT current_word; + END IF; + + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-341/luca-ferrari/plpgsql/ch-2.sql b/challenge-341/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..d5947bac41 --- /dev/null +++ b/challenge-341/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 341 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +-- + +CREATE SCHEMA IF NOT EXISTS pwc341; + +CREATE OR REPLACE FUNCTION +pwc341.task2_plpgsql( word text, p text ) +RETURNS text +AS $CODE$ +DECLARE + i int; + j int; + tt text[]; + r text := ''; +BEGIN + i := position( p IN word ); + tt := regexp_split_to_array( word, '' ); + + j := i; + WHILE i > 0 LOOP + r := r || tt[ i ]; + i := i - 1; + END LOOP; + + r := r || array_to_string( tt[ j + 1 : ], '' ); + + RETURN r; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-341/luca-ferrari/python/ch-1.py b/challenge-341/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..011d3a9598 --- /dev/null +++ b/challenge-341/luca-ferrari/python/ch-1.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 341 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + sentence = args[ 0 ] + keys = args[ 1 : ] + result = [] + + for word in sentence.split(): + found = False + + for k in keys: + if k in word: + found = True + + if not found: + result.append( word ) + + return ' '.join( result ) + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-341/luca-ferrari/python/ch-2.py b/challenge-341/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..c27b0b937c --- /dev/null +++ b/challenge-341/luca-ferrari/python/ch-2.py @@ -0,0 +1,25 @@ +#!python + +# +# Perl Weekly Challenge 341 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + word = args[ 0 ] + prefix = args[ 1 ] + + index = word.index( prefix ) + + return word[ index : 0 : -1 ] + word[ index : ] + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-341/luca-ferrari/raku/ch-1.raku b/challenge-341/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..4084ba207e --- /dev/null +++ b/challenge-341/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,25 @@ +#!raku + +# +# Perl Weekly Challenge 341 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +# + +sub MAIN( Str $text, *@keys ) { + $text.say and exit( 0 ) unless ( @keys.elems ); + + my @words; + for $text.split( / \s+ / ) -> $current-word { + my $found = 0; + for @keys { + $found += $current-word.comb.grep( * ~~ $_ ).elems; + } + last if $found; + + @words.push: $current-word; + } + + @words.join( ' ' ).say; +} diff --git a/challenge-341/luca-ferrari/raku/ch-2.raku b/challenge-341/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..18cab1d04c --- /dev/null +++ b/challenge-341/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,13 @@ +#!raku + +# +# Perl Weekly Challenge 341 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-341> +# + +sub MAIN( Str $text, Str $prefix where { $text ~~ / $prefix / } ) { + my $index = $text.comb( :skip-empty ).first( * ~~ $prefix, :k ); + ( $text.comb[ 0 .. $index ].join.flip ~ $text.comb[ $index + 1 .. * - 1 ].join ).join.say; +} |
