diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-11 12:55:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-11 12:55:50 +0100 |
| commit | 0cda868f8b14657f5e436276e262b47035bf341d (patch) | |
| tree | 9c5da69e00266941f1ca0bf017d29af3d95618de | |
| parent | b62ded51075dde53bac2ba4ac7de6089c4996241 (diff) | |
| parent | 33685946d60ac25cbbe9d221413d1240f124145f (diff) | |
| download | perlweeklychallenge-club-0cda868f8b14657f5e436276e262b47035bf341d.tar.gz perlweeklychallenge-club-0cda868f8b14657f5e436276e262b47035bf341d.tar.bz2 perlweeklychallenge-club-0cda868f8b14657f5e436276e262b47035bf341d.zip | |
Merge pull request #10244 from fluca1978/PWC273
PWC 273
21 files changed, 302 insertions, 3 deletions
diff --git a/challenge-273/luca-ferrari/blog-1.txt b/challenge-273/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..6c5f158157 --- /dev/null +++ b/challenge-273/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task1 diff --git a/challenge-273/luca-ferrari/blog-10.txt b/challenge-273/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..8ea138d487 --- /dev/null +++ b/challenge-273/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge273.html#task2pljava diff --git a/challenge-273/luca-ferrari/blog-2.txt b/challenge-273/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..40635c9385 --- /dev/null +++ b/challenge-273/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task2 diff --git a/challenge-273/luca-ferrari/blog-3.txt b/challenge-273/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..6c9a9638bb --- /dev/null +++ b/challenge-273/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task1plperl diff --git a/challenge-273/luca-ferrari/blog-4.txt b/challenge-273/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..826cabd8d7 --- /dev/null +++ b/challenge-273/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task2plperl diff --git a/challenge-273/luca-ferrari/blog-5.txt b/challenge-273/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..b0402584ef --- /dev/null +++ b/challenge-273/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task1plpgsql diff --git a/challenge-273/luca-ferrari/blog-6.txt b/challenge-273/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..a97cf8b8b6 --- /dev/null +++ b/challenge-273/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task2plpgsql diff --git a/challenge-273/luca-ferrari/blog-7.txt b/challenge-273/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..3638fe271a --- /dev/null +++ b/challenge-273/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task1python diff --git a/challenge-273/luca-ferrari/blog-8.txt b/challenge-273/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..2c9d30467c --- /dev/null +++ b/challenge-273/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task2python diff --git a/challenge-273/luca-ferrari/blog-9.txt b/challenge-273/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..8a9060e6a2 --- /dev/null +++ b/challenge-273/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/10/PerlWeeklyChallenge273.html#task1pljava diff --git a/challenge-273/luca-ferrari/pljava/pom.xml b/challenge-273/luca-ferrari/pljava/pom.xml index 88583e06f9..ade9b2efb4 100644 --- a/challenge-273/luca-ferrari/pljava/pom.xml +++ b/challenge-273/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC272 + PWC273 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 272 with package PWC272</name> - <description>Implementation of the tasks in PL/Java for PWC 272</description> + <name>Perl Weekly Challenge 273 with package PWC273</name> + <description>Implementation of the tasks in PL/Java for PWC 273</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-273/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-273/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..be0d702144 --- /dev/null +++ b/challenge-273/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,68 @@ + + + +package PWC273; + +/** + * PL/Java implementation for PWC 273 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> + * + * + * 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/PWC273-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC273-1.jar', 'PWC273', true ); + select sqlj.set_classpath( 'public', 'PWC273' ); + + select pwc273.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC273-1.jar', 'PWC273', 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 = "pwc273", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( String s, String c ) throws SQLException { + logger.log( Level.INFO, "Entering pwc273.task1_pljava" ); + + if ( c.length() != 1 ) + throw new SQLException( "Need a single char!" ); + + + final int[] values = new int[]{ 0, 0 }; + values[ 1 ] = s.length(); + Stream.of( s.split( "" ) ) + .forEach( cc -> { + if ( cc.equals( c ) ) + values[ 0 ]++; + } ); + + return (int) Math.round( (double) values[ 0 ] / values[ 1 ] * 100 ); + } +} diff --git a/challenge-273/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-273/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..91e2f8078b --- /dev/null +++ b/challenge-273/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,60 @@ + + + +package PWC273; + +/** + * PL/Java implementation for PWC 273 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> + * + * + * 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/PWC273-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC273-1.jar', 'PWC273', true ); + select sqlj.set_classpath( 'public', 'PWC273' ); + + select pwc273.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC273-1.jar', 'PWC273', 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.regex.*; +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 = "pwc273", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task2_pljava( String s ) throws SQLException { + logger.log( Level.INFO, "Entering pwc273.task2_pljava" ); + + Pattern p1 = Pattern.compile( "b" ); + Pattern p2 = Pattern.compile( "b.*a" ); + + return p1.matcher( s ).find() && ! p2.matcher( s ).find(); + } +} diff --git a/challenge-273/luca-ferrari/plperl/ch-1.plperl b/challenge-273/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..d19899ae38 --- /dev/null +++ b/challenge-273/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,23 @@ +-- +-- Perl Weekly Challenge 273 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +-- + +CREATE SCHEMA IF NOT EXISTS pwc273; + +CREATE OR REPLACE FUNCTION +pwc273.task1_plperl( text, text ) +RETURNS int +AS $CODE$ + use POSIX; + + my ( $string, $char ) = @_; + + die "Pass a single char!" if ( length( $char ) != 1 ); + + return 0 if $string !~ / $char /x; + return POSIX::ceil( ( scalar( grep { $_ eq $char } split //, $string ) / scalar( split //, $string ) * 100 ) ); + +$CODE$ +LANGUAGE plperlu; diff --git a/challenge-273/luca-ferrari/plperl/ch-2.plperl b/challenge-273/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..ee82054085 --- /dev/null +++ b/challenge-273/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,20 @@ +-- +-- Perl Weekly Challenge 273 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +-- + +CREATE SCHEMA IF NOT EXISTS pwc273; + +CREATE OR REPLACE FUNCTION +pwc273.task2_plperl( text ) +RETURNS boolean +AS $CODE$ + + my ( $string ) = @_; + + return 1 if ( $string =~ / b /x and $string !~ / b .* a /x ); + return 0; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-273/luca-ferrari/plpgsql/ch-1.sql b/challenge-273/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..2cbb56b6d2 --- /dev/null +++ b/challenge-273/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,22 @@ +-- +-- Perl Weekly Challenge 273 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +-- + +CREATE SCHEMA IF NOT EXISTS pwc273; + +CREATE OR REPLACE FUNCTION +pwc273.task1_plpgsql( s text, c char ) +RETURNS int +AS $CODE$ + + WITH letters AS ( SELECT v FROM regexp_split_to_table( s, '' ) v ) + , matches AS ( SELECT v FROM letters v WHERE v = c ) + , c_l AS ( SELECT count(*) as x FROM letters ) + , c_m AS ( SELECT count(*) as x FROM matches ) + SELECT m.x::numeric / l.x::numeric * 100 + FROM c_l l , c_m m; + +$CODE$ +LANGUAGE sql; diff --git a/challenge-273/luca-ferrari/plpgsql/ch-2.sql b/challenge-273/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..3246c04f0e --- /dev/null +++ b/challenge-273/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,21 @@ +-- +-- Perl Weekly Challenge 273 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +-- + +CREATE SCHEMA IF NOT EXISTS pwc273; + +CREATE OR REPLACE FUNCTION +pwc273.task2_plpgsql( s text ) +RETURNS boolean +AS $CODE$ + + WITH has_b AS ( SELECT v FROM regexp_count( s, 'b' ) v ) + , has_b_without_trailing_a AS ( SELECT v FROM regexp_count( s, 'b.*a' ) v ) + SELECT CASE b.v WHEN NULL THEN false ELSE true END + FROM has_b b, has_b_without_trailing_a a + WHERE a.v = 0 AND b.v >= 1; + +$CODE$ +LANGUAGE sql; diff --git a/challenge-273/luca-ferrari/python/ch-1.py b/challenge-273/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..00e24b5173 --- /dev/null +++ b/challenge-273/luca-ferrari/python/ch-1.py @@ -0,0 +1,23 @@ +#!python + +# +# Perl Weekly Challenge 273 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + s = args[ 0 ] + c = args[ 1 ] + + return round( len( list( filter( lambda x: x == c, s ) ) ) / len( s ) * 100 ) + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-273/luca-ferrari/python/ch-2.py b/challenge-273/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..98cb99df7a --- /dev/null +++ b/challenge-273/luca-ferrari/python/ch-2.py @@ -0,0 +1,26 @@ +#!python + +# +# Perl Weekly Challenge 273 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +# + +import sys +import re + +# task implementation +# the return value will be printed +def task_2( args ): + s = args[ 0 ] + p1 = re.compile( 'b' ) + p2 = re.compile( 'b.*a' ) + return p1.search( s ) and not p2.search( s ) + + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-273/luca-ferrari/raku/ch-1.raku b/challenge-273/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..95681c6a98 --- /dev/null +++ b/challenge-273/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 273 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +# + +sub MAIN( Str $string, Str $char where { $char.chars == 1 } ) { + ( $string.comb.grep( * ~~ $char ).elems / $string.comb.elems * 100 ).Rat.round.say; +} diff --git a/challenge-273/luca-ferrari/raku/ch-2.raku b/challenge-273/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..679bc0b643 --- /dev/null +++ b/challenge-273/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,14 @@ +#!raku + +# +# Perl Weekly Challenge 273 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-273> +# + +sub MAIN( Str $string where { $string.chars > 0 } ) { + 'True'.say and exit if ( $string ~~ / b / && $string !~~ / b .* a / ); + 'False'.say; + +} |
