diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-01-29 09:55:32 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-01-29 11:13:58 +0100 |
| commit | 7c3628d24263d828b990af5eeac286e3ab7819af (patch) | |
| tree | 537d3fd7a2bc03b05575cde6fbfd0e96e728ec1b | |
| parent | 4099837cf3689e1d78a66905fde9c5a45ff95940 (diff) | |
| download | perlweeklychallenge-club-7c3628d24263d828b990af5eeac286e3ab7819af.tar.gz perlweeklychallenge-club-7c3628d24263d828b990af5eeac286e3ab7819af.tar.bz2 perlweeklychallenge-club-7c3628d24263d828b990af5eeac286e3ab7819af.zip | |
PWC 254
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 Python done
Task 2 Python done
Task 1 PL/Java done
Task2 PL/java done
26 files changed, 527 insertions, 3 deletions
diff --git a/challenge-252/luca-ferrari/java/ch-1.java b/challenge-252/luca-ferrari/java/ch-1.java new file mode 100644 index 0000000000..18ba5f7aa1 --- /dev/null +++ b/challenge-252/luca-ferrari/java/ch-1.java @@ -0,0 +1,15 @@ +package PWC252; + +public class ch_1 { + public static void main( String argv[] ) { + int sum = 0; + for ( int i = 0; i < argv.length; i++ ) { + if ( agrv.length % i != 0 ) + continue; + + sum += Integer.parseInt( argv[ i ] ) ^ 2; + } + + System.out.println( sum ); + } +} diff --git a/challenge-252/luca-ferrari/postgresql/java/pom.xml b/challenge-252/luca-ferrari/postgresql/java/pom.xml new file mode 100644 index 0000000000..8975c014a2 --- /dev/null +++ b/challenge-252/luca-ferrari/postgresql/java/pom.xml @@ -0,0 +1,84 @@ +<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> + + <!-- Above is all boilerplate. Next: your project's "Maven coordinates" --> + + <groupId>pwc</groupId> + <artifactId>pwc252</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <!-- Coordinates are nice, but so are names and descriptions for humans. --> + + <name>Perl Weekly Challenge 252</name> + <description>Implementation of the tasks in PL/Java</description> + + <!-- + Many Maven plugins care what character set encoding your files are in. + For this example I've chosen the most restrictive (US-ASCII). Change if + your files use a different encoding, but be sure not to lie. You should + be sure the encoding named here IS the way your source files are coded. + --> + + <properties> + <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> + </properties> + + <!-- Here's where you say your project depends on a pljava-api version. --> + + <dependencies> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>pljava-api</artifactId> + <version>1.6.6</version> + </dependency> + </dependencies> + + <!-- The rest here is pretty much boilerplate. --> + + <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-252/luca-ferrari/postgresql/java/src/main/java/PWC252/Task1.java b/challenge-252/luca-ferrari/postgresql/java/src/main/java/PWC252/Task1.java new file mode 100644 index 0000000000..4714df0ad5 --- /dev/null +++ b/challenge-252/luca-ferrari/postgresql/java/src/main/java/PWC252/Task1.java @@ -0,0 +1,11 @@ +package PWC252; + +import org.postgresql.pljava.annotation.Function; + +public class Task1 { + + @Function + public static final int task1( int[] nums ) { + return 0; + } +} diff --git a/challenge-253/luca-ferrari/pljava/ch-1.java b/challenge-253/luca-ferrari/pljava/ch-1.java new file mode 100644 index 0000000000..51a2428a5b --- /dev/null +++ b/challenge-253/luca-ferrari/pljava/ch-1.java @@ -0,0 +1,40 @@ +//package PWC253; + +/** + * PL/Java implementation for PWC 253 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> + * + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC253.1.jar', 'PWC253', true ); + select sqlj.set_classpath( 'public', 'PWC253' ); + + select task$tn_pljava(); + +*/ + +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.*; + +public class Task1 { + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final String[] task1_pljava( String separator, String[] words ) { + List<String> result = new LinkedList<String>(); + + for ( String w : words ) { + if ( ! w.contains( separator ) ) + result.add( w ); + else + result.addAll( w.split( separator ) ); + } + + return result; + + } +} diff --git a/challenge-253/luca-ferrari/pljava/ch-2.java b/challenge-253/luca-ferrari/pljava/ch-2.java new file mode 100644 index 0000000000..4a664fb345 --- /dev/null +++ b/challenge-253/luca-ferrari/pljava/ch-2.java @@ -0,0 +1,28 @@ +package PWC253; + +/** + * PL/Java implementation for PWC 253 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> + * + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC253.1.jar', 'PWC253', true ); + select sqlj.set_classpath( 'public', 'PWC253' ); + + select task$tn_pljava(); + +*/ + +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; + +public class Task2 { + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final void task2_pljava() throws Exception { + throws Exception( "Not Implemented" ); + } +} diff --git a/challenge-254/luca-ferrari/blog-1.txt b/challenge-254/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..3014ec9e95 --- /dev/null +++ b/challenge-254/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task1 diff --git a/challenge-254/luca-ferrari/blog-10.txt b/challenge-254/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..7fcf1cea31 --- /dev/null +++ b/challenge-254/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge254.html#task2pljava diff --git a/challenge-254/luca-ferrari/blog-2.txt b/challenge-254/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..b678fbc47d --- /dev/null +++ b/challenge-254/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task2 diff --git a/challenge-254/luca-ferrari/blog-3.txt b/challenge-254/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..e21ffb6783 --- /dev/null +++ b/challenge-254/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task1plperl diff --git a/challenge-254/luca-ferrari/blog-4.txt b/challenge-254/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..423c6c15a3 --- /dev/null +++ b/challenge-254/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task2plperl diff --git a/challenge-254/luca-ferrari/blog-5.txt b/challenge-254/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..d16e156f35 --- /dev/null +++ b/challenge-254/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task1plpgsql diff --git a/challenge-254/luca-ferrari/blog-6.txt b/challenge-254/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..83e86f8b34 --- /dev/null +++ b/challenge-254/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task2plpgsql diff --git a/challenge-254/luca-ferrari/blog-7.txt b/challenge-254/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..84b7463fba --- /dev/null +++ b/challenge-254/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task1python diff --git a/challenge-254/luca-ferrari/blog-8.txt b/challenge-254/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..f7d14503d6 --- /dev/null +++ b/challenge-254/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task2python diff --git a/challenge-254/luca-ferrari/blog-9.txt b/challenge-254/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..aa57bfbb2b --- /dev/null +++ b/challenge-254/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/29/PerlWeeklyChallenge254.html#task1pljava diff --git a/challenge-254/luca-ferrari/pljava/pom.xml b/challenge-254/luca-ferrari/pljava/pom.xml index d9047c527c..8777b204e3 100644 --- a/challenge-254/luca-ferrari/pljava/pom.xml +++ b/challenge-254/luca-ferrari/pljava/pom.xml @@ -5,11 +5,11 @@ <modelVersion>4.0.0</modelVersion> <groupId>PWC</groupId> - <artifactId>PWC253</artifactId> + <artifactId>PWC254</artifactId> <version>1</version> - <name>Perl Weekly Challenge 253</name> - <description>Implementation of the tasks in PL/Java for PWC 253</description> + <name>Perl Weekly Challenge 254</name> + <description>Implementation of the tasks in PL/Java for PWC 254</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-254/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-254/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..643a13cbab --- /dev/null +++ b/challenge-254/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,50 @@ +package PWC254; + +/** + * PL/Java implementation for PWC 254 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> + * + * + * 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/PWC253-1.jar luca@venkman:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC254-1.jar', 'PWC254', true ); + select sqlj.set_classpath( 'public', 'PWC254' ); + + select task1_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC254-1.jar', 'PWC254', 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.sql.SQLException; +import java.util.logging.*; + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final boolean task1_pljava( int num ) throws SQLException { + for ( int i = 2; i < Math.sqrt( num ); i++ ) + if ( Math.pow( i, 3 ) == num ) + return true; + + return false; + } +} diff --git a/challenge-254/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-254/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..034943bebc --- /dev/null +++ b/challenge-254/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,69 @@ +package PWC254; + +/** + * PL/Java implementation for PWC 254 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> + * + * + * 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/PWC253-1.jar luca@venkman:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC254-1.jar', 'PWC254', true ); + select sqlj.set_classpath( 'public', 'PWC254' ); + + select task1_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC254-1.jar', 'PWC254', 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.sql.SQLException; +import java.util.logging.*; + +public class Task2 { + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final String task2_pljava( String word ) throws SQLException { + String result = ""; + Stack<String> vowels = new Stack<String>(); + + for ( String letter : word.split( "" ) ) { + if ( isVowel( letter ) ) + vowels.push( letter ); + } + + for ( String letter : word.split( "" ) ) { + if ( ! isVowel( letter ) || vowels.empty() ) + result += letter; + else + result += vowels.pop(); + } + + return result; + } + + + public static final boolean isVowel( String letter ) { + return letter.toLowerCase().equals( "a" ) + || letter.toLowerCase().equals( "e" ) + || letter.toLowerCase().equals( "i" ) + || letter.toLowerCase().equals( "o" ) + || letter.toLowerCase().equals( "u" ); + } +} diff --git a/challenge-254/luca-ferrari/plperl/ch-1.plperl b/challenge-254/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..6b1db77580 --- /dev/null +++ b/challenge-254/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,23 @@ +-- +-- Perl Weekly Challenge 254 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +-- + +CREATE SCHEMA IF NOT EXISTS pwc254; + +CREATE OR REPLACE FUNCTION +pwc254.task1_plperl( int ) +RETURNS bool +AS $CODE$ + + my ( $num ) = @_; + + for ( 2 .. ( $num / 2 ) ) { + return 1 if ( $_ ** 3 == $num ); + } + + return 0; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-254/luca-ferrari/plperl/ch-2.plperl b/challenge-254/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..9deb0bdab1 --- /dev/null +++ b/challenge-254/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,27 @@ +-- +-- Perl Weekly Challenge 254 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +-- + +CREATE SCHEMA IF NOT EXISTS pwc254; + +CREATE OR REPLACE FUNCTION +pwc254.task2_plperl( text ) +RETURNS text +AS $CODE$ + + my ( $word ) = @_; + + my @vowels = reverse grep { $_ =~ / [aeiou] /ix } split( //, $word ); + my $output = ''; + + for ( split //, $word ) { + $output .= $_ and next if ( $_ !~ / [aeiou] /ix || ! @vowels ); + $output .= shift( @vowels ) and next; + } + + return $output; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-254/luca-ferrari/plpgsql/ch-1.sql b/challenge-254/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..e52a58df16 --- /dev/null +++ b/challenge-254/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 254 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +-- + +CREATE SCHEMA IF NOT EXISTS pwc254; + +CREATE OR REPLACE FUNCTION +pwc254.task1_plpgsql( n int ) +RETURNS bool +AS $CODE$ +BEGIN + FOR i IN 2 .. sqrt( n )::int LOOP + IF pow( i, 3 ) = n THEN + RETURN TRUE; + END IF; + END LOOP; + + RETURN FALSE; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-254/luca-ferrari/plpgsql/ch-2.sql b/challenge-254/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..78cb4eb8eb --- /dev/null +++ b/challenge-254/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,50 @@ +-- +-- Perl Weekly Challenge 254 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +-- + +CREATE SCHEMA IF NOT EXISTS pwc254; + +CREATE OR REPLACE FUNCTION +pwc254.task2_plpgsql( word text ) +RETURNS text +AS $CODE$ +DECLARE + output_string text := ''; + current_vowel char; + current_index int; + remaining_vowels int; + letter char; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS vowels( v char, i serial ); + TRUNCATE vowels; + + INSERT INTO vowels( v ) + SELECT v + FROM regexp_split_to_table( lower( word ), '' ) v + WHERE v IN ( 'a', 'e', 'i', 'o', 'u' ); + + FOR letter IN SELECT v FROM regexp_split_to_table( lower( word ), '' ) v LOOP + + SELECT count( * ) + FROM vowels + INTO remaining_vowels; + + IF letter NOT IN ('a', 'e', 'i', 'o', 'u' ) OR remaining_vowels = 0 THEN + output_string := output_string || letter; + ELSE + SELECT v, i + INTO current_vowel, current_index + FROM vowels + ORDER BY i DESC; + + output_string := output_string || current_vowel; + DELETE FROM vowels WHERE i = current_index; + END IF; + END LOOP; + + RETURN output_string; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-254/luca-ferrari/python/ch-1.python b/challenge-254/luca-ferrari/python/ch-1.python new file mode 100644 index 0000000000..a0533551d8 --- /dev/null +++ b/challenge-254/luca-ferrari/python/ch-1.python @@ -0,0 +1,26 @@ +#!python + +# +# Perl Weekly Challenge 254 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +# + +import sys +import math + +# task implementation +# the return value will be printed +def task_1( args ): + num = int( args[ 0 ] ) + for i in range( 2, int( math.sqrt( num ) ) ): + if ( i ** 3 ) == num: + return True + + return False + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-254/luca-ferrari/python/ch-2.python b/challenge-254/luca-ferrari/python/ch-2.python new file mode 100644 index 0000000000..dc37e2ad93 --- /dev/null +++ b/challenge-254/luca-ferrari/python/ch-2.python @@ -0,0 +1,29 @@ +#!python + +# +# Perl Weekly Challenge 254 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + word = args[ 0 ].lower() + vowels = list( reversed( list( filter( lambda x: x in ('a','e','i','o','u'), word ) ) ) ) + output = '' + for letter in word: + if letter not in ( 'a', 'e', 'i', 'o', 'u' ) or len( vowels ) == 0 + output += letter + else: + output += vowels.pop( 0 ) + + return output + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-254/luca-ferrari/raku/ch-1.raku b/challenge-254/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..48656b5e81 --- /dev/null +++ b/challenge-254/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 254 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +# + +sub MAIN( Int $n ) { + # say ( $n ** ( 1 / 3 ) ).Int == ( $n ** ( 1 / 3 ) ); + for ( 2 ..^ $n.sqrt.Int ) { + 'true'.say and exit if ( $_ ** 3 == $n ); + } + + 'false'.say; +} diff --git a/challenge-254/luca-ferrari/raku/ch-2.raku b/challenge-254/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..c72766df6f --- /dev/null +++ b/challenge-254/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!raku + +# +# Perl Weekly Challenge 254 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254> +# + +sub MAIN( Str $word ) { + my @reversed; + my @vowels.push: |$word.lc.comb.grep( * ~~ / <[aeiou]> / ).reverse; + + for $word.comb { + + @reversed.push( $_ ) and next if ( $_.lc !~~ / <[aeiou]> / || @vowels.elems == 0 ); + @reversed.push: @vowels.shift if ( @vowels.elems > 0 ); + } + + @reversed.join.say; +} |
