aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-15 19:07:15 +0000
committerGitHub <noreply@github.com>2024-02-15 19:07:15 +0000
commitd94684a52764452af4edbb2a33b7d409306e271c (patch)
tree0e4a7052c4c740d03ef90644e220981492e502cf
parente515224a439c9525a7e7cb7d33fc4b4c9af9785e (diff)
parent0cc3d19f45790123a25f7f33d9c5b3b239a48c5f (diff)
downloadperlweeklychallenge-club-d94684a52764452af4edbb2a33b7d409306e271c.tar.gz
perlweeklychallenge-club-d94684a52764452af4edbb2a33b7d409306e271c.tar.bz2
perlweeklychallenge-club-d94684a52764452af4edbb2a33b7d409306e271c.zip
Merge pull request #9587 from fluca1978/PWC256
PWC 256
-rw-r--r--challenge-256/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-10.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-7.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-8.txt1
-rw-r--r--challenge-256/luca-ferrari/blog-9.txt1
-rw-r--r--challenge-256/luca-ferrari/pljava/pom.xml6
-rw-r--r--challenge-256/luca-ferrari/pljava/src/main/java/Task1.java70
-rw-r--r--challenge-256/luca-ferrari/pljava/src/main/java/Task2.java58
-rw-r--r--challenge-256/luca-ferrari/plperl/ch-1.plperl30
-rw-r--r--challenge-256/luca-ferrari/plperl/ch-2.plperl28
-rw-r--r--challenge-256/luca-ferrari/plpgsql/ch-1.sql21
-rw-r--r--challenge-256/luca-ferrari/plpgsql/ch-2.sql59
-rw-r--r--challenge-256/luca-ferrari/python/ch-1.python26
-rw-r--r--challenge-256/luca-ferrari/python/ch-2.python32
-rw-r--r--challenge-256/luca-ferrari/raku/ch-1.raku17
-rw-r--r--challenge-256/luca-ferrari/raku/ch-2.raku21
21 files changed, 375 insertions, 3 deletions
diff --git a/challenge-256/luca-ferrari/blog-1.txt b/challenge-256/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..031fcff3f4
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task1
diff --git a/challenge-256/luca-ferrari/blog-10.txt b/challenge-256/luca-ferrari/blog-10.txt
new file mode 100644
index 0000000000..af53a4df07
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-10.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge256.html#task2pljava
diff --git a/challenge-256/luca-ferrari/blog-2.txt b/challenge-256/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..43387918ad
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task2
diff --git a/challenge-256/luca-ferrari/blog-3.txt b/challenge-256/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..4457aac83d
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task1plperl
diff --git a/challenge-256/luca-ferrari/blog-4.txt b/challenge-256/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..ed811dc530
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task2plperl
diff --git a/challenge-256/luca-ferrari/blog-5.txt b/challenge-256/luca-ferrari/blog-5.txt
new file mode 100644
index 0000000000..5461bf0882
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-5.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task1plpgsql
diff --git a/challenge-256/luca-ferrari/blog-6.txt b/challenge-256/luca-ferrari/blog-6.txt
new file mode 100644
index 0000000000..75aebd1af4
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-6.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task2plpgsql
diff --git a/challenge-256/luca-ferrari/blog-7.txt b/challenge-256/luca-ferrari/blog-7.txt
new file mode 100644
index 0000000000..409bf20db6
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-7.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task1python
diff --git a/challenge-256/luca-ferrari/blog-8.txt b/challenge-256/luca-ferrari/blog-8.txt
new file mode 100644
index 0000000000..f8b6e92dd8
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-8.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task2python
diff --git a/challenge-256/luca-ferrari/blog-9.txt b/challenge-256/luca-ferrari/blog-9.txt
new file mode 100644
index 0000000000..329d3b1a0e
--- /dev/null
+++ b/challenge-256/luca-ferrari/blog-9.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/14/PerlWeeklyChallenge256.html#task1pljava
diff --git a/challenge-256/luca-ferrari/pljava/pom.xml b/challenge-256/luca-ferrari/pljava/pom.xml
index e5ba684e37..eec35352dc 100644
--- a/challenge-256/luca-ferrari/pljava/pom.xml
+++ b/challenge-256/luca-ferrari/pljava/pom.xml
@@ -5,11 +5,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>PWC</groupId>
- <artifactId>PWC255</artifactId>
+ <artifactId>PWC256</artifactId>
<version>1</version>
- <name>Perl Weekly Challenge 255</name>
- <description>Implementation of the tasks in PL/Java for PWC 255</description>
+ <name>Perl Weekly Challenge 256</name>
+ <description>Implementation of the tasks in PL/Java for PWC 256</description>
<properties>
<project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding>
diff --git a/challenge-256/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-256/luca-ferrari/pljava/src/main/java/Task1.java
new file mode 100644
index 0000000000..f532901d6e
--- /dev/null
+++ b/challenge-256/luca-ferrari/pljava/src/main/java/Task1.java
@@ -0,0 +1,70 @@
+package PWC256;
+
+/**
+ * PL/Java implementation for PWC 256
+ * Task 1
+ * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+ *
+ *
+ * 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/PWC256.jar luca@venkman:/tmp
+
+
+ * To install into PostgreSQL execute:
+
+ select sqlj.install_jar( 'file:///tmp/PWC256.jar', 'PWC256', true );
+
+ select 256.task1_pljava();
+
+ and then to redeploy:
+
+ select sqlj.replace_jar( 'file:///tmp/PWC256.jar', 'PWC256', 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.sql.SQLException;
+import java.util.logging.*;
+
+public class Task1 {
+
+ private final static Logger logger = Logger.getAnonymousLogger();
+
+ @Function( schema = "256",
+ onNullInput = RETURNS_NULL,
+ effects = IMMUTABLE )
+ public static final int task1_pljava( String[] words ) throws SQLException {
+ logger.log( Level.INFO, "Entering task1_pljava" );
+
+ int max = 0;
+ for ( String current : words ) {
+ int count = count( current, words );
+ if ( count > max )
+ max = count;
+ }
+
+ return max;
+
+ }
+
+ private static final int count( String word, String[] words ) {
+ int count = 0;
+ StringBuilder builder = new StringBuilder();
+ builder.append( word );
+ word = builder.reverse().toString();
+
+ for ( String w : words )
+ if ( w.equals( word ) )
+ count++;
+
+ return count;
+ }
+}
diff --git a/challenge-256/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-256/luca-ferrari/pljava/src/main/java/Task2.java
new file mode 100644
index 0000000000..75e231c2c2
--- /dev/null
+++ b/challenge-256/luca-ferrari/pljava/src/main/java/Task2.java
@@ -0,0 +1,58 @@
+package PWC256;
+
+/**
+ * PL/Java implementation for PWC 256
+ * Task 1
+ * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+ *
+ *
+ * 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/PWC256.jar luca@venkman:/tmp
+
+
+ * To install into PostgreSQL execute:
+
+ select sqlj.install_jar( 'file:///tmp/PWC256.jar', 'PWC256', true );
+
+ select 256.task2_pljava();
+
+ and then to redeploy:
+
+ select sqlj.replace_jar( 'file:///tmp/PWC256.jar', 'PWC256', 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.sql.SQLException;
+import java.util.logging.*;
+
+public class Task2 {
+ private final static Logger logger = Logger.getAnonymousLogger();
+
+ @Function( schema = "256",
+ onNullInput = RETURNS_NULL,
+ effects = IMMUTABLE )
+ public static final String task2_pljava( String left, String right ) throws SQLException {
+ logger.log( Level.INFO, "Entering task2_pljava" );
+
+ StringBuffer buffer = new StringBuffer( left.length() + right.length() );
+
+ int max_index = Math.max( left.length(), right.length() );
+ for ( int index = 0; index < max_index; index++ ) {
+ if ( index < left.length() )
+ buffer.append( left.charAt( index ) );
+ if ( index < right.length() )
+ buffer.append( right.charAt( index ) );
+ }
+
+ return buffer.toString();
+ }
+}
diff --git a/challenge-256/luca-ferrari/plperl/ch-1.plperl b/challenge-256/luca-ferrari/plperl/ch-1.plperl
new file mode 100644
index 0000000000..40f34e42a6
--- /dev/null
+++ b/challenge-256/luca-ferrari/plperl/ch-1.plperl
@@ -0,0 +1,30 @@
+--
+-- Perl Weekly Challenge 256
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc256;
+
+CREATE OR REPLACE FUNCTION
+pwc256.task1_plperl( text[] )
+RETURNS int
+AS $CODE$
+
+ my ( $words ) = @_;
+
+ my $counting = {};
+ for my $current ( $words->@* ) {
+ $counting->{ $current } = scalar grep( { $_ eq reverse( $current ) } $words->@* ) ;
+ }
+
+
+ my $max = 0;
+ for ( values $counting->%* ) {
+ $max = $_ if ( $_ > $max );
+ }
+
+ return $max;
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-256/luca-ferrari/plperl/ch-2.plperl b/challenge-256/luca-ferrari/plperl/ch-2.plperl
new file mode 100644
index 0000000000..846afec065
--- /dev/null
+++ b/challenge-256/luca-ferrari/plperl/ch-2.plperl
@@ -0,0 +1,28 @@
+--
+-- Perl Weekly Challenge 256
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc256;
+
+CREATE OR REPLACE FUNCTION
+pwc256.task2_plperl( text, text )
+RETURNS text
+AS $CODE$
+
+ my ( $left, $right ) = @_;
+ my $output = '';
+
+ my @left_chars = split //, $left;
+ my @right_chars = split //, $right;
+
+ while ( @left_chars || @right_chars ) {
+ $output .= shift( @left_chars ) if ( @left_chars );
+ $output .= shift( @right_chars ) if ( @right_chars );
+ $index++;
+ }
+
+ return $output;
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-256/luca-ferrari/plpgsql/ch-1.sql b/challenge-256/luca-ferrari/plpgsql/ch-1.sql
new file mode 100644
index 0000000000..3c11674589
--- /dev/null
+++ b/challenge-256/luca-ferrari/plpgsql/ch-1.sql
@@ -0,0 +1,21 @@
+--
+-- Perl Weekly Challenge 256
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc256;
+
+CREATE OR REPLACE FUNCTION
+pwc256.task1_plpgsql( words text[] )
+RETURNS text
+AS $CODE$
+ SELECT max( o )
+ FROM ( SELECT w, count(*) as o
+ FROM unnest( words ) w, unnest( words ) ww
+ WHERE w = reverse ( ww )
+ GROUP BY w
+ );
+
+$CODE$
+LANGUAGE sql;
diff --git a/challenge-256/luca-ferrari/plpgsql/ch-2.sql b/challenge-256/luca-ferrari/plpgsql/ch-2.sql
new file mode 100644
index 0000000000..99e5c735fa
--- /dev/null
+++ b/challenge-256/luca-ferrari/plpgsql/ch-2.sql
@@ -0,0 +1,59 @@
+--
+-- Perl Weekly Challenge 256
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc256;
+
+CREATE OR REPLACE FUNCTION
+pwc256.task2_plpgsql( lw text, rw text )
+RETURNS text
+AS $CODE$
+DECLARE
+ max_index int;
+ output text := '';
+ next_char char;
+BEGIN
+ CREATE TEMPORARY TABLE IF NOT EXISTS words( l char, r char, o int );
+ TRUNCATE words;
+
+ INSERT INTO words( l, o )
+ SELECT v, row_number() over ()
+ FROM regexp_split_to_table( lw, '' ) v;
+
+ INSERT INTO words( r, o )
+ SELECT v, row_number() over ()
+ FROM regexp_split_to_table( rw, '' ) v;
+
+ SELECT max( o )
+ INTO max_index
+ FROM words;
+
+ FOR i IN 1 .. max_index LOOP
+ SELECT l
+ INTO next_char
+ FROM words
+ WHERE o = i
+ AND l IS NOT NULL;
+
+ IF FOUND AND next_char IS NOT NULL THEN
+ output := output || next_char;
+ END IF;
+
+ SELECT r
+ INTO next_char
+ FROM words
+ WHERE o = i
+ AND r IS NOT NULL;
+
+ IF FOUND AND next_char IS NOT NULL THEN
+ output := output || next_char;
+ END IF;
+ END LOOP;
+
+ RETURN output;
+
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-256/luca-ferrari/python/ch-1.python b/challenge-256/luca-ferrari/python/ch-1.python
new file mode 100644
index 0000000000..e8456319db
--- /dev/null
+++ b/challenge-256/luca-ferrari/python/ch-1.python
@@ -0,0 +1,26 @@
+#!python
+
+#
+# Perl Weekly Challenge 256
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+#
+
+import sys
+
+# task implementation
+# the return value will be printed
+def task_1( args ):
+ max = 0
+ for w in args:
+ count = len( list( filter( lambda rw: rw == ''.join( reversed( w ) ), args ) ) )
+ if count > max:
+ max = count
+
+ return max
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ print( task_1( sys.argv[ 1: ] ) )
diff --git a/challenge-256/luca-ferrari/python/ch-2.python b/challenge-256/luca-ferrari/python/ch-2.python
new file mode 100644
index 0000000000..abf9af51de
--- /dev/null
+++ b/challenge-256/luca-ferrari/python/ch-2.python
@@ -0,0 +1,32 @@
+#!python
+
+#
+# Perl Weekly Challenge 256
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+#
+
+import sys
+
+# task implementation
+# the return value will be printed
+def task_2( args ):
+ left = args[ 0 ]
+ right = args[ 1 ]
+ output = ''
+
+ max_index = max( len( left ), len( right ) )
+
+ for i in range( 0, max_index ):
+ if i < len( left ):
+ output += left[ i ]
+ if i < len( right ):
+ output += right[ i ]
+
+ return output
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ print( task_2( sys.argv[ 1: ] ) )
diff --git a/challenge-256/luca-ferrari/raku/ch-1.raku b/challenge-256/luca-ferrari/raku/ch-1.raku
new file mode 100644
index 0000000000..006e85b36b
--- /dev/null
+++ b/challenge-256/luca-ferrari/raku/ch-1.raku
@@ -0,0 +1,17 @@
+#!raku
+
+#
+# Perl Weekly Challenge 256
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+#
+
+sub MAIN( *@words ) {
+ my %counting;
+ for @words -> $current-word {
+ %counting{ $current-word } = @words.grep( { $_ ~~ $current-word.flip } ).elems;
+ }
+
+ %counting.values.max.say;
+}
diff --git a/challenge-256/luca-ferrari/raku/ch-2.raku b/challenge-256/luca-ferrari/raku/ch-2.raku
new file mode 100644
index 0000000000..ce67732a39
--- /dev/null
+++ b/challenge-256/luca-ferrari/raku/ch-2.raku
@@ -0,0 +1,21 @@
+#!raku
+
+#
+# Perl Weekly Challenge 256
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-256>
+#
+
+sub MAIN( Str $left, Str $right ) {
+ my $output = ( $left.comb Z $right.comb ).flat.join;
+
+ if ( $left.chars > $right.chars ) {
+ $output ~= $left.comb[ $right.chars .. * ].join;
+ }
+ elsif ( $left.chars < $right.chars ) {
+ $output ~= $right.comb[ $left.chars .. * ].join;
+ }
+
+ $output.say;
+}