From ab26905beb70d890b8e43bbb1ae0e06a00a22491 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Fri, 20 Sep 2024 09:38:38 +0200 Subject: PWC 287 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Task 1 PL/Java done Task 2 PL/Java done Task 1 Python done Task 2 Python done --- challenge-287/luca-ferrari/blog-1.txt | 1 + challenge-287/luca-ferrari/blog-10.txt | 1 + challenge-287/luca-ferrari/blog-2.txt | 1 + challenge-287/luca-ferrari/blog-3.txt | 1 + challenge-287/luca-ferrari/blog-4.txt | 1 + challenge-287/luca-ferrari/blog-5.txt | 1 + challenge-287/luca-ferrari/blog-6.txt | 1 + challenge-287/luca-ferrari/blog-7.txt | 1 + challenge-287/luca-ferrari/blog-8.txt | 1 + challenge-287/luca-ferrari/blog-9.txt | 1 + challenge-287/luca-ferrari/pljava/pom.xml | 72 ++++++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task1.java | 67 ++++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task2.java | 58 +++++++++++++++++ challenge-287/luca-ferrari/plperl/ch-1.plperl | 24 ++++++++ challenge-287/luca-ferrari/plperl/ch-2.plperl | 20 ++++++ challenge-287/luca-ferrari/plpgsql/ch-1.sql | 58 +++++++++++++++++ challenge-287/luca-ferrari/plpgsql/ch-2.sql | 28 +++++++++ challenge-287/luca-ferrari/python/ch-1.py | 25 ++++++++ challenge-287/luca-ferrari/python/ch-2.py | 22 +++++++ challenge-287/luca-ferrari/raku/ch-1.raku | 26 ++++++++ challenge-287/luca-ferrari/raku/ch-2.raku | 18 ++++++ 21 files changed, 428 insertions(+) create mode 100644 challenge-287/luca-ferrari/blog-1.txt create mode 100644 challenge-287/luca-ferrari/blog-10.txt create mode 100644 challenge-287/luca-ferrari/blog-2.txt create mode 100644 challenge-287/luca-ferrari/blog-3.txt create mode 100644 challenge-287/luca-ferrari/blog-4.txt create mode 100644 challenge-287/luca-ferrari/blog-5.txt create mode 100644 challenge-287/luca-ferrari/blog-6.txt create mode 100644 challenge-287/luca-ferrari/blog-7.txt create mode 100644 challenge-287/luca-ferrari/blog-8.txt create mode 100644 challenge-287/luca-ferrari/blog-9.txt create mode 100644 challenge-287/luca-ferrari/pljava/pom.xml create mode 100644 challenge-287/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-287/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-287/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-287/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-287/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-287/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-287/luca-ferrari/python/ch-1.py create mode 100644 challenge-287/luca-ferrari/python/ch-2.py create mode 100644 challenge-287/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-287/luca-ferrari/raku/ch-2.raku (limited to 'challenge-287') diff --git a/challenge-287/luca-ferrari/blog-1.txt b/challenge-287/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..177d8ad1bb --- /dev/null +++ b/challenge-287/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task1 diff --git a/challenge-287/luca-ferrari/blog-10.txt b/challenge-287/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..9045d03914 --- /dev/null +++ b/challenge-287/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task2pljava diff --git a/challenge-287/luca-ferrari/blog-2.txt b/challenge-287/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..62ef4b21a1 --- /dev/null +++ b/challenge-287/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task2 diff --git a/challenge-287/luca-ferrari/blog-3.txt b/challenge-287/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..5e0da9daab --- /dev/null +++ b/challenge-287/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task1plperl diff --git a/challenge-287/luca-ferrari/blog-4.txt b/challenge-287/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..935fd661a7 --- /dev/null +++ b/challenge-287/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task2plperl diff --git a/challenge-287/luca-ferrari/blog-5.txt b/challenge-287/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..79287d10a4 --- /dev/null +++ b/challenge-287/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task1plpgsql diff --git a/challenge-287/luca-ferrari/blog-6.txt b/challenge-287/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..48482f6491 --- /dev/null +++ b/challenge-287/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task2plpgsql diff --git a/challenge-287/luca-ferrari/blog-7.txt b/challenge-287/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..65e3ad7cd5 --- /dev/null +++ b/challenge-287/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task1python diff --git a/challenge-287/luca-ferrari/blog-8.txt b/challenge-287/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..fe1cb2f2bd --- /dev/null +++ b/challenge-287/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task2python diff --git a/challenge-287/luca-ferrari/blog-9.txt b/challenge-287/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..4653f51056 --- /dev/null +++ b/challenge-287/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/20/PerlWeeklyChallenge287.html#task1pljava diff --git a/challenge-287/luca-ferrari/pljava/pom.xml b/challenge-287/luca-ferrari/pljava/pom.xml new file mode 100644 index 0000000000..47fa2f66a2 --- /dev/null +++ b/challenge-287/luca-ferrari/pljava/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + PWC + + PWC287 + + + 1 + + + Perl Weekly Challenge 287 with package PWC287 + Implementation of the tasks in PL/Java for PWC 287 + + + US-ASCII + + + + + org.postgresql + pljava-api + 1.6.6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 9 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + + + + true + + + + + + + pljava.ddr + + + true + + + + + + + + + + diff --git a/challenge-287/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-287/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..8990641a74 --- /dev/null +++ b/challenge-287/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,67 @@ + + + +package PWC287; + +/** + * PL/Java implementation for PWC 287 + * Task 1 + * See + * + * + * 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/PWC287-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC287-1.jar', 'PWC287', true ); + select sqlj.set_classpath( 'public', 'PWC287' ); + + select pwc287.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC287-1.jar', 'PWC287', 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.Matcher; +import java.util.regex.Pattern; + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc287", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task1_pljava( String password ) throws SQLException { + if ( password.length() < 6 ) + return false; + + Pattern lowerCase = Pattern.compile( "[a-z]" ); + Pattern upperCase = Pattern.compile( "[A-Z]" ); + Pattern digit = Pattern.compile( "[0-9]" ); + Pattern repetitions = Pattern.compile( "(.)\\1\\1" ); + + return lowerCase.matcher( password ).find() + && upperCase.matcher( password ).find() + && digit.matcher( password ).find() + && ! repetitions.matcher( password ).find(); + } +} diff --git a/challenge-287/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-287/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..0c3a926c16 --- /dev/null +++ b/challenge-287/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,58 @@ + + + +package PWC287; + +/** + * PL/Java implementation for PWC 287 + * Task 2 + * See + * + * + * 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/PWC287-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC287-1.jar', 'PWC287', true ); + select sqlj.set_classpath( 'public', 'PWC287' ); + + select pwc287.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC287-1.jar', 'PWC287', 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.Matcher; +import java.util.regex.Pattern; + + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc287", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task2_pljava( String number ) throws SQLException { + Pattern numberRegexp = Pattern.compile( "^[+-]?\\d+([.]\\d+)?(E[+-]?\\d+)?$" ); + return numberRegexp.matcher( number ).find(); + } +} diff --git a/challenge-287/luca-ferrari/plperl/ch-1.plperl b/challenge-287/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..8b8395a7d7 --- /dev/null +++ b/challenge-287/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 287 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc287; + +CREATE OR REPLACE FUNCTION +pwc287.task1_plperl( text ) +RETURNS bool +AS $CODE$ + + my ( $password ) = @_; + return 0 if ( length( $password ) < 6 ); + return 0 if ( $password !~ / [a-z] /x ); + return 0 if ( $password !~ / [A-Z] /x ); + return 0 if ( $password !~ / [0-9] /x ); + return 0 if ( $password ~~ / (.)\1\1 /x ); + + return 1; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-287/luca-ferrari/plperl/ch-2.plperl b/challenge-287/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..bd36941088 --- /dev/null +++ b/challenge-287/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,20 @@ +-- +-- Perl Weekly Challenge 287 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc287; + +CREATE OR REPLACE FUNCTION +pwc287.task2_plperl( text ) +RETURNS bool +AS $CODE$ + + my ( $number ) = @_; + + return 1 if ( $number =~ / ^ [+-]? \d+ (.\d+)? (E[+-]?\d+)? $/x ); + return 0; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-287/luca-ferrari/plpgsql/ch-1.sql b/challenge-287/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..2df3b950cc --- /dev/null +++ b/challenge-287/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,58 @@ +-- +-- Perl Weekly Challenge 287 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc287; + +CREATE OR REPLACE FUNCTION +pwc287.task1_plpgsql( pwd text ) +RETURNS bool +AS $CODE$ +DECLARE + counter int := 0; +BEGIN + + IF length( pwd ) < 6 THEN + RETURN false; + END IF; + + SELECT count( x ) + INTO counter + FROM regexp_matches( pwd, '[a-z]', 'g' ) x; + + IF counter <= 0 THEN + RETURN false; + END IF; + + SELECT count( x ) + INTO counter + FROM regexp_matches( pwd, '[A-Z]', 'g' ) x; + + IF counter <= 0 THEN + RETURN false; + END IF; + + SELECT count( x ) + INTO counter + FROM regexp_matches( pwd, '[0-9]', 'g' ) x; + + IF counter <= 0 THEN + RETURN false; + END IF; + + + SELECT count( x ) + INTO counter + FROM regexp_matches( pwd, '(.)\1\1', 'g' ) x; + + IF counter > 0 THEN + RETURN false; + END IF; + + + RETURN true; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-287/luca-ferrari/plpgsql/ch-2.sql b/challenge-287/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..73ce87787d --- /dev/null +++ b/challenge-287/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 287 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc287; + +CREATE OR REPLACE FUNCTION +pwc287.task2_plpgsql( n text ) +RETURNS bool +AS $CODE$ +DECLARE + counter int := 0; +BEGIN + + SELECT count( x ) + INTO counter + FROM regexp_matches( n, '^[+-]?\d+(.\d+)?(E[+-]?\d+)?$' ) x; + + IF counter > 0 THEN + RETURN true; + ELSE + RETURN false; + END IF; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-287/luca-ferrari/python/ch-1.py b/challenge-287/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..2fbe66f7b9 --- /dev/null +++ b/challenge-287/luca-ferrari/python/ch-1.py @@ -0,0 +1,25 @@ +#!python + +# +# Perl Weekly Challenge 287 +# Task 1 +# +# See +# + +import sys +import re + +# task implementation +# the return value will be printed +def task_1( args ): + password = args[ 0 ] + lc = re.compile( '[a-z]' ) + uc = re.compile( '[A-Z]' ) + dg = re.compile( '[0-9]' ) + wrong = re.compile( '(.)\\1\\1' ) + return lc.search( password ) and uc.search( password ) and dg.search( password ) and wrong.search( password ) is None + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-287/luca-ferrari/python/ch-2.py b/challenge-287/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..d76523ff39 --- /dev/null +++ b/challenge-287/luca-ferrari/python/ch-2.py @@ -0,0 +1,22 @@ +#!python + +# +# Perl Weekly Challenge 287 +# Task 2 +# +# See +# + +import sys +import re + +# task implementation +# the return value will be printed +def task_2( args ): + number = args[ 0 ] + good = re.compile( '^[+-]?\\d+([.]\\d+)?(E[+-]?\\d+)?$' ) + return good.match( number ) is not None + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-287/luca-ferrari/raku/ch-1.raku b/challenge-287/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..0fecbc01c3 --- /dev/null +++ b/challenge-287/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!raku + +# +# Perl Weekly Challenge 287 +# Task 1 +# +# See +# + +sub MAIN( Str :$password ) { +# - It must have at least 6 characters. +# - It must contains at least one lowercase letter, at least one upper case letter and at least one digit. + # - It shouldn't contain 3 repeating characters in a row. + + my $ok = True; + 'Too short'.say and $ok = False if ( $password.chars < 6 ); + 'At least one lowercase char'.say and $ok = False if ( $password !~~ / <[a .. z]> / ); + 'At least one uppercase char'.say and $ok = False if ( $password !~~ / <[A .. Z]> / ); + 'At least one digit char'.say and $ok = False if ( $password !~~ / <[0 .. 9]> / ); + 'No three repetitions in a row'.say and $ok = False if ( $password ~~ / (<[a..zA..Z0..9]>)$0$0 / ); + + + 'Strong enough'.say if ( $ok ); + 'Weak!'.say if ( ! $ok ); + exit( $ok ?? 0 !! 1 ); +} diff --git a/challenge-287/luca-ferrari/raku/ch-2.raku b/challenge-287/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..458e0965ca --- /dev/null +++ b/challenge-287/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,18 @@ +#!raku + +# +# Perl Weekly Challenge 287 +# Task 2 +# +# See +# + +sub MAIN( Str $number ) { +# - An integer number followed by an optional exponent. +# - A decimal number followed by an optional exponent. + # - An integer number is defined with an optional sign '-' or '+' followed by digits. + + 'False'.say and exit( 1 ) if ( $number !~~ / ^ <[+-]>? \d+ (.\d+)? (E<[+-]>?\d+)? $ / ); + 'True'.say; + +} -- cgit