diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-01-22 08:54:32 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-01-22 13:21:10 +0100 |
| commit | 52e2018af75d08a9b77b8e3bb56a7b2fffc6f42b (patch) | |
| tree | 63b205906393dabcfbf3422ee6dc8ae48a3a61e7 | |
| parent | 9d7dc816f7775abfee7d90f0a2a969611902be54 (diff) | |
| download | perlweeklychallenge-club-52e2018af75d08a9b77b8e3bb56a7b2fffc6f42b.tar.gz perlweeklychallenge-club-52e2018af75d08a9b77b8e3bb56a7b2fffc6f42b.tar.bz2 perlweeklychallenge-club-52e2018af75d08a9b77b8e3bb56a7b2fffc6f42b.zip | |
PWC 253
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
Task 2 PL/Java done
21 files changed, 449 insertions, 0 deletions
diff --git a/challenge-253/luca-ferrari/blog-1.txt b/challenge-253/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..a812e9dccc --- /dev/null +++ b/challenge-253/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task1 diff --git a/challenge-253/luca-ferrari/blog-10.txt b/challenge-253/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..5b8450592a --- /dev/null +++ b/challenge-253/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge253.html#task2pljava diff --git a/challenge-253/luca-ferrari/blog-2.txt b/challenge-253/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..cbf694933d --- /dev/null +++ b/challenge-253/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task2 diff --git a/challenge-253/luca-ferrari/blog-3.txt b/challenge-253/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..d8c14d0691 --- /dev/null +++ b/challenge-253/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task1plperl diff --git a/challenge-253/luca-ferrari/blog-4.txt b/challenge-253/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..627b98ae6e --- /dev/null +++ b/challenge-253/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task2plperl diff --git a/challenge-253/luca-ferrari/blog-5.txt b/challenge-253/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..07ebc3095b --- /dev/null +++ b/challenge-253/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task1plpgsql diff --git a/challenge-253/luca-ferrari/blog-6.txt b/challenge-253/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..eecee2c6bb --- /dev/null +++ b/challenge-253/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task2plpgsql diff --git a/challenge-253/luca-ferrari/blog-7.txt b/challenge-253/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..7091a103e7 --- /dev/null +++ b/challenge-253/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task1python diff --git a/challenge-253/luca-ferrari/blog-8.txt b/challenge-253/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..2a5fe0f0da --- /dev/null +++ b/challenge-253/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task2python diff --git a/challenge-253/luca-ferrari/blog-9.txt b/challenge-253/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..09d1d4786f --- /dev/null +++ b/challenge-253/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/22/PerlWeeklyChallenge253.html#task1pljava diff --git a/challenge-253/luca-ferrari/pljava/pom.xml b/challenge-253/luca-ferrari/pljava/pom.xml new file mode 100644 index 0000000000..d9047c527c --- /dev/null +++ b/challenge-253/luca-ferrari/pljava/pom.xml @@ -0,0 +1,67 @@ +<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> + + <groupId>PWC</groupId> + <artifactId>PWC253</artifactId> + <version>1</version> + + <name>Perl Weekly Challenge 253</name> + <description>Implementation of the tasks in PL/Java for PWC 253</description> + + <properties> + <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>pljava-api</artifactId> + <version>1.6.6</version> + </dependency> + </dependencies> + + <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-253/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-253/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..af8329b557 --- /dev/null +++ b/challenge-253/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,49 @@ +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.*; +import java.util.logging.*; + +public class Task1 { + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final String[] task1_pljava( String separator, String[] words ) { + List<String> result = new LinkedList<String>(); + logger.info( "Entering task1_pljava" ); + for ( String w : words ) { + if ( ! w.contains( separator ) ) + result.add( w ); + else + System.out.println( "SPLITTO " + w ); + for ( String ww : w.split( "[" + separator + "]" ) ) { + result.add( ww ); + } + } + + return result.toArray( new String[ result.size() ] ); + } + + @Function + public static void logMessage(String logLevel, String message) { + Logger.getAnonymousLogger().log(Level.parse(logLevel), message); + } +} diff --git a/challenge-253/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-253/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..9afa1e555f --- /dev/null +++ b/challenge-253/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,75 @@ +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; + +import java.util.*; + +/* + example of invocation + + java=# select task2_pljava( 4 , array[ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ] ); + task2_pljava +-------------- + {0,2,3,1} +(1 row) + + +*/ +public class Task2 { + + @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE ) + public static final Integer[] task2_pljava( int size, int[] matrix ) throws Exception { + int row_index = 0; + Map<Integer, List<Integer>> ones = new HashMap<Integer, List<Integer>>(); + + int number_of_ones = 0; + + for ( int i = 0; i < matrix.length; i++ ) { + number_of_ones += matrix[ i ]; + if ( ( i + 1 ) % size == 0 || i == matrix.length ) { + + if ( ! ones.containsKey( number_of_ones ) ) + ones.put( number_of_ones, new LinkedList<Integer>() ); + + ones.get( number_of_ones ).add( row_index++ ); + number_of_ones = 0; + } + + + } + + List<Integer> keys = new LinkedList<Integer>( ones.keySet() ); + Collections.sort( keys ); + + List<Integer> result = new LinkedList<Integer>(); + for ( int k : keys ) { + List<Integer> values = ones.get( k ); + Collections.sort( values ); + result.addAll( values ); + } + + return result.toArray( new Integer[ result.size() ] ); + } +} diff --git a/challenge-253/luca-ferrari/plperl/ch-1.plperl b/challenge-253/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..0d75a2c6a2 --- /dev/null +++ b/challenge-253/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 253 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +-- + +CREATE SCHEMA IF NOT EXISTS pwc253; + +CREATE OR REPLACE FUNCTION +pwc253.task1_plperl( char, text[] ) +RETURNS SETOF text +AS $CODE$ + + my ( $separator, $words ) = @_; + for my $word ( $words->@* ) { + my $pattern = qr/ [$separator] /x; + + if ( $word !~ $pattern ) { + return_next( $word ); + next; + } + + return_next( $_ ) for ( split( /$pattern/, $word ) ); + } + +return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-253/luca-ferrari/plperl/ch-2.plperl b/challenge-253/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..2d7abeb5c6 --- /dev/null +++ b/challenge-253/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,38 @@ +-- +-- Perl Weekly Challenge 253 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +-- + +CREATE SCHEMA IF NOT EXISTS pwc253; + +/** + * Example of invocation + testdb=> select pwc253.task2_plperl( array[ [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] ] ); +*/ +CREATE OR REPLACE FUNCTION +pwc253.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $matrix ) = @_; + + my $ones = {}; + + for my $row_index ( 0 .. $matrix->@* - 1 ) { + my $count = scalar grep { $_ == 1 } $matrix->[ $row_index ]->@*; + push $ones->{ $count }->@*, $row_index; + } + + for my $count ( sort keys $ones->%* ) { + return_next( $_ ) for ( sort $ones->{ $count }->@* ); + } + +return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-253/luca-ferrari/plpgsql/ch-1.sql b/challenge-253/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..78911752d1 --- /dev/null +++ b/challenge-253/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 253 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +-- + +CREATE SCHEMA IF NOT EXISTS pwc253; + +CREATE OR REPLACE FUNCTION +pwc253.task1_plpgsql( s char, words text[] ) +RETURNS SETOF text +AS $CODE$ +DECLARE + current_word text; +BEGIN + FOREACH current_word IN ARRAY words LOOP + RETURN QUERY + SELECT regexp_split_to_table( current_word, '[' || s || ']' ); + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-253/luca-ferrari/plpgsql/ch-2.sql b/challenge-253/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..80971effac --- /dev/null +++ b/challenge-253/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,40 @@ +-- +-- Perl Weekly Challenge 253 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +-- + +CREATE SCHEMA IF NOT EXISTS pwc253; + +CREATE OR REPLACE FUNCTION +pwc253.task2_plpgsql( matrix int[][] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current_row int[]; + c_ones int; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS ones( r int, c int ); + TRUNCATE ones; + + FOR i IN 1 .. array_length( matrix, 1 ) LOOP + c_ones := 0; + FOR j IN 1 .. array_length( matrix, 2 ) LOOP + c_ones := c_ones + matrix[ i ][ j ]; + END LOOP; + + INSERT INTO ones + SELECT i, c_ones; + + END LOOP; + + RETURN QUERY + SELECT r + FROM ones + ORDER BY c DESC, r ASC; + + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-253/luca-ferrari/python/ch-1.python b/challenge-253/luca-ferrari/python/ch-1.python new file mode 100644 index 0000000000..07a97f09cd --- /dev/null +++ b/challenge-253/luca-ferrari/python/ch-1.python @@ -0,0 +1,28 @@ +#!python + +# +# Perl Weekly Challenge 253 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +# + +import sys +import re + +# task implementation +# the return value will be printed +def task_1( args ): + separator = args[ 0 ]; + split_words = [] + for i in range( 1, len( args ) ): + current_words = args[ i ] + for w in re.split( "[%s]" % separator, current_words ): + split_words.append( w ) + + return ','.join( split_words ) + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-253/luca-ferrari/python/ch-2.python b/challenge-253/luca-ferrari/python/ch-2.python new file mode 100644 index 0000000000..2e4abff0e5 --- /dev/null +++ b/challenge-253/luca-ferrari/python/ch-2.python @@ -0,0 +1,49 @@ +#!python + +# +# Perl Weekly Challenge 253 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +# + +import sys +import collections + +# task implementation +# the return value will be printed +def task_2( args ): + ones = {} + for row_index in range( 0, len( args ) ): + sum = 0 + for x in args[ row_index ]: + sum += x + + if not sum in ones: + ones[ str( sum ) ] = [] + + ones[ str( sum ) ].append( row_index ) + + keys = list( ones.keys() ) + keys.sort() + result = "" + for k in keys: + ones[ k ].sort() + for v in ones[ k ]: + result += str( v ) + ',' + + + return result + + + +# invoke the main without the command itself +if __name__ == '__main__': + matrix = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ] + print( task_2( matrix ) ) diff --git a/challenge-253/luca-ferrari/raku/ch-1.raku b/challenge-253/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..e907262eea --- /dev/null +++ b/challenge-253/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 253 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +# + +sub MAIN( $separator, *@words ) { + @words.split( $separator, :skip-empty ).join( ',' ).say; +} diff --git a/challenge-253/luca-ferrari/raku/ch-2.raku b/challenge-253/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..2d0a7da4e5 --- /dev/null +++ b/challenge-253/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!raku + +# +# Perl Weekly Challenge 253 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-253> +# + +sub MAIN() { + + my $matrix = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ]; + + my %ones; + for 0 ..^ $matrix.elems -> $row_index { + my $count = $matrix[ $row_index ].grep( * ~~ 1 ).elems; + %ones{ $count }.push: $row_index; + } + + + my @rows.push: |%ones{ $_ }.sort for %ones.keys.sort; + @rows.join( ', ').say; +} |
