diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-26 11:53:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-26 11:53:23 +0000 |
| commit | 38676de2ac85384680efb69898afd3175aba5702 (patch) | |
| tree | 06497978b6d8356ae92408e424b8563a285061da | |
| parent | 6b5e4c8ab3a769e4aac7b512e2248af0d62409c0 (diff) | |
| parent | 3dc7347c37e5c52da5e3a3a8c76a785a1ca9f3af (diff) | |
| download | perlweeklychallenge-club-38676de2ac85384680efb69898afd3175aba5702.tar.gz perlweeklychallenge-club-38676de2ac85384680efb69898afd3175aba5702.tar.bz2 perlweeklychallenge-club-38676de2ac85384680efb69898afd3175aba5702.zip | |
Merge pull request #9822 from fluca1978/PWC262
PWC 262
21 files changed, 353 insertions, 3 deletions
diff --git a/challenge-262/luca-ferrari/blog-1.txt b/challenge-262/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..634c8fd21d --- /dev/null +++ b/challenge-262/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task1 diff --git a/challenge-262/luca-ferrari/blog-10.txt b/challenge-262/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..6f9cdfaf9f --- /dev/null +++ b/challenge-262/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge262.html#task2pljava diff --git a/challenge-262/luca-ferrari/blog-2.txt b/challenge-262/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..708e169028 --- /dev/null +++ b/challenge-262/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task2 diff --git a/challenge-262/luca-ferrari/blog-3.txt b/challenge-262/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..30cc620ef8 --- /dev/null +++ b/challenge-262/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task1plperl diff --git a/challenge-262/luca-ferrari/blog-4.txt b/challenge-262/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..342f34fdaf --- /dev/null +++ b/challenge-262/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task2plperl diff --git a/challenge-262/luca-ferrari/blog-5.txt b/challenge-262/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..cc0cebedac --- /dev/null +++ b/challenge-262/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task1plpgsql diff --git a/challenge-262/luca-ferrari/blog-6.txt b/challenge-262/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..fd601c5002 --- /dev/null +++ b/challenge-262/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task2plpgsql diff --git a/challenge-262/luca-ferrari/blog-7.txt b/challenge-262/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..6c5ce12629 --- /dev/null +++ b/challenge-262/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task1python diff --git a/challenge-262/luca-ferrari/blog-8.txt b/challenge-262/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..81d396d49a --- /dev/null +++ b/challenge-262/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task2python diff --git a/challenge-262/luca-ferrari/blog-9.txt b/challenge-262/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..375347cf9f --- /dev/null +++ b/challenge-262/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/26/PerlWeeklyChallenge262.html#task1pljava diff --git a/challenge-262/luca-ferrari/pljava/pom.xml b/challenge-262/luca-ferrari/pljava/pom.xml index d05bdc8222..d227373f13 100644 --- a/challenge-262/luca-ferrari/pljava/pom.xml +++ b/challenge-262/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ <groupId>PWC</groupId> <artifactId> - PWC261 + PWC262 </artifactId> <version> 1 </version> - <name>Perl Weekly Challenge 261 with package PWC261</name> - <description>Implementation of the tasks in PL/Java for PWC 261</description> + <name>Perl Weekly Challenge 262 with package PWC262</name> + <description>Implementation of the tasks in PL/Java for PWC 262</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-262/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-262/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..02e1474d67 --- /dev/null +++ b/challenge-262/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,66 @@ + + + +package PWC262; + +/** + * PL/Java implementation for PWC 262 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> + * + * + * 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/PWC262-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC262-1.jar', 'PWC262', true ); + select sqlj.set_classpath( 'public', 'PWC262' ); + + select pwc262.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC262-1.jar', 'PWC262', 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 = "pwc262", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( int[] nums ) throws SQLException { + logger.log( Level.INFO, "Entering pwc262.task1_pljava" ); + + int positives = 0; + int negatives = 0; + + for ( int current : nums ) + if ( current >= 0 ) + positives++; + else + negatives++; + + return positives >= negatives ? positives : negatives; + + } +} diff --git a/challenge-262/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-262/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..0260bb29b3 --- /dev/null +++ b/challenge-262/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,66 @@ + + + +package PWC262; + +/** + * PL/Java implementation for PWC 262 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> + * + * + * 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/PWC262-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC262-1.jar', 'PWC262', true ); + select sqlj.set_classpath( 'public', 'PWC262' ); + + select pwc262.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC262-1.jar', 'PWC262', 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 = "pwc262", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( int k, int[] nums ) throws SQLException { + logger.log( Level.INFO, "Entering pwc262.task2_pljava" ); + + int pairs = 0; + for ( int i = 0; i < nums.length; i++ ) + for ( int j = i + 1; j < nums.length; j++ ) + if ( nums[ i ] != nums[ j ] + || ( i * j ) % k != 0 ) + continue; + else + pairs++; + + return pairs; + + } +} diff --git a/challenge-262/luca-ferrari/plperl/ch-1.plperl b/challenge-262/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..7d3edfe195 --- /dev/null +++ b/challenge-262/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,25 @@ +-- +-- Perl Weekly Challenge 262 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +-- + +CREATE SCHEMA IF NOT EXISTS pwc262; + +CREATE OR REPLACE FUNCTION +pwc262.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + + my ( $nums ) = @_; + + my ( $positives, $negatives ) = + ( scalar( grep { $_ >= 0 } $nums->@* ), + scalar( grep { $_ < 0 } $nums->@* ) ); + + return $positives >= $negatives + ? $positives + : $negatives; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-262/luca-ferrari/plperl/ch-2.plperl b/challenge-262/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..1d058fb776 --- /dev/null +++ b/challenge-262/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 262 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +-- + +CREATE SCHEMA IF NOT EXISTS pwc262; + +CREATE OR REPLACE FUNCTION +pwc262.task2_plperl( int, int[] ) +RETURNS int +AS $CODE$ + + my ( $k, $nums ) = @_; + + my @pairs; + for my $i ( 0 .. $nums->@* - 1 ) { + for my $j ( $i + 1 .. $nums->@* - 1 ) { + next if ( $i * $j ) % $k != 0; + next if ( $nums->@[ $i ] != $nums->@[ $j ] ); + push @pairs, [ $i, $k ]; + } + } + + + return scalar @pairs; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-262/luca-ferrari/plpgsql/ch-1.sql b/challenge-262/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..46f754e796 --- /dev/null +++ b/challenge-262/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,31 @@ +-- +-- Perl Weekly Challenge 262 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +-- + +CREATE SCHEMA IF NOT EXISTS pwc262; + +CREATE OR REPLACE FUNCTION +pwc262.task1_plpgsql( nums int[] ) +RETURNS int +AS $CODE$ + + WITH positives AS ( + SELECT count( v ) as c + FROM unnest( nums ) v + WHERE v >= 0 + ), negatives AS ( + SELECT count( v ) as c + FROM unnest( nums ) v + WHERE v < 0 + ) + , b AS ( + SELECT c as v FROM positives + UNION + SELECT c as v FROM negatives + ) + SELECT max( b.v ) + FROM b; +$CODE$ +LANGUAGE sql; diff --git a/challenge-262/luca-ferrari/plpgsql/ch-2.sql b/challenge-262/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..1a90a2fa4c --- /dev/null +++ b/challenge-262/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 262 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +-- + +CREATE SCHEMA IF NOT EXISTS pwc262; + +CREATE OR REPLACE FUNCTION +pwc262.task2_plpgsql( k int, nums int[] ) +RETURNS int +AS $CODE$ +DECLARE + pairs_count int := 0; +BEGIN + FOR i IN 1 .. array_length( nums, 1 ) LOOP + FOR j IN ( i + 1 ) .. array_length( nums, 1 ) LOOP + IF nums[ i ] <> nums[ j ] THEN + CONTINUE; + END IF; + + IF mod( i * j, k ) <> 0 THEN + CONTINUE; + END IF; + + pairs_count := pairs_count + 1; + END LOOP; + END LOOP; + + RETURN pairs_count; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-262/luca-ferrari/python/ch-1.py b/challenge-262/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..cf22506a4c --- /dev/null +++ b/challenge-262/luca-ferrari/python/ch-1.py @@ -0,0 +1,24 @@ +#!python + +# +# Perl Weekly Challenge 262 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + nums = list( map( int, args ) ) + positives = len( list( filter( lambda x: True if x >= 0 else False, nums ) ) ) + negatives = len( list( filter( lambda x: True if x < 0 else False, nums ) ) ) + + return positives if positives >= negatives else negatives + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-262/luca-ferrari/python/ch-2.py b/challenge-262/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..2fae3dfa1f --- /dev/null +++ b/challenge-262/luca-ferrari/python/ch-2.py @@ -0,0 +1,30 @@ +#!python + +# +# Perl Weekly Challenge 262 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + k = int( args[ 0 ] ) + nums = list( map( int, args[ 1: ] ) ) + pairs = [] + + for i in range( 0, len( nums ) ): + for j in range( i + 1, len( nums ) ): + if nums[ i ] != nums[ j ] or ( i * j ) % k != 0: + continue + else: + pairs.append( [ i, j ] ) + + return len( pairs ) + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-262/luca-ferrari/raku/ch-1.raku b/challenge-262/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..823f5f19ea --- /dev/null +++ b/challenge-262/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!raku + +# +# Perl Weekly Challenge 262 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +# + +sub MAIN( *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + ( @nums.grep( * >= 0 ).elems, @nums.grep( * < 0 ).elems ).max.say; + +} diff --git a/challenge-262/luca-ferrari/raku/ch-2.raku b/challenge-262/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..d37b763bf1 --- /dev/null +++ b/challenge-262/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,24 @@ +#!raku + +# +# Perl Weekly Challenge 262 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-262> +# + +sub MAIN( Int $k where { $k != 0 }, + *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + + my @pairs; + + for 0 ..^ @nums.elems -> $i { + for $i ^..^ @nums.elems -> $j { + next if ( $i * $j ) !%% $k; + next if @nums[ $i ] != @nums[ $j ]; + @pairs.push: [ $i, $j ]; + } + } + + @pairs.elems.say; +} |
