diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-30 15:14:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-30 15:14:14 +0100 |
| commit | 0f48e7b96f6c4d16ca56fbe4da008ea8ca99ddae (patch) | |
| tree | e13bce535b572c44bac1c839ac6722edaf05b870 | |
| parent | 722193671f9e0565a3f42479828d103e6f6a4768 (diff) | |
| parent | 92d7474a6754e622fe4f2091f8e25c52ac239e52 (diff) | |
| download | perlweeklychallenge-club-0f48e7b96f6c4d16ca56fbe4da008ea8ca99ddae.tar.gz perlweeklychallenge-club-0f48e7b96f6c4d16ca56fbe4da008ea8ca99ddae.tar.bz2 perlweeklychallenge-club-0f48e7b96f6c4d16ca56fbe4da008ea8ca99ddae.zip | |
Merge pull request #10932 from fluca1978/PWC289
PWC 289
21 files changed, 489 insertions, 0 deletions
diff --git a/challenge-289/luca-ferrari/blog-1.txt b/challenge-289/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..5e27a383e1 --- /dev/null +++ b/challenge-289/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task1 diff --git a/challenge-289/luca-ferrari/blog-10.txt b/challenge-289/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..7ce20af2ff --- /dev/null +++ b/challenge-289/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task2pljava diff --git a/challenge-289/luca-ferrari/blog-2.txt b/challenge-289/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..6b1d5ae71c --- /dev/null +++ b/challenge-289/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task2 diff --git a/challenge-289/luca-ferrari/blog-3.txt b/challenge-289/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..1f08ed2747 --- /dev/null +++ b/challenge-289/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task1plperl diff --git a/challenge-289/luca-ferrari/blog-4.txt b/challenge-289/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..c3c1d5e2ad --- /dev/null +++ b/challenge-289/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task2plperl diff --git a/challenge-289/luca-ferrari/blog-5.txt b/challenge-289/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..5c91d9309c --- /dev/null +++ b/challenge-289/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task1plpgsql diff --git a/challenge-289/luca-ferrari/blog-6.txt b/challenge-289/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..8977d21652 --- /dev/null +++ b/challenge-289/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task2plpgsql diff --git a/challenge-289/luca-ferrari/blog-7.txt b/challenge-289/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..de1b243a92 --- /dev/null +++ b/challenge-289/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task1python diff --git a/challenge-289/luca-ferrari/blog-8.txt b/challenge-289/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..f30a1b00a0 --- /dev/null +++ b/challenge-289/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task2python diff --git a/challenge-289/luca-ferrari/blog-9.txt b/challenge-289/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..c442b427f8 --- /dev/null +++ b/challenge-289/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/30/PerlWeeklyChallenge289.html#task1pljava diff --git a/challenge-289/luca-ferrari/pljava/pom.xml b/challenge-289/luca-ferrari/pljava/pom.xml new file mode 100644 index 0000000000..27cc94c005 --- /dev/null +++ b/challenge-289/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> + PWC289 + </artifactId> + <version> + 1 + </version> + + <name>Perl Weekly Challenge 289 with package PWC289</name> + <description>Implementation of the tasks in PL/Java for PWC 289</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-289/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-289/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..9666996c9a --- /dev/null +++ b/challenge-289/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,64 @@ + + + +package PWC289; + +/** + * PL/Java implementation for PWC 289 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> + * + * + * 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/PWC289-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC289-1.jar', 'PWC289', true ); + select sqlj.set_classpath( 'public', 'PWC289' ); + + select pwc289.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC289-1.jar', 'PWC289', 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 = "pwc289", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( int[] nums ) throws SQLException { + logger.log( Level.INFO, "Entering pwc289.task1_pljava" ); + + List<Integer> sorted = IntStream.of( nums ).boxed().sorted( Collections.reverseOrder() ) + .collect( Collectors.toList() ); + + + if ( sorted.size() >= 3 ) + return sorted.get( 3 ); + else + return sorted.get( 0 ); + + } +} diff --git a/challenge-289/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-289/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..8532cda167 --- /dev/null +++ b/challenge-289/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,89 @@ + + + +package PWC289; + +/** + * PL/Java implementation for PWC 289 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> + * + * + * 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/PWC289-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC289-1.jar', 'PWC289', true ); + select sqlj.set_classpath( 'public', 'PWC289' ); + + select pwc289.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC289-1.jar', 'PWC289', 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; +import java.util.regex.*; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc289", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String[] task2_pljava( String[] words ) throws SQLException { + logger.log( Level.INFO, "Entering pwc289.task2_pljava" ); + + List<String> result = new LinkedList<String>(); + + for ( String current : words ) { + String[] letters = current.split( "" ); + + + int last_index = letters.length - 1; + int first_index = 0; + + Pattern pattern = Pattern.compile( "^[a-z0-9A-Z]$" ); + + + while ( ! pattern.matcher( letters[ last_index ] ).find() ) + last_index--; + + List<String> shuffling = new LinkedList<String>(); + for ( int i = 1; i < last_index; i++ ) + shuffling.add( letters[ i ] ); + + Collections.shuffle( shuffling ); + + String current_result = letters[ first_index ] + String.join( "", shuffling ); + for ( int i = last_index; i < letters.length; i++ ) + current_result += letters[ i ]; + + result.add( current_result ); + } + + String[] output = new String[ result.size() ]; + for ( int i = 0; i < output.length; i++ ) + output[ i ] = result.get( i ); + + return output; + } +} diff --git a/challenge-289/luca-ferrari/plperl/ch-1.plperl b/challenge-289/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..49c10cd8f1 --- /dev/null +++ b/challenge-289/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 289 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +-- + +CREATE SCHEMA IF NOT EXISTS pwc289; + +CREATE OR REPLACE FUNCTION +pwc289.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + + my ( $numbers ) = @_; + + my $classify = {}; + $classify->{ $_ }++ for ( $numbers->@* ); + + my @sorted = sort keys $classify->%*; + return $sorted[ -1 ] if ( @sorted < 3 ); + return $sorted[ -3 ]; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-289/luca-ferrari/plperl/ch-2.plperl b/challenge-289/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..813f126e7c --- /dev/null +++ b/challenge-289/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,38 @@ +-- +-- Perl Weekly Challenge 289 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +-- + +CREATE SCHEMA IF NOT EXISTS pwc289; + +CREATE OR REPLACE FUNCTION +pwc289.task2_plperl( text[] ) +RETURNS SETOF text +AS $CODE$ + + use List::Util qw/shuffle/; + my ( $words ) = @_; + + for my $current_word ( $words->@* ) { + my @letters = split //, $current_word; + my @new_word; + + my ( $first_index, $last_index ) = ( 0, $#letters ); + my ( $first, $last ) = ( @letters[ $first_index ], @letters[ $last_index ] ); + + while ( $last !~ /[a-z0-9]/i ) { + $last_index--; + $last = $letters[ $last_index ]; + } + + + # shuffle the remaining part + my @shuffled = shuffle @letters[ $first_index + 1 .. $last_index - 1 ]; + + return_next( join '', $first, @shuffled, $last, @letters[ $last_index + 1 .. $#letters ] ); + } + +return undef; +$CODE$ +LANGUAGE plperlu; diff --git a/challenge-289/luca-ferrari/plpgsql/ch-1.sql b/challenge-289/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..e215785cc7 --- /dev/null +++ b/challenge-289/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,39 @@ +-- +-- Perl Weekly Challenge 289 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +-- + +CREATE SCHEMA IF NOT EXISTS pwc289; + +CREATE OR REPLACE FUNCTION +pwc289.task1_plpgsql( n int[] ) +RETURNS int +AS $CODE$ +DECLARE + found_max int; +BEGIN + IF array_length( n, 1 ) > 3 THEN + WITH s AS ( + SELECT v, row_number() OVER ( ORDER BY v DESC ) AS r + FROM unnest( n ) v + GROUP BY v + ORDER BY v DESC + ) + SELECT v + INTO found_max + FROM s + WHERE + r = 3 + ; + ELSE + SELECT MAX( v ) + INTO found_max + FROM unnest( n ) v; + END IF; + + RETURN found_max; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-289/luca-ferrari/plpgsql/ch-2.sql b/challenge-289/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..6865f0e67b --- /dev/null +++ b/challenge-289/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,46 @@ +-- +-- Perl Weekly Challenge 289 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +-- + +CREATE SCHEMA IF NOT EXISTS pwc289; + +CREATE OR REPLACE FUNCTION +pwc289.task2_plpgsql( words text[]) +RETURNS SETOF text +AS $CODE$ +DECLARE + + current_word text; + first_index int; + last_index int; + new_word text; +BEGIN + FOREACH current_word IN ARRAY words LOOP + first_index := 1; + last_index := length( current_word ); + new_word := substring( current_word from first_index for 1 ); + + WHILE NOT substring( current_word from last_index for 1 ) ~ '[a-z0-9A-Z]' LOOP + last_index := last_index - 1; + END LOOP; + + + WITH l AS ( SELECT v::text + FROM regexp_split_to_table( substring( current_word from first_index + 1 for last_index - first_index - 1 ), '' ) v + ORDER BY random() ) + SELECT string_agg( l.v, '' ) + INTO new_word + FROM l; + + + RETURN NEXT substring( current_word from first_index for 1 ) + || new_word + || substring( current_word from last_index ); + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-289/luca-ferrari/python/ch-1.py b/challenge-289/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..1713851c1f --- /dev/null +++ b/challenge-289/luca-ferrari/python/ch-1.py @@ -0,0 +1,26 @@ +#!python + +# +# Perl Weekly Challenge 289 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + sorted_nums = list( sorted( map( int, args ) ) ) + sorted_nums = sorted_nums[ ::-1 ] + + if len( sorted_nums ) >= 3: + return sorted_nums[ 2 ] + else: + return sorted_nums[ 0 ] + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-289/luca-ferrari/python/ch-2.py b/challenge-289/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..30c16a5e2a --- /dev/null +++ b/challenge-289/luca-ferrari/python/ch-2.py @@ -0,0 +1,40 @@ +#!python + +# +# Perl Weekly Challenge 289 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +# + +import sys +import random + +# task implementation +# the return value will be printed +def task_2( words ): + new_words = [] + + for word in words: + current_word = [] + current_word.append( word[ 0 ] ) + + last_index = len( word ) - 1 + while not str.isalpha( word[ last_index ] ) and not str.isdigit( word[ last_index ] ): + last_index -= 1 + + shuffling = list( map( str, word[ 1 : last_index ] ) ) + random.shuffle( shuffling ) + for r in shuffling: + current_word.append( r ) + + current_word.append( word[ last_index : ] ) + new_words.append( ''.join( current_word ) ) + + return new_words + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-289/luca-ferrari/raku/ch-1.raku b/challenge-289/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..cf2acaf32c --- /dev/null +++ b/challenge-289/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!raku + +# +# Perl Weekly Challenge 289 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +# + +sub MAIN( *@numbers is copy where { @numbers.grep( * ~~ Int ).elems == @numbers.elems } ) { + @numbers = @numbers.sort.unique; + @numbers.elems >= 3 ?? @numbers[ * - 3 ].say !! @numbers[ * - 1 ].say; +} diff --git a/challenge-289/luca-ferrari/raku/ch-2.raku b/challenge-289/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..a14669661a --- /dev/null +++ b/challenge-289/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!raku + +# +# Perl Weekly Challenge 289 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-289> +# + +sub MAIN( *@words ) { + my @output; + for @words -> $current_word { + my @letters = $current_word.comb( :skip-empty ); + my ( $index_first, $index_last ) = 0, @letters.elems - 1; + while ( @letters[ $index_last ] !~~ /<[a .. z A .. Z 0 .. 9 ]>/ ) { + $index_last--; + } + + my $new_word = ( @letters[ $index_first ], + @letters[ $index_first + 1 .. $index_last - 1 ].pick( $index_last - $index_first ).join( '' ), + @letters[ $index_last ], + @letters[ $index_last + 1 .. * - 1 ] ).join( '' ); + + @output.push: $new_word; + } + + @output.join( ' ' ).say; +} |
