diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-12 11:34:11 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-12 11:34:11 +0000 |
| commit | d62a01b918fdafcc375bab2ac6096b7ceef49718 (patch) | |
| tree | 4e8a9c83c6bffea3f5a5ea0242cc84c2ad4177cc | |
| parent | cc76368fff2cead23a96361849a30447c3a02b87 (diff) | |
| parent | dbe495712e34674a941fb1c17e0cd5bca5c55ae4 (diff) | |
| download | perlweeklychallenge-club-d62a01b918fdafcc375bab2ac6096b7ceef49718.tar.gz perlweeklychallenge-club-d62a01b918fdafcc375bab2ac6096b7ceef49718.tar.bz2 perlweeklychallenge-club-d62a01b918fdafcc375bab2ac6096b7ceef49718.zip | |
Merge pull request #9737 from fluca1978/PWC260
PWC 260
21 files changed, 390 insertions, 4 deletions
diff --git a/challenge-260/luca-ferrari/blog-1.txt b/challenge-260/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..7510d731e5 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1 diff --git a/challenge-260/luca-ferrari/blog-10.txt b/challenge-260/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..514d3aef6b --- /dev/null +++ b/challenge-260/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge260.html#task2pljava diff --git a/challenge-260/luca-ferrari/blog-2.txt b/challenge-260/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e16337dcb3 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2 diff --git a/challenge-260/luca-ferrari/blog-3.txt b/challenge-260/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..3d06909558 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1plperl diff --git a/challenge-260/luca-ferrari/blog-4.txt b/challenge-260/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..eda8b81e26 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2plperl diff --git a/challenge-260/luca-ferrari/blog-5.txt b/challenge-260/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..53e0a8ea7e --- /dev/null +++ b/challenge-260/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1plpgsql diff --git a/challenge-260/luca-ferrari/blog-6.txt b/challenge-260/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..2e7c624fd6 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2plpgsql diff --git a/challenge-260/luca-ferrari/blog-7.txt b/challenge-260/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..346f3ac1a0 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1python diff --git a/challenge-260/luca-ferrari/blog-8.txt b/challenge-260/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..8cb455f6be --- /dev/null +++ b/challenge-260/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2python diff --git a/challenge-260/luca-ferrari/blog-9.txt b/challenge-260/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..7abc1e76ed --- /dev/null +++ b/challenge-260/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1pljava diff --git a/challenge-260/luca-ferrari/pljava/pom.xml b/challenge-260/luca-ferrari/pljava/pom.xml index 30b7a337f3..e9d5777410 100644 --- a/challenge-260/luca-ferrari/pljava/pom.xml +++ b/challenge-260/luca-ferrari/pljava/pom.xml @@ -1,3 +1,4 @@ + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" @@ -5,11 +6,15 @@ <modelVersion>4.0.0</modelVersion> <groupId>PWC</groupId> - <artifactId>PWC259</artifactId> - <version>1</version> + <artifactId> + PWC260 + </artifactId> + <version> + 1 + </version> - <name>Perl Weekly Challenge 259</name> - <description>Implementation of the tasks in PL/Java for PWC 259</description> + <name>Perl Weekly Challenge 260 with package PWC260</name> + <description>Implementation of the tasks in PL/Java for PWC 260</description> <properties> <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> diff --git a/challenge-260/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-260/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..5f861c3715 --- /dev/null +++ b/challenge-260/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,75 @@ + + + +package PWC260; + +/** + * PL/Java implementation for PWC 260 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> + * + * + * 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/PWC260-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', true ); + select sqlj.set_classpath( 'public', 'PWC260' ); + + select pwc260.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', 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 = "pwc260", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task1_pljava( int[] nums ) throws SQLException { + logger.log( Level.INFO, "Entering pwc260.task1_pljava" ); + + List<Integer> numList = new LinkedList<Integer>(); + for ( int n : nums ) + numList.add( n ); + + Map<Integer, List<Integer>> bag = new HashMap<Integer, List<Integer>>(); + + for ( int current : numList ) { + int occurrency = Collections.frequency( numList, current ); + bag.putIfAbsent( occurrency, new LinkedList<Integer>() ); + if ( ! bag.get( occurrency ).contains( current ) ) + bag.get( occurrency ).add( current ); + } + + for ( int k : bag.keySet() ) + if ( bag.get( k ).size() > 1 ) + return false; + + + return true; + + } +} diff --git a/challenge-260/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-260/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..4b4766866c --- /dev/null +++ b/challenge-260/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,91 @@ + + + +package PWC260; + +/** + * PL/Java implementation for PWC 260 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> + * + * + * 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/PWC260-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', true ); + select sqlj.set_classpath( 'public', 'pwc260' ); + + select pwc260.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', 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; +import java.math.*; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc260", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( String word ) throws SQLException { + logger.log( Level.INFO, "Entering pwc260.task2_pljava" ); + + List<String> chars = new LinkedList<String>(); + for ( String s : word.split( "" ) ) { + logger.log( Level.INFO, "CHAR " + s ); + chars.add( s ); + } + + List<String> words = new LinkedList<String>(); + + BigInteger limit = BigInteger.ONE; + for ( int i = 1; i <= chars.size(); i++ ) { + limit = limit.multiply( new BigInteger( "" + i ) ); + } + + logger.log( Level.INFO, "Limit is " + limit ); + while ( BigInteger.ZERO.compareTo( limit ) < 0 ) { + + + String newWord = ""; + do { + Collections.shuffle( chars ); + newWord = String.join( "", chars ); + } while ( words.contains( newWord ) ); + + words.add( newWord ); + limit = limit.subtract( BigInteger.ONE ); + } + + Collections.sort( words ); + + for ( int i = 0; i < words.size(); i++ ) + if ( words.get( i ).equals( word ) ) + return i + 1; + + + return -1; + } +} diff --git a/challenge-260/luca-ferrari/plperl/ch-1.plperl b/challenge-260/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..f0feecdf67 --- /dev/null +++ b/challenge-260/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 260 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +-- + +CREATE SCHEMA IF NOT EXISTS pwc260; + +CREATE OR REPLACE FUNCTION +pwc260.task1_plperl( int[] ) +RETURNS boolean +AS $CODE$ + + my ( $nums ) = @_; + my $bag = {}; + + for my $current ( $nums->@* ) { + next if $bag->{ current }; # no need to reinitialize + $bag->{ $current } = scalar grep { $current == $_ } $nums->@*; + } + + for my $current ( values $bag->%* ) { + return 0 if ( scalar( grep { $current == $_ } values( $bag->%* ) ) > 1 ); + } + + return 1; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-260/luca-ferrari/plperl/ch-2.plperl b/challenge-260/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..9d0df6df44 --- /dev/null +++ b/challenge-260/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,32 @@ +-- +-- Perl Weekly Challenge 260 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +-- + +CREATE SCHEMA IF NOT EXISTS pwc260; + +CREATE OR REPLACE FUNCTION +pwc260.task2_plperl( text ) +RETURNS int +AS $CODE$ + use List::Permutor; + + my ( $word ) = @_; + my @words; + + my $engine = List::Permutor->new( split //, $word ); + while ( my @letters = $engine->next ) { + push @words, join( '', @letters ); + } + + @words = sort @words; + + + for my $index ( 0 .. @words - 1 ) { + return $index + 1 if ( $words[ $index ] eq $word ); + } + + return -1; +$CODE$ +LANGUAGE plperlu; diff --git a/challenge-260/luca-ferrari/plpgsql/ch-1.sql b/challenge-260/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..3f6d3cdaa6 --- /dev/null +++ b/challenge-260/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,34 @@ +-- +-- Perl Weekly Challenge 260 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +-- + +CREATE SCHEMA IF NOT EXISTS pwc260; + +CREATE OR REPLACE FUNCTION +pwc260.task1_plpgsql( nums int[] ) +RETURNS boolean +AS $CODE$ + + WITH numbers AS ( + SELECT n + FROM unnest( nums ) n + ) + , counting AS ( + SELECT n, count(*) AS c + FROM numbers + GROUP BY n + ) + , grouping AS ( + SELECT c, count(*) AS g + FROM counting + GROUP BY c + ) + SELECT NOT EXISTS( SELECT g + FROM grouping + WHERE g > 1 + ); + +$CODE$ +LANGUAGE sql; diff --git a/challenge-260/luca-ferrari/plpgsql/ch-2.sql b/challenge-260/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..2ee0e07735 --- /dev/null +++ b/challenge-260/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 260 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +-- + +CREATE SCHEMA IF NOT EXISTS pwc260; + +CREATE OR REPLACE FUNCTION +pwc260.task2_plpgsql( w text ) +RETURNS int +AS $CODE$ + SELECT pwc260.task2_plperl( w ); +$CODE$ +LANGUAGE ùsql; diff --git a/challenge-260/luca-ferrari/python/ch-1.py b/challenge-260/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..8f70bb0494 --- /dev/null +++ b/challenge-260/luca-ferrari/python/ch-1.py @@ -0,0 +1,33 @@ +#!python + +# +# Perl Weekly Challenge 260 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + nums = list( map( int, args ) ) + bag = {} + + for n in nums: + occurrencies = len( list( filter( lambda x: x == n, nums ) ) ) + if not n in bag: + bag[ n ] = occurrencies + + + for o in bag.values(): + if len( list( filter( lambda x: x == o, bag.values() ) ) ) > 1: + return 0 + + return 1 + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-260/luca-ferrari/python/ch-2.py b/challenge-260/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..b69ddaa9f4 --- /dev/null +++ b/challenge-260/luca-ferrari/python/ch-2.py @@ -0,0 +1,31 @@ +#!python + +# +# Perl Weekly Challenge 260 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +# + +import sys +import array +from itertools import permutations + +# task implementation +# the return value will be printed +def task_2( args ): + word = args[ 0 ] + words = [] + engine = permutations( list( word ) ) + for p in list( engine ): + words.append( ''.join( p ) ) + + words = sorted( words ) + for i in range( 0, len( words ) ): + if words[ i ] == word: + return i + 1 + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-260/luca-ferrari/raku/ch-1.raku b/challenge-260/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..3007368c9a --- /dev/null +++ b/challenge-260/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 260 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +# + +sub MAIN( *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + my $bag = Bag.new( @nums ); + + for $bag.values -> $current { + '0'.say and exit if ( $bag.values.grep( * ~~ $current ).elems > 1 ); + } + + '1'.say; + +} diff --git a/challenge-260/luca-ferrari/raku/ch-2.raku b/challenge-260/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..1bf593f90c --- /dev/null +++ b/challenge-260/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 260 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-260> +# + +sub MAIN( $word ) { + say $word.comb.permutations.map( *.join ).sort.grep( * ~~ $word, :k ).first + 1; +} |
