aboutsummaryrefslogtreecommitdiff
path: root/challenge-263
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2024-04-04 13:03:27 +0200
committerLuca Ferrari <fluca1978@gmail.com>2024-04-04 15:05:03 +0200
commit44a92ee6dd0ca577889cc1489e1237d5084a1efc (patch)
tree49d7ea43e0763e88f5812ddb0932336209f02b97 /challenge-263
parentc2aa18d20881e4079d3858c0218ff99a07032d33 (diff)
downloadperlweeklychallenge-club-44a92ee6dd0ca577889cc1489e1237d5084a1efc.tar.gz
perlweeklychallenge-club-44a92ee6dd0ca577889cc1489e1237d5084a1efc.tar.bz2
perlweeklychallenge-club-44a92ee6dd0ca577889cc1489e1237d5084a1efc.zip
PWC 263
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 PL/Java done Task 2 PL/Java done Task 1 Python done Task 2 PYthon done
Diffstat (limited to 'challenge-263')
-rw-r--r--challenge-263/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-10.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-7.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-8.txt1
-rw-r--r--challenge-263/luca-ferrari/blog-9.txt1
-rw-r--r--challenge-263/luca-ferrari/pljava/pom.xml6
-rw-r--r--challenge-263/luca-ferrari/pljava/src/main/java/Task1.java74
-rw-r--r--challenge-263/luca-ferrari/pljava/src/main/java/Task2.java103
-rw-r--r--challenge-263/luca-ferrari/plperl/ch-1.plperl23
-rw-r--r--challenge-263/luca-ferrari/plperl/ch-2.plperl25
-rw-r--r--challenge-263/luca-ferrari/plpgsql/ch-1.sql21
-rw-r--r--challenge-263/luca-ferrari/plpgsql/ch-2.sql45
-rw-r--r--challenge-263/luca-ferrari/python/ch-1.py23
-rw-r--r--challenge-263/luca-ferrari/python/ch-2.py32
-rw-r--r--challenge-263/luca-ferrari/raku/ch-1.raku13
-rw-r--r--challenge-263/luca-ferrari/raku/ch-2.raku19
21 files changed, 391 insertions, 3 deletions
diff --git a/challenge-263/luca-ferrari/blog-1.txt b/challenge-263/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..dab3a02826
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task1
diff --git a/challenge-263/luca-ferrari/blog-10.txt b/challenge-263/luca-ferrari/blog-10.txt
new file mode 100644
index 0000000000..044f9ee49c
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-10.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge263.html#task2pljava
diff --git a/challenge-263/luca-ferrari/blog-2.txt b/challenge-263/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..3c5b58d3a1
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task2
diff --git a/challenge-263/luca-ferrari/blog-3.txt b/challenge-263/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..5ad557ee5b
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task1plperl
diff --git a/challenge-263/luca-ferrari/blog-4.txt b/challenge-263/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..15de02d41c
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task2plperl
diff --git a/challenge-263/luca-ferrari/blog-5.txt b/challenge-263/luca-ferrari/blog-5.txt
new file mode 100644
index 0000000000..23d88f2d97
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-5.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task1plpgsql
diff --git a/challenge-263/luca-ferrari/blog-6.txt b/challenge-263/luca-ferrari/blog-6.txt
new file mode 100644
index 0000000000..a97d4233a5
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-6.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task2plpgsql
diff --git a/challenge-263/luca-ferrari/blog-7.txt b/challenge-263/luca-ferrari/blog-7.txt
new file mode 100644
index 0000000000..8c22e2b162
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-7.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task1python
diff --git a/challenge-263/luca-ferrari/blog-8.txt b/challenge-263/luca-ferrari/blog-8.txt
new file mode 100644
index 0000000000..68436eca53
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-8.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task2python
diff --git a/challenge-263/luca-ferrari/blog-9.txt b/challenge-263/luca-ferrari/blog-9.txt
new file mode 100644
index 0000000000..a397b75dbd
--- /dev/null
+++ b/challenge-263/luca-ferrari/blog-9.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/04/04/PerlWeeklyChallenge263.html#task1pljava
diff --git a/challenge-263/luca-ferrari/pljava/pom.xml b/challenge-263/luca-ferrari/pljava/pom.xml
index d227373f13..23b9d42d1a 100644
--- a/challenge-263/luca-ferrari/pljava/pom.xml
+++ b/challenge-263/luca-ferrari/pljava/pom.xml
@@ -7,14 +7,14 @@
<groupId>PWC</groupId>
<artifactId>
- PWC262
+ PWC263
</artifactId>
<version>
1
</version>
- <name>Perl Weekly Challenge 262 with package PWC262</name>
- <description>Implementation of the tasks in PL/Java for PWC 262</description>
+ <name>Perl Weekly Challenge 263 with package PWC263</name>
+ <description>Implementation of the tasks in PL/Java for PWC 263</description>
<properties>
<project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding>
diff --git a/challenge-263/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-263/luca-ferrari/pljava/src/main/java/Task1.java
new file mode 100644
index 0000000000..50f9cdd131
--- /dev/null
+++ b/challenge-263/luca-ferrari/pljava/src/main/java/Task1.java
@@ -0,0 +1,74 @@
+
+
+
+package PWC263;
+
+/**
+ * PL/Java implementation for PWC 263
+ * Task 1
+ * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+ *
+ *
+ * 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/PWC263-1.jar luca@rachel:/tmp
+
+
+ * To install into PostgreSQL execute:
+
+ select sqlj.install_jar( 'file:///tmp/PWC263-1.jar', 'PWC263', true );
+ select sqlj.set_classpath( 'public', 'PWC263' );
+
+ select pwc263.task2_pljava();
+
+ and then to redeploy:
+
+ select sqlj.replace_jar( 'file:///tmp/PWC263-1.jar', 'PWC263', 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 = "pwc263",
+ onNullInput = RETURNS_NULL,
+ effects = IMMUTABLE )
+ public static final int[] task1_pljava( int k, int[] nums ) throws SQLException {
+ logger.log( Level.INFO, "Entering pwc263.task1_pljava" );
+
+ List<Integer> source = new LinkedList<Integer>();
+ for ( int v : nums )
+ source.add( v );
+
+ Collections.sort( source );
+
+ final List<Integer> result = new LinkedList<Integer>();
+ IntStream.range( 0, source.size() )
+ .forEach( index -> {
+ if ( source.get( index ) == k )
+ result.add( index );
+ } );
+
+ int res[] = new int[ result.size() ];
+ for ( int i = 0; i < res.length; i++ )
+ res[ i ] = result.get( i );
+
+ return res;
+
+ }
+}
diff --git a/challenge-263/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-263/luca-ferrari/pljava/src/main/java/Task2.java
new file mode 100644
index 0000000000..e177ef5a38
--- /dev/null
+++ b/challenge-263/luca-ferrari/pljava/src/main/java/Task2.java
@@ -0,0 +1,103 @@
+
+
+
+package PWC263;
+
+/**
+ * PL/Java implementation for PWC 263
+ * Task 2
+ * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+ *
+ *
+ * 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/PWC263-1.jar luca@rachel:/tmp
+
+
+ * To install into PostgreSQL execute:
+
+ select sqlj.install_jar( 'file:///tmp/PWC263-1.jar', 'PWC263', true );
+ select sqlj.set_classpath( 'public', 'PWC263' );
+
+ select pwc263.task2_pljava();
+
+ and then to redeploy:
+
+ select sqlj.replace_jar( 'file:///tmp/PWC263-1.jar', 'PWC263', 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 implements ResultSetProvider {
+
+ private final static Logger logger = Logger.getAnonymousLogger();
+
+ @Function( schema = "pwc263",
+ onNullInput = RETURNS_NULL,
+ effects = IMMUTABLE )
+ public static final ResultSetProvider task2_pljava( int i[], int j[] ) throws SQLException {
+ logger.log( Level.INFO, "Entering pwc263.task2_pljava" );
+ return new Task2( i, j );
+ }
+
+
+ Map<Integer, Integer> quantity = new HashMap<Integer, Integer>();
+ List<Integer> items = new LinkedList<Integer>();
+
+ public Task2( int[] i, int[] j ) {
+ super();
+
+ int index = 0;
+ while ( index < i.length ) {
+ int item = i[ index ];
+ int qty = i[ index + 1 ];
+ quantity.put( i[ index ], i[ index + 1 ] );
+ index += 2;
+ }
+
+ index = 0;
+ while ( index < j.length ) {
+ int item = j[ index ];
+ int qty = j[ index + 1 ];
+ if ( quantity.containsKey( item ) )
+ qty += quantity.get( item );
+
+
+ quantity.put( item, qty );
+ index += 2;
+ }
+
+ items.addAll( quantity.keySet() );
+
+ }
+
+ @Override
+ public boolean assignRowValues( ResultSet tuples, int row )
+ throws SQLException {
+
+ if ( items.isEmpty() || quantity.isEmpty() )
+ return false;
+
+ int item = items.remove( 0 );
+
+ tuples.updateInt( 1, item );
+ tuples.updateInt( 2, quantity.get( item ) );
+ return true;
+ }
+
+ @Override
+ public void close() {}
+}
diff --git a/challenge-263/luca-ferrari/plperl/ch-1.plperl b/challenge-263/luca-ferrari/plperl/ch-1.plperl
new file mode 100644
index 0000000000..3430f227af
--- /dev/null
+++ b/challenge-263/luca-ferrari/plperl/ch-1.plperl
@@ -0,0 +1,23 @@
+--
+-- Perl Weekly Challenge 263
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc263;
+
+CREATE OR REPLACE FUNCTION
+pwc263.task1_plperl( int, int[] )
+RETURNS SETOF int
+AS $CODE$
+
+ my ( $k, $nums ) = @_;
+
+ return [
+ map { $_->[ 0 ] }
+ grep { $_->[ 1 ] == $k }
+ map { [ $index++, $_ ] }
+ sort $nums->@* ];
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-263/luca-ferrari/plperl/ch-2.plperl b/challenge-263/luca-ferrari/plperl/ch-2.plperl
new file mode 100644
index 0000000000..2dd1cbe968
--- /dev/null
+++ b/challenge-263/luca-ferrari/plperl/ch-2.plperl
@@ -0,0 +1,25 @@
+--
+-- Perl Weekly Challenge 263
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc263;
+
+CREATE OR REPLACE FUNCTION
+pwc263.task2_plperl( int[], int[] )
+RETURNS TABLE( item int, qty int )
+AS $CODE$
+
+ my ( $items1, $items2 ) = @_;
+ my $q = {};
+
+ for my $pair ( $items1->@*, $items2->@* ) {
+ $q->{ $pair->[ 0 ] } += $pair->[ 1 ];
+ }
+
+
+ return_next( { item => $_, qty => $q->{ $_ } } ) for ( sort keys $q->%* );
+ return undef;
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-263/luca-ferrari/plpgsql/ch-1.sql b/challenge-263/luca-ferrari/plpgsql/ch-1.sql
new file mode 100644
index 0000000000..94d1bc7dc0
--- /dev/null
+++ b/challenge-263/luca-ferrari/plpgsql/ch-1.sql
@@ -0,0 +1,21 @@
+--
+-- Perl Weekly Challenge 263
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc263;
+
+CREATE OR REPLACE FUNCTION
+pwc263.task1_plpgsql( k int, nums int[] )
+RETURNS SETOF int
+AS $CODE$
+
+ SELECT x
+ FROM (
+ SELECT v, row_number() over ( order by v ) as x
+ FROM unnest( nums ) v
+ WHERE v = k
+ )
+$CODE$
+LANGUAGE sql;
diff --git a/challenge-263/luca-ferrari/plpgsql/ch-2.sql b/challenge-263/luca-ferrari/plpgsql/ch-2.sql
new file mode 100644
index 0000000000..ed18e9b8e2
--- /dev/null
+++ b/challenge-263/luca-ferrari/plpgsql/ch-2.sql
@@ -0,0 +1,45 @@
+--
+-- Perl Weekly Challenge 263
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc263;
+
+CREATE OR REPLACE FUNCTION
+pwc263.task2_plpgsql( i int[], j int[] )
+RETURNS TABLE( item int, qty int )
+AS $CODE$
+DECLARE
+
+BEGIN
+
+ CREATE TEMPORARY TABLE IF NOT EXISTS q( item int, qty int );
+ TRUNCATE TABLE q;
+
+ FOR x IN 1 .. array_length( i, 1 ) LOOP
+ IF mod( x, 2 ) = 0 THEN
+ CONTINUE;
+ END IF;
+ INSERT INTO q( item, qty )
+ VALUES ( i[ x ], i[ x + 1 ] );
+
+ END LOOP;
+
+ FOR x IN 1 .. array_length( j, 1 ) LOOP
+ IF mod( x, 2 ) = 0 THEN
+ CONTINUE;
+ END IF;
+ INSERT INTO q( item, qty )
+ VALUES ( j[ x ], j[ x + 1 ] );
+
+ END LOOP;
+
+ RETURN QUERY
+ SELECT q.item, sum( q.qty )::int
+ FROM q
+ GROUP BY q.item;
+
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-263/luca-ferrari/python/ch-1.py b/challenge-263/luca-ferrari/python/ch-1.py
new file mode 100644
index 0000000000..445b48b004
--- /dev/null
+++ b/challenge-263/luca-ferrari/python/ch-1.py
@@ -0,0 +1,23 @@
+#!python
+
+#
+# Perl Weekly Challenge 263
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+#
+
+import sys
+
+# task implementation
+# the return value will be printed
+def task_1( args ):
+ k = int( args[ 0 ] )
+ nums = list( map( int, args[ 1: ] ) )
+ nums.sort()
+ return list( filter( lambda x: nums[ x ] == k, range( 0, len( nums ) ) ) )
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ print( task_1( sys.argv[ 1: ] ) )
diff --git a/challenge-263/luca-ferrari/python/ch-2.py b/challenge-263/luca-ferrari/python/ch-2.py
new file mode 100644
index 0000000000..c4f6249586
--- /dev/null
+++ b/challenge-263/luca-ferrari/python/ch-2.py
@@ -0,0 +1,32 @@
+#!python
+
+#
+# Perl Weekly Challenge 263
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+#
+
+import sys
+
+# task implementation
+# the return value will be printed
+def task_2( args ):
+ quantity = {}
+ for index in range( 0, len( args ) - 1 ):
+ if index % 2 == 1:
+ continue
+
+ item = args[ index ]
+ qty = int( args[ index + 1 ] )
+ if item in quantity:
+ quantity[ item ] += qty
+ else:
+ quantity[ item ] = qty
+
+ return quantity
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ print( task_2( sys.argv[ 1: ] ) )
diff --git a/challenge-263/luca-ferrari/raku/ch-1.raku b/challenge-263/luca-ferrari/raku/ch-1.raku
new file mode 100644
index 0000000000..9e0a172c8d
--- /dev/null
+++ b/challenge-263/luca-ferrari/raku/ch-1.raku
@@ -0,0 +1,13 @@
+#!raku
+
+#
+# Perl Weekly Challenge 263
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+#
+
+sub MAIN( Int $k, *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) {
+ @nums.sort.grep( * ~~ $k, :k ).say;
+
+}
diff --git a/challenge-263/luca-ferrari/raku/ch-2.raku b/challenge-263/luca-ferrari/raku/ch-2.raku
new file mode 100644
index 0000000000..bd905f788d
--- /dev/null
+++ b/challenge-263/luca-ferrari/raku/ch-2.raku
@@ -0,0 +1,19 @@
+#!raku
+
+#
+# Perl Weekly Challenge 263
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-263>
+#
+
+sub MAIN() {
+ my $items1 = [ [1,1], [2,1], [3,2] ];
+ my $items2 = [ [2,2], [1,3] ];
+
+ my %quantities;
+ %quantities{ $_[ 0 ] } += $_[ 1 ] for $items1.flat;
+ %quantities{ $_[ 0 ] } += $_[ 1 ] for $items2.flat;
+
+ %quantities.Array.say;
+}