diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-06-06 10:26:38 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-06-06 13:39:23 +0200 |
| commit | 56cd4be064ea930654c9b199442e7b083c0ed73a (patch) | |
| tree | 6831654fa8fdf54c60936c8b0329c668eccb1ce8 | |
| parent | 48d0e34525b4183b4943fd8108e14755bf96e6d4 (diff) | |
| download | perlweeklychallenge-club-56cd4be064ea930654c9b199442e7b083c0ed73a.tar.gz perlweeklychallenge-club-56cd4be064ea930654c9b199442e7b083c0ed73a.tar.bz2 perlweeklychallenge-club-56cd4be064ea930654c9b199442e7b083c0ed73a.zip | |
PWC 272
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
21 files changed, 317 insertions, 3 deletions
diff --git a/challenge-272/luca-ferrari/blog-1.txt b/challenge-272/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..e7579f5b4b --- /dev/null +++ b/challenge-272/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1 diff --git a/challenge-272/luca-ferrari/blog-10.txt b/challenge-272/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..eda35729fd --- /dev/null +++ b/challenge-272/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge272.html#task2pljava diff --git a/challenge-272/luca-ferrari/blog-2.txt b/challenge-272/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..fc64e99089 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2 diff --git a/challenge-272/luca-ferrari/blog-3.txt b/challenge-272/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..6cb55cedef --- /dev/null +++ b/challenge-272/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1plperl diff --git a/challenge-272/luca-ferrari/blog-4.txt b/challenge-272/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..cb1275772f --- /dev/null +++ b/challenge-272/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2plperl diff --git a/challenge-272/luca-ferrari/blog-5.txt b/challenge-272/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..86a012d6e5 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1plpgsql diff --git a/challenge-272/luca-ferrari/blog-6.txt b/challenge-272/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..403db8c903 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2plpgsql diff --git a/challenge-272/luca-ferrari/blog-7.txt b/challenge-272/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..c2fb30cccc --- /dev/null +++ b/challenge-272/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1python diff --git a/challenge-272/luca-ferrari/blog-8.txt b/challenge-272/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..63130d622d --- /dev/null +++ b/challenge-272/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task2python diff --git a/challenge-272/luca-ferrari/blog-9.txt b/challenge-272/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..ce0f7adec4 --- /dev/null +++ b/challenge-272/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/06/PerlWeeklyChallenge272.html#task1pljava diff --git a/challenge-272/luca-ferrari/pljava/pom.xml b/challenge-272/luca-ferrari/pljava/pom.xml index bf11a0cf59..88583e06f9 100644 --- a/challenge-272/luca-ferrari/pljava/pom.xml +++ b/challenge-272/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC271 + PWC272 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 271 with package PWC271</name> - <description>Implementation of the tasks in PL/Java for PWC 271</description> + <name>Perl Weekly Challenge 272 with package PWC272</name> + <description>Implementation of the tasks in PL/Java for PWC 272</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-272/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-272/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..8dddbce6c0 --- /dev/null +++ b/challenge-272/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,56 @@ + + + +package PWC272; + +/** + * PL/Java implementation for PWC 272 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> + * + * + * 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/PWC272-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', true ); + select sqlj.set_classpath( 'public', 'PWC272' ); + + select pwc272.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', 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 = "pwc272", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String task1_pljava( String ip ) throws SQLException { + logger.log( Level.INFO, "Entering pwc272.task1_pljava" ); + + return ip.replaceAll( "\\.", "[.]" ); + } +} diff --git a/challenge-272/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-272/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..4002c64b9d --- /dev/null +++ b/challenge-272/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,63 @@ + + + +package PWC272; + +/** + * PL/Java implementation for PWC 272 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> + * + * + * 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/PWC272-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', true ); + select sqlj.set_classpath( 'public', 'PWC272' ); + + select pwc272.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC272-1.jar', 'PWC272', 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 = "pwc272", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( String string ) throws SQLException { + logger.log( Level.INFO, "Entering pwc272.task2_pljava" ); + + int score = 0; + + for ( int i = 0; i < string.length() - 1; i++ ) { + int diff = string.charAt( i ) - string.charAt( i + 1 ); + score += diff > 0 ? diff : diff * -1; + } + + return score; + } +} diff --git a/challenge-272/luca-ferrari/plperl/ch-1.plperl b/challenge-272/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..a82a3f0e38 --- /dev/null +++ b/challenge-272/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,22 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task1_plperl( text ) +RETURNS text +AS $CODE$ + + my ( $ip ) = @_; + + die "IP not valid" if ( $ip !~ / ^ ( \d {1,3} [.] ) {3} \d {1,3} $ /x ); + + $ip =~ s/[.]/\[\.\]/g; + return $ip; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-272/luca-ferrari/plperl/ch-2.plperl b/challenge-272/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..2085b3b3bd --- /dev/null +++ b/challenge-272/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,26 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task2_plperl( text ) +RETURNS int +AS $CODE$ + + my ( $string ) = @_; + + my $score = 0; + my @chars = split //, $string; + + for my $index ( 0 .. $#chars - 1 ) { + my $diff = ord( $chars[ $index ] ) - ord( $chars[ $index + 1 ] ); + $score += $diff > 0 ? $diff : $diff * -1; + } + + return $score; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-272/luca-ferrari/plpgsql/ch-1.sql b/challenge-272/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..588bd9b572 --- /dev/null +++ b/challenge-272/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task1_plpgsql( addr text ) +RETURNS text +AS $CODE$ + SELECT + regexp_replace( addr, '\.', '[.]', 'g' ); +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-272/luca-ferrari/plpgsql/ch-2.sql b/challenge-272/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..b044a9d85c --- /dev/null +++ b/challenge-272/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 272 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +-- + +CREATE SCHEMA IF NOT EXISTS pwc272; + +CREATE OR REPLACE FUNCTION +pwc272.task2_plpgsql( s text ) +RETURNS int +AS $CODE$ +DECLARE + score int; + previous int; + v int; +BEGIN + + previous := NULL; + score := 0; + FOR v IN SELECT ascii( x )::int FROM regexp_split_to_table( s, '' ) x LOOP + IF previous IS NOT NULL THEN + score := score + abs( previous - v::int ); + END IF; + + previous := v::int; + + END LOOP; + + RETURN score; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-272/luca-ferrari/python/ch-1.py b/challenge-272/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..02c5837ce1 --- /dev/null +++ b/challenge-272/luca-ferrari/python/ch-1.py @@ -0,0 +1,20 @@ +#!python + +# +# Perl Weekly Challenge 272 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + return args[ 0 ].replace( '.', '[.]' ) + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-272/luca-ferrari/python/ch-2.py b/challenge-272/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..0a8a0001f4 --- /dev/null +++ b/challenge-272/luca-ferrari/python/ch-2.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 272 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + score = 0 + string = args[ 0 ] + previous = None + + for x in string: + if previous is not None: + diff = ord( x ) - ord( previous ) + if diff < 0: + diff *= -1 + + score += diff + + previous = x + + return score + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-272/luca-ferrari/raku/ch-1.raku b/challenge-272/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..4966334941 --- /dev/null +++ b/challenge-272/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!raku + +# +# Perl Weekly Challenge 272 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +# + +sub MAIN( Str $ip is copy + where { $ip ~~ /^ ( \d ** 1..3 '.' ) ** 3 \d ** 1..3 / } ) { + + $ip .= subst( '.', '[.]', :g ); + $ip.say; +} diff --git a/challenge-272/luca-ferrari/raku/ch-2.raku b/challenge-272/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..75cd2e7b8e --- /dev/null +++ b/challenge-272/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 272 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-272> +# + +sub MAIN( Str $string where { $string.elems > 0 } ) { + my $score = 0; + my @letters = $string.comb; + + for 0 ..^ @letters.elems - 1 -> $index { + $score += abs( @letters[ $index ].Str.ord - @letters[ $index + 1 ].Str.ord ); + } + + $score.say; +} |
