diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-20 09:11:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-20 09:11:35 +0100 |
| commit | 6fb845d9f7473188b0285f3f352adadb2521ea19 (patch) | |
| tree | aa6b996170e25e2137ad2ec12b4545954863c9a7 | |
| parent | 44c8332cc4a1a7d9f817d444d43fb544e89d5eae (diff) | |
| parent | b2c1df23b6752082dc150b267b7c32942cab289b (diff) | |
| download | perlweeklychallenge-club-6fb845d9f7473188b0285f3f352adadb2521ea19.tar.gz perlweeklychallenge-club-6fb845d9f7473188b0285f3f352adadb2521ea19.tar.bz2 perlweeklychallenge-club-6fb845d9f7473188b0285f3f352adadb2521ea19.zip | |
Merge pull request #12050 from fluca1978/PWC322
PWC 322
21 files changed, 397 insertions, 3 deletions
diff --git a/challenge-322/luca-ferrari/blog-1.txt b/challenge-322/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..18b8dc411f --- /dev/null +++ b/challenge-322/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1 diff --git a/challenge-322/luca-ferrari/blog-10.txt b/challenge-322/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..1a42faab32 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2pljava diff --git a/challenge-322/luca-ferrari/blog-2.txt b/challenge-322/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..3614d5d753 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2 diff --git a/challenge-322/luca-ferrari/blog-3.txt b/challenge-322/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..95f69cfb89 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1plperl diff --git a/challenge-322/luca-ferrari/blog-4.txt b/challenge-322/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..584e3c712a --- /dev/null +++ b/challenge-322/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2plperl diff --git a/challenge-322/luca-ferrari/blog-5.txt b/challenge-322/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..02f83e5adb --- /dev/null +++ b/challenge-322/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1plpgsql diff --git a/challenge-322/luca-ferrari/blog-6.txt b/challenge-322/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..8aaf0598a9 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2plpgsql diff --git a/challenge-322/luca-ferrari/blog-7.txt b/challenge-322/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..c3cc83cf7b --- /dev/null +++ b/challenge-322/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1python diff --git a/challenge-322/luca-ferrari/blog-8.txt b/challenge-322/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..f4247582c1 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2python diff --git a/challenge-322/luca-ferrari/blog-9.txt b/challenge-322/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..d59d53b97c --- /dev/null +++ b/challenge-322/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1pljava diff --git a/challenge-322/luca-ferrari/pljava/pom.xml b/challenge-322/luca-ferrari/pljava/pom.xml index 3a9f67f2be..8afd8c47a8 100644 --- a/challenge-322/luca-ferrari/pljava/pom.xml +++ b/challenge-322/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC321 + PWC322 </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> + <name>Perl Weekly Challenge 322 with package PWC322</name> + <description>Implementation of the tasks in PL/Java for PWC 322</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-322/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-322/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..c2d3ed7dcb --- /dev/null +++ b/challenge-322/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,77 @@ + + + +package PWC322; + +/** + * PL/Java implementation for PWC 322 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> + * + * + * 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/PWC322-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', true ); + select sqlj.set_classpath( 'public', 'PWC322' ); + + select pwc322.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', 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 = "pwc322", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String task1_pljava( String string, int size ) throws SQLException { + logger.log( Level.INFO, "Entering pwc322.task1_pljava" ); + + List<String> pieces = new LinkedList<String>(); + + for ( String s : string.split( "-" ) ) { + for ( String q : s.split( "" ) ) + pieces.add( q ); + } + + String result = ""; + + for ( int i = 0; i < pieces.size(); ) { + for ( int j = 0; j < size; j++ ) { + if ( i >= pieces.size() ) + break; + + result += pieces.get( i++ ); + } + + if ( i < pieces.size() ) + result += "-"; + } + + return result; + } +} diff --git a/challenge-322/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-322/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..b237865f31 --- /dev/null +++ b/challenge-322/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,81 @@ + + + +package PWC322; + +/** + * PL/Java implementation for PWC 322 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> + * + * + * 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/PWC322-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', true ); + select sqlj.set_classpath( 'public', 'PWC322' ); + + select pwc322.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', 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 = "pwc322", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static int[] task2_pljava( int[] numbers ) throws SQLException { + logger.log( Level.INFO, "Entering pwc322.task2_pljava" ); + + List<Integer> sorted = new LinkedList<Integer>(); + + for ( int i : numbers ) + sorted.add( i ); + + Collections.sort( sorted ); + + int index = 1; + int c = 0; + int[] result = new int[ numbers.length ]; + + for ( int current : numbers ) { + index = 1; + + for ( int seeking : sorted ) { + if ( seeking == current ) + break; + else + index++; + } + + result[ c ] = index; + c++; + } + + return result; + } +} diff --git a/challenge-322/luca-ferrari/plperl/ch-1.plperl b/challenge-322/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..5b9c2f024b --- /dev/null +++ b/challenge-322/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,27 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task1_plperl( text, int) +RETURNS text +AS $CODE$ + + my ( $string, $size ) = @_; + die "Size should be less then the length of the string" if ( length( $string ) < $size ); + + my @chars = split //, reverse join( '', split( '-', $string ) ); + my $string; + while ( @chars ) { + $string .= shift @chars for 1 .. $size; + $string .= '-' if ( @chars ); + } + + return reverse $string; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-322/luca-ferrari/plperl/ch-2.plperl b/challenge-322/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..af0df79895 --- /dev/null +++ b/challenge-322/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $numbers ) = @_; + + my @result; + + for my $current ( $numbers->@* ) { + my $index = 1; + for ( sort { $a <=> $b } $numbers->@* ) { + if ( $_ == $current ) { + push @result, $index; + last; + } + + $index++; + } + } + + return [ @result ]; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-322/luca-ferrari/plpgsql/ch-1.sql b/challenge-322/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..815feb0ef2 --- /dev/null +++ b/challenge-322/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task1_plpgsql( s text, l int ) +RETURNS text +AS $CODE$ +DECLARE + q_match text; + q_result text; +BEGIN + q_match := '(\w{' || l || '})'; + + q_match := $$ SELECT reverse( regexp_replace( reverse( regexp_replace( '$$ || s || $$', '-', '', 'g' ) ), '$$ || q_match || $$', '\1-', 'g' ) )$$; + + raise info '[%]', q_match; + EXECUTE q_match + INTO q_result; + + + RETURN q_result; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-322/luca-ferrari/plpgsql/ch-2.sql b/challenge-322/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..fb7f7558b9 --- /dev/null +++ b/challenge-322/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,34 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task2_plpgsql( n int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current int; + r int; +BEGIN + + FOR current IN SELECT x FROM unnest( n ) x LOOP + WITH sorting AS ( + SELECT row_number() over ( order by y ) as sorted, y + FROM unnest( n ) y + ) + SELECT sorted + INTO r + FROM sorting + WHERE y = current; + + RETURN NEXT r; + + END LOOP; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-322/luca-ferrari/python/ch-1.py b/challenge-322/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..22e45f3117 --- /dev/null +++ b/challenge-322/luca-ferrari/python/ch-1.py @@ -0,0 +1,39 @@ +#!python + +# +# Perl Weekly Challenge 322 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + string = args[ 0 ] + size = int( args[ 1 ] ) + chars = [] + result = "" + + for c in string[::-1] : + if c == '-': + continue + + chars.append( c ) + + counter = 1 + for c in chars: + result += c + counter += 1 + + if counter % size == 0: + result += '-' + + return result[::-1] + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-322/luca-ferrari/python/ch-2.py b/challenge-322/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..5915cc602b --- /dev/null +++ b/challenge-322/luca-ferrari/python/ch-2.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 322 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + numbers = list( map( int, args ) ) + sorted_numbers = list( map( int, args ) ) + sorted_numbers.sort() + result = [] + + for c in numbers: + index = 1 + for x in sorted_numbers: + if x == c: + result.append( index ) + else: + index += 1 + + + return result + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-322/luca-ferrari/raku/ch-1.raku b/challenge-322/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..f18e4e7323 --- /dev/null +++ b/challenge-322/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 322 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +# + +sub MAIN( Str $string, Int $size where { $size <= $string.chars } ) { + $string.split( '-' ).join.flip.comb( :skip-empty ).rotor( $size, :partial ).map( *.join ).join( '-' ).flip.say; +} diff --git a/challenge-322/luca-ferrari/raku/ch-2.raku b/challenge-322/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..8b2ab701b0 --- /dev/null +++ b/challenge-322/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 322 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-322> +# + +sub MAIN( *@numbers where { @numbers.grep( * ~~ Int ).elems == @numbers.elems } ) { + my @result; + for @numbers -> $current { + @result.push: @numbers.sort.grep( * ~~ $current, :k ).first + 1; + } + + @result.say; +} |
