diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-05-20 15:09:14 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-05-22 10:11:31 +0200 |
| commit | 4e32b6d7e4b63219b58bc5cab8a8402b33edf0ea (patch) | |
| tree | 42f4335ae9ba58a27fe0ecb3598e47ab0d906452 | |
| parent | ed462bf99ed6fda013ab14d58855951ef13b05fa (diff) | |
| download | perlweeklychallenge-club-4e32b6d7e4b63219b58bc5cab8a8402b33edf0ea.tar.gz perlweeklychallenge-club-4e32b6d7e4b63219b58bc5cab8a8402b33edf0ea.tar.bz2 perlweeklychallenge-club-4e32b6d7e4b63219b58bc5cab8a8402b33edf0ea.zip | |
PWC 270
Task 1 Raku done
Task 2 Raku done
Task 1 PL/Perl done
Task 2 PL/Perl done
Task 1 and 2 PL/PgSQL done using PL/Perl
Task 1 Python done
Task 2 Python done
Task 1 Java done
Task 2 Java done
21 files changed, 484 insertions, 3 deletions
diff --git a/challenge-270/luca-ferrari/blog-1.txt b/challenge-270/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..3ff06d4523 --- /dev/null +++ b/challenge-270/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1 diff --git a/challenge-270/luca-ferrari/blog-10.txt b/challenge-270/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..a7d43eeb0f --- /dev/null +++ b/challenge-270/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge270.html#task2pljava diff --git a/challenge-270/luca-ferrari/blog-2.txt b/challenge-270/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..599b6e1514 --- /dev/null +++ b/challenge-270/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2 diff --git a/challenge-270/luca-ferrari/blog-3.txt b/challenge-270/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..ed515527ef --- /dev/null +++ b/challenge-270/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1plperl diff --git a/challenge-270/luca-ferrari/blog-4.txt b/challenge-270/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..c6e10ace9a --- /dev/null +++ b/challenge-270/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2plperl diff --git a/challenge-270/luca-ferrari/blog-5.txt b/challenge-270/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..abb7db2c9b --- /dev/null +++ b/challenge-270/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1plpgsql diff --git a/challenge-270/luca-ferrari/blog-6.txt b/challenge-270/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..d9a8d4be66 --- /dev/null +++ b/challenge-270/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2plpgsql diff --git a/challenge-270/luca-ferrari/blog-7.txt b/challenge-270/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..dbdb33d49a --- /dev/null +++ b/challenge-270/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1python diff --git a/challenge-270/luca-ferrari/blog-8.txt b/challenge-270/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..80a4ca20ad --- /dev/null +++ b/challenge-270/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2python diff --git a/challenge-270/luca-ferrari/blog-9.txt b/challenge-270/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..5cb1b54dfa --- /dev/null +++ b/challenge-270/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1pljava diff --git a/challenge-270/luca-ferrari/pljava/pom.xml b/challenge-270/luca-ferrari/pljava/pom.xml index b5fe1e2ab4..4de418afd7 100644 --- a/challenge-270/luca-ferrari/pljava/pom.xml +++ b/challenge-270/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC269 + PWC270 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 269 with package PWC269</name> - <description>Implementation of the tasks in PL/Java for PWC 269</description> + <name>Perl Weekly Challenge 270 with package PWC270</name> + <description>Implementation of the tasks in PL/Java for PWC 270</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-270/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-270/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..5a49f2024d --- /dev/null +++ b/challenge-270/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,99 @@ + + + +package PWC270; + +/** + * PL/Java implementation for PWC 270 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> + * + * + * 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/PWC270-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', true ); + select sqlj.set_classpath( 'public', 'PWC270' ); + + select pwc270.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', 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 = "pwc270", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String[] task1_pljava( int row_size, int[] matrix ) throws SQLException { + logger.log( Level.INFO, "Entering pwc270.task1_pljava" ); + + List<Integer> rows = new LinkedList<Integer>(); + List<Integer> cols = new LinkedList<Integer>(); + List<String> result = new LinkedList<String>(); + + // seek for ones + for( int row = 0; row < ( matrix.length / row_size ) ; row++ ) + for ( int col = 0; col < row_size; col++ ) + if ( matrix[ row * row_size + col ] == 1 ) { + rows.add( row ); + cols.add( col ); + } + + for ( int i = 0; i < rows.size(); i++ ) { + int current_row = rows.get( i ); + int current_col = rows.get( i ); + boolean ok = true; + + for ( int r = 0; r < rows.size(); r++ ) + if ( r == i ) + continue; + else if ( rows.get( r ) == current_row ) + ok = false; + + for ( int c = 0; c < cols.size(); c++ ) + if ( c == i ) + continue; + else if ( cols.get( c ) == current_col ) + ok = false; + + + if ( ok ) + result.add( String.format( "Row %d Col %d", current_row, current_col ) ); + } + + String[] to_return = new String[ result.size() ]; + int i = 0; + for ( String s : result ) + to_return[ i++ ] = s; + + return to_return; + + } +} diff --git a/challenge-270/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-270/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..5735002d25 --- /dev/null +++ b/challenge-270/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,92 @@ + + + +package PWC270; + +/** + * PL/Java implementation for PWC 270 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> + * + * + * 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/PWC270-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', true ); + select sqlj.set_classpath( 'public', 'PWC270' ); + + select pwc270.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', 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 = "pwc270", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( int score_single, int score_double, int[] numbers ) throws SQLException { + logger.log( Level.INFO, "Entering pwc270.task2_pljava" ); + + int score = 0; + List<Integer> nums = Arrays.stream( numbers ).boxed().collect( Collectors.toList() ); + + // get the max + Integer max = nums.stream().max( Integer::compare ).get(); + + List<Integer> needs_operation = new LinkedList<Integer>(); + do { + needs_operation.clear(); + IntStream + .range( 0, nums.size() - 1 ) + .forEach( index -> { + if ( nums.get( index ) < max ) + needs_operation.add( index ); + } ); + + + if ( needs_operation.size() == 1 ) { + score += score_single; + int index = needs_operation.get( 0 ); + int value = nums.get( index ); + nums.set( index, ++value ); + } + else if ( needs_operation.size() >= 2 ) { + score += score_double; + + for ( int i = 0; i < 2; i ++ ) { + int index = needs_operation.get( i ); + int value = nums.get( index ); + nums.set( index, ++value ); + } + } + + } while ( ! needs_operation.isEmpty() ); + + return score; + + } +} diff --git a/challenge-270/luca-ferrari/plperl/ch-1.plperl b/challenge-270/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..ef65b0bdcb --- /dev/null +++ b/challenge-270/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,41 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +/** +testdb=> select distinct( r, c ) from pwc270.task1_plperl( array[ [ 1, 0, 0 ], [ 1, 0, 1 ], [0, 0 , 0 ] ] ); + row +------- + (1,2) +(1 row) + +*/ +CREATE OR REPLACE FUNCTION +pwc270.task1_plperl( int[][] ) +RETURNS TABLE( r int, c int ) +AS $CODE$ + + my ( $matrix ) = @_; + my @pnes; + + for my $row ( 0 .. $matrix->@* -1 ) { + for my $column ( 0 .. $matrix->@[ $row ]->@* -1 ) { + next if ( $matrix->@[ $row ]->@[ $column ] != 1 ); + push @ones, [ $row, $column, sprintf( '%02d-%02d', $row, $column ) ]; + } + } + + for my $position ( @ones ) { + if ( grep( { $position->[ 2 ] != $_->[ 2] && ( $position->[ 0 ] == $_->[ 0 ] || $position->[ 1 ] == $_->[ 1 ] ) } @ones ) == 0 ) { + return_next( { r => $position->@[ 0 ], c => $position->@[ 1 ] } ); + } + } + + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-270/luca-ferrari/plperl/ch-2.plperl b/challenge-270/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..e3579a141c --- /dev/null +++ b/challenge-270/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,56 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +CREATE OR REPLACE FUNCTION +pwc270.task2_plperl( int, int, int[] ) +RETURNS int +AS $CODE$ + + my ( $single, $double, $nums ) = @_; + my $score = 0; + + my $max = sub { + my ( $nums ) = @_; + my $value = undef; + + for ( $nums->@* ) { + $value = $_ if ( ! $value || $value < $_ ); + } + + return $value; + }; + + my $current_max = $max->( $nums ); + my @need_operation; + + + for ( 0 .. $nums->@* - 1 ) { + push @need_operation, $_ if ( $nums->@[ $_ ] < $current_max ); + } + + while ( @need_operation > 0 ) { + if ( @need_operation > 1 ) { + $nums->[ $need_operation[ 0 ] ]++; + $nums->[ $need_operation[ 1 ] ]++; + $score += $double; + } + elsif ( @need_operation == 1 ) { + $nums->[ $need_operation[ 0 ] ]++; + $score += $single; + } + + @need_operation = (); + for ( 0 .. $nums->@* - 1 ) { + push @need_operation, $_ if ( $nums->@[ $_ ] < $current_max ); + } + } + + return $score; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-270/luca-ferrari/plpgsql/ch-1.sql b/challenge-270/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..acd32b41cc --- /dev/null +++ b/challenge-270/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +CREATE OR REPLACE FUNCTION +pwc270.task1_plpgsql( matrix int[][] ) +RETURNS SETOF int[] +AS $CODE$ + SELECT pwc270.task1_plperl( matrix ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-270/luca-ferrari/plpgsql/ch-2.sql b/challenge-270/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..b2e37dae7e --- /dev/null +++ b/challenge-270/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +CREATE OR REPLACE FUNCTION +pwc270.task2_plpgsql( s int, d int, nums int[] ) +RETURNS int +AS $CODE$ + SELECT pwc270.task2_plperl( s, d, nums ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-270/luca-ferrari/python/ch-1.py b/challenge-270/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..bb4786c9bb --- /dev/null +++ b/challenge-270/luca-ferrari/python/ch-1.py @@ -0,0 +1,46 @@ +#!python + +# +# Perl Weekly Challenge 270 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + row_size = int( args[ 0 ] ) + matrix = [] + + # build the matrix + current_row = 0 + matrix.append( [] ) + for x in args[ 1: ]: + + if len( matrix[ current_row ] ) >= row_size: + current_row += 1 + matrix.append( [] ) + + matrix[ current_row ].append( int( x ) ) + + + ones = [] + for r in range( 0, len( matrix ) ): + for c in range( 0, len( matrix[ r ] ) ): + if matrix[ r ][ c ] == 1: + ones.append( [ r, c ] ) + + for current in ones: + found = list( filter( lambda x: x[ 0 ] == current[ 0 ] or x[ 1 ] == current[ 1 ], ones ) ) + if len( found ) == 1: + print( current ) + + + return "" + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-270/luca-ferrari/python/ch-2.py b/challenge-270/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..1b9794bb2e --- /dev/null +++ b/challenge-270/luca-ferrari/python/ch-2.py @@ -0,0 +1,45 @@ +#!python + +# +# Perl Weekly Challenge 270 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + single = int( args[ 0 ] ) + double = int( args[ 1 ] ) + nums = list( map( int, args[ 2: ] ) ) + + # compute the current max + max = None + for v in nums: + if not max or v > max: + max = v + + need_operation = list( filter( lambda x: nums[ x ] < max, range( 0, len( nums ) ) ) ) + score = 0 + while len( need_operation ) > 0: + if len( need_operation ) > 1 : + score += double + nums[ need_operation[ 0 ] ] += 1 + nums[ need_operation[ 1 ] ] += 1 + need_operation = need_operation[ 2: ] + elif len( need_operation ) == 1: + score += single + nums[ need_operation[ 0 ] ] += 1 + need_operation = need_operation[ 2: ] + + + return score + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-270/luca-ferrari/raku/ch-1.raku b/challenge-270/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..490b3b4679 --- /dev/null +++ b/challenge-270/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!raku + +# +# Perl Weekly Challenge 270 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +# + +sub MAIN() { + my @matrix = [1, 0, 0], + [0, 0, 1], + [1, 0, 0], + ; + + my @ones; + for 0 ..^ @matrix.elems -> $row { + @ones.push: @matrix[ $row ].grep( { $_ == 1 }, :k ).map( { $row, $_, '%02d-%02d'.sprintf( $row, $_ ) } ).flat; + } + + + for @ones -> $current_one { + $current_one[ 0, 1 ].join( ',' ).say if @ones.grep( { $current_one[ 2 ] ne $_[ 2 ] && ( $_[ 1 ] == $current_one[ 1 ] || $_[ 0 ] == $current_one[ 0 ] ) } ).elems == 0 + } + +} diff --git a/challenge-270/luca-ferrari/raku/ch-2.raku b/challenge-270/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..b5ca114def --- /dev/null +++ b/challenge-270/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!raku + +# +# Perl Weekly Challenge 270 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-270> +# + +sub MAIN( Int $single, + Int $double, + *@nums is copy where { @nums.elems == @nums.grep( * ~~ Int ).elems } ) { + + my $current_max = @nums.max; + my @need_operation = @nums.grep( * < $current_max, :k ); + my $score = 0; + + while ( @need_operation ) { + + if @need_operation.elems == 1 { + # single operation + @nums[ @need_operation[ 0 ] ] += 1; + $score += $single; + } + elsif @need_operation.elems > 1 { + @nums[ @need_operation[ 0 ] ] += 1; + @nums[ @need_operation[ 1 ] ] += 1; + $score += $double; + } + + @need_operation = @nums.grep( * < $current_max, :k ); + } + + $score.say; + +} |
