diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-29 12:15:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-29 12:15:20 +0100 |
| commit | e844e48cdcb9faf8019762ddd4f098e556ec77be (patch) | |
| tree | 3062528438b70793509217d9484f39e0dd7486e5 | |
| parent | 692029da785a7046dbf7b4b47177be1f0a1a160f (diff) | |
| parent | c02a31ffc17ad47304d60556c6e0c8987a876715 (diff) | |
| download | perlweeklychallenge-club-e844e48cdcb9faf8019762ddd4f098e556ec77be.tar.gz perlweeklychallenge-club-e844e48cdcb9faf8019762ddd4f098e556ec77be.tar.bz2 perlweeklychallenge-club-e844e48cdcb9faf8019762ddd4f098e556ec77be.zip | |
Merge pull request #10510 from fluca1978/PWC280
PWC 280
21 files changed, 353 insertions, 3 deletions
diff --git a/challenge-280/luca-ferrari/blog-1.txt b/challenge-280/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..a52ee84368 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task1 diff --git a/challenge-280/luca-ferrari/blog-10.txt b/challenge-280/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..d2beb602b6 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task2pljava diff --git a/challenge-280/luca-ferrari/blog-2.txt b/challenge-280/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..042d740a59 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task2 diff --git a/challenge-280/luca-ferrari/blog-3.txt b/challenge-280/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..e50e1a9b09 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task1plperl diff --git a/challenge-280/luca-ferrari/blog-4.txt b/challenge-280/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..013e6d0999 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task2plperl diff --git a/challenge-280/luca-ferrari/blog-5.txt b/challenge-280/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..ec834e2e11 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task1plpgsql diff --git a/challenge-280/luca-ferrari/blog-6.txt b/challenge-280/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..6a7061d47c --- /dev/null +++ b/challenge-280/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task2plpgsql diff --git a/challenge-280/luca-ferrari/blog-7.txt b/challenge-280/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..a6e3ca6709 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task1python diff --git a/challenge-280/luca-ferrari/blog-8.txt b/challenge-280/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..e64a701aed --- /dev/null +++ b/challenge-280/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task2python diff --git a/challenge-280/luca-ferrari/blog-9.txt b/challenge-280/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..d8b6484bb7 --- /dev/null +++ b/challenge-280/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/07/29/PerlWeeklyChallenge280.html#task1pljava diff --git a/challenge-280/luca-ferrari/pljava/pom.xml b/challenge-280/luca-ferrari/pljava/pom.xml index 9402a684d7..96ca25649f 100644 --- a/challenge-280/luca-ferrari/pljava/pom.xml +++ b/challenge-280/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC278 + PWC280 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 278 with package PWC278</name> - <description>Implementation of the tasks in PL/Java for PWC 278</description> + <name>Perl Weekly Challenge 280 with package PWC280</name> + <description>Implementation of the tasks in PL/Java for PWC 280</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-280/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-280/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..38d4c0f8ff --- /dev/null +++ b/challenge-280/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,67 @@ + + + +package PWC280; + +/** + * PL/Java implementation for PWC 280 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> + * + * + * 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/PWC280-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC280-1.jar', 'PWC280', true ); + select sqlj.set_classpath( 'public', 'PWC280' ); + + select pwc280.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC280-1.jar', 'PWC280', 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 = "pwc280", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static String task1_pljava( String string ) throws SQLException { + logger.log( Level.INFO, "Entering pwc280.task1_pljava" ); + + for ( String needle : string.split( "" ) ) { + int count = 0; + for ( String s : string.split( "" ) ) + if ( s.equals( needle ) ) + count++; + + + if ( count > 1 ) + return needle; + } + + return null; + } +} diff --git a/challenge-280/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-280/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..2f15f8698d --- /dev/null +++ b/challenge-280/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,70 @@ + + + +package PWC280; + +/** + * PL/Java implementation for PWC 280 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> + * + * + * 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/PWC280-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC280-1.jar', 'PWC280', true ); + select sqlj.set_classpath( 'public', 'PWC280' ); + + select pwc280.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC280-1.jar', 'PWC280', 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 = "pwc280", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( String input ) throws SQLException { + logger.log( Level.INFO, "Entering pwc280.task2_pljava" ); + + + int pos = -1; + int count = 0; + for ( String needle : input.split( "[|]" ) ) { + pos++; + if ( pos % 2 != 0 ) + continue; + + // here I've a left part in the pair + for ( String a : needle.split( "" ) ) + if ( a.equals( "*" ) ) + count++; + } + + return count; + } +} diff --git a/challenge-280/luca-ferrari/plperl/ch-1.plperl b/challenge-280/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..99152db30f --- /dev/null +++ b/challenge-280/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 280 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +-- + +CREATE SCHEMA IF NOT EXISTS pwc280; + +CREATE OR REPLACE FUNCTION +pwc280.task1_plperl( text ) +RETURNS text +AS $CODE$ + + my ( $string ) = @_; + my @chars = split //, $string; + + for my $needle ( @chars ) { + return $needle if ( grep( { $_ eq $needle } @chars ) > 1 ); + } + + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-280/luca-ferrari/plperl/ch-2.plperl b/challenge-280/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..b59d34c94d --- /dev/null +++ b/challenge-280/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 280 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +-- + +CREATE SCHEMA IF NOT EXISTS pwc280; + +CREATE OR REPLACE FUNCTION +pwc280.task2_plperl( text ) +RETURNS int +AS $CODE$ + + my ( $input ) = @_; + my @pairs; + + my @parts = split /[|]/, $input; + my $counter = 0; + for ( 0 .. @parts - 1 ) { + next if $_ % 2; # take only left side of a pair + my $needle = $parts[ $_ ]; + + next if ! $needle; + $counter += scalar grep { $_ eq '*' } split //, $needle; + } + + return $counter; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-280/luca-ferrari/plpgsql/ch-1.sql b/challenge-280/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..54ebf2965c --- /dev/null +++ b/challenge-280/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 280 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +-- + +CREATE SCHEMA IF NOT EXISTS pwc280; + +CREATE OR REPLACE FUNCTION +pwc280.task1_plpgsql( s text ) +RETURNS char +AS $CODE$ +DECLARE + needle char; + counting int; +BEGIN + + FOR needle IN SELECT v::char FROM regexp_split_to_table( s, '' ) v LOOP + counting := 0; + SELECT count( * ) + INTO counting + FROM regexp_split_to_table( s, '' ) v + WHERE v = needle; + + IF counting > 1 THEN + RETURN needle; + END IF; + END LOOP; + + RETURN NULL; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-280/luca-ferrari/plpgsql/ch-2.sql b/challenge-280/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..64a871f027 --- /dev/null +++ b/challenge-280/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,27 @@ +-- +-- Perl Weekly Challenge 280 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +-- + +CREATE SCHEMA IF NOT EXISTS pwc280; + +CREATE OR REPLACE FUNCTION +pwc280.task2_plpgsql( s text ) +RETURNS int +AS $CODE$ + + WITH searching_for AS ( + SELECT v::text, row_number() over () as r + FROM regexp_split_to_table( s, '[|]' ) v + ) + , data_parts AS ( + SELECT v + FROM searching_for + WHERE r % 2 <> 0 + ) + SELECT sum( length( v ) - length( replace( v, '*', '' ) ) ) + FROM data_parts; + +$CODE$ +LANGUAGE sql; diff --git a/challenge-280/luca-ferrari/python/ch-1.py b/challenge-280/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..b3c9ef9c4c --- /dev/null +++ b/challenge-280/luca-ferrari/python/ch-1.py @@ -0,0 +1,23 @@ +#!python + +# +# Perl Weekly Challenge 280 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + string = args[ 0 ] + for l in string: + if len( list( filter( lambda x: x == l, string ) ) ) > 1: + return l + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-280/luca-ferrari/python/ch-2.py b/challenge-280/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..82ff85167f --- /dev/null +++ b/challenge-280/luca-ferrari/python/ch-2.py @@ -0,0 +1,28 @@ +#!python + +# +# Perl Weekly Challenge 280 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + pos = 0 + count = 0 + for needle in args[ 0 ]: + if needle == '|': + pos += 1 + continue + elif needle == '*' and pos % 2 != 0: + count += 1 + + return count + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-280/luca-ferrari/raku/ch-1.raku b/challenge-280/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..0de32170d4 --- /dev/null +++ b/challenge-280/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!raku + +# +# Perl Weekly Challenge 280 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +# + +sub MAIN( Str $string where { $string ~~ / ^ <[a..z]>+ $ / } ) { + for $string.comb -> $needle { + $needle.say and exit if ( $string.comb.grep( * ~~ $needle ).elems > 1 ); + } +} diff --git a/challenge-280/luca-ferrari/raku/ch-2.raku b/challenge-280/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..648f4bc25b --- /dev/null +++ b/challenge-280/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!raku + +# +# Perl Weekly Challenge 280 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-280> +# + +sub MAIN( Str $input is copy where { $input ~~ / <[|]>+ / } ) { + + # avoid the last bar to trigger an empty pair + if $input ~~ / <[|]> $ / { + $input = $input.subst( / <[|]> $ /, '' ); + } + + my @pairs; + for $input.split( '|' ) -> $k, $v { + next if ! $k || ! $v; + @pairs.push: $k => $v; + } + + @pairs.map( { $_.key.comb.grep( * ~~ '*' ).elems } ).sum.say; + +} |
