aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-05 18:58:36 +0000
committerGitHub <noreply@github.com>2024-02-05 18:58:36 +0000
commit261ffda2e5ef595efd76b5692a7fcc7a5fe39a8a (patch)
tree3f92c1382a3474bf5273fccd28fe740789ab3257
parent3e8277a32e27b17cee966058d5daef4a79ba6957 (diff)
parent7ae41852c28f903f5821561705eba50147c9c6e4 (diff)
downloadperlweeklychallenge-club-261ffda2e5ef595efd76b5692a7fcc7a5fe39a8a.tar.gz
perlweeklychallenge-club-261ffda2e5ef595efd76b5692a7fcc7a5fe39a8a.tar.bz2
perlweeklychallenge-club-261ffda2e5ef595efd76b5692a7fcc7a5fe39a8a.zip
Merge pull request #9520 from fluca1978/PWC255
PWC 255
-rw-r--r--challenge-255/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-10.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-7.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-8.txt1
-rw-r--r--challenge-255/luca-ferrari/blog-9.txt1
-rw-r--r--challenge-255/luca-ferrari/pljava/pom.xml6
-rw-r--r--challenge-255/luca-ferrari/pljava/src/main/java/Task1.java74
-rw-r--r--challenge-255/luca-ferrari/pljava/src/main/java/Task2.java62
-rw-r--r--challenge-255/luca-ferrari/plperl/ch-1.plperl29
-rw-r--r--challenge-255/luca-ferrari/plperl/ch-2.plperl28
-rw-r--r--challenge-255/luca-ferrari/plpgsql/ch-1.sql18
-rw-r--r--challenge-255/luca-ferrari/plpgsql/ch-2.sql35
-rw-r--r--challenge-255/luca-ferrari/python/ch-1.python26
-rw-r--r--challenge-255/luca-ferrari/python/ch-2.python21
-rw-r--r--challenge-255/luca-ferrari/raku/ch-1.raku18
-rw-r--r--challenge-255/luca-ferrari/raku/ch-2.raku16
21 files changed, 322 insertions, 21 deletions
diff --git a/challenge-255/luca-ferrari/blog-1.txt b/challenge-255/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..936241c6c4
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task1
diff --git a/challenge-255/luca-ferrari/blog-10.txt b/challenge-255/luca-ferrari/blog-10.txt
new file mode 100644
index 0000000000..a21839e412
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-10.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge255.html#task2pljava
diff --git a/challenge-255/luca-ferrari/blog-2.txt b/challenge-255/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..f829c75fbf
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task2
diff --git a/challenge-255/luca-ferrari/blog-3.txt b/challenge-255/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..7951fbdfd3
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task1plperl
diff --git a/challenge-255/luca-ferrari/blog-4.txt b/challenge-255/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..4242e183fd
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task2plperl
diff --git a/challenge-255/luca-ferrari/blog-5.txt b/challenge-255/luca-ferrari/blog-5.txt
new file mode 100644
index 0000000000..e0e73821c1
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-5.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task1plpgsql
diff --git a/challenge-255/luca-ferrari/blog-6.txt b/challenge-255/luca-ferrari/blog-6.txt
new file mode 100644
index 0000000000..27c518b72f
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-6.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task2plpgsql
diff --git a/challenge-255/luca-ferrari/blog-7.txt b/challenge-255/luca-ferrari/blog-7.txt
new file mode 100644
index 0000000000..ee88ec0e8d
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-7.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task1python
diff --git a/challenge-255/luca-ferrari/blog-8.txt b/challenge-255/luca-ferrari/blog-8.txt
new file mode 100644
index 0000000000..6929325c63
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-8.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task2python
diff --git a/challenge-255/luca-ferrari/blog-9.txt b/challenge-255/luca-ferrari/blog-9.txt
new file mode 100644
index 0000000000..2a75b6dad2
--- /dev/null
+++ b/challenge-255/luca-ferrari/blog-9.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2024/02/05/PerlWeeklyChallenge255.html#task1pljava
diff --git a/challenge-255/luca-ferrari/pljava/pom.xml b/challenge-255/luca-ferrari/pljava/pom.xml
index 8777b204e3..e5ba684e37 100644
--- a/challenge-255/luca-ferrari/pljava/pom.xml
+++ b/challenge-255/luca-ferrari/pljava/pom.xml
@@ -5,11 +5,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>PWC</groupId>
- <artifactId>PWC254</artifactId>
+ <artifactId>PWC255</artifactId>
<version>1</version>
- <name>Perl Weekly Challenge 254</name>
- <description>Implementation of the tasks in PL/Java for PWC 254</description>
+ <name>Perl Weekly Challenge 255</name>
+ <description>Implementation of the tasks in PL/Java for PWC 255</description>
<properties>
<project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding>
diff --git a/challenge-255/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-255/luca-ferrari/pljava/src/main/java/Task1.java
new file mode 100644
index 0000000000..ab63904e23
--- /dev/null
+++ b/challenge-255/luca-ferrari/pljava/src/main/java/Task1.java
@@ -0,0 +1,74 @@
+package PWC255;
+
+/**
+ * PL/Java implementation for PWC 255
+ * Task 1
+ * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+ *
+ *
+ * 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/PWC253-1.jar luca@venkman:/tmp
+
+
+ * To install into PostgreSQL execute:
+
+ select sqlj.install_jar( 'file:///tmp/PWC255-1.jar', 'PWC255', true );
+ select sqlj.set_classpath( 'public', 'PWC255' );
+
+ select task1_pljava();
+
+ and then to redeploy:
+
+ select sqlj.replace_jar( 'file:///tmp/PWC255-1.jar', 'PWC255', 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( onNullInput = RETURNS_NULL, effects = IMMUTABLE )
+ public static final String task1_pljava( String origin, String shuffled ) throws SQLException {
+ logger.log( Level.INFO, "Entering task1_pljava" );
+
+ if ( origin.length() + 1 != shuffled.length() ) {
+ throw new SQLException( "Shuffled string should be one character longer than the original one" );
+ }
+
+ Map<String, Integer> classification = new HashMap<String, Integer>();
+ for ( String needle : shuffled.split( "" ) ) {
+ int value = classification.keySet().contains( needle )
+ ? classification.get( needle )
+ : 0;
+
+ classification.put( needle, ++value );
+ }
+
+
+ for ( String comparison : origin.split( "" ) ) {
+ int value = classification.get( comparison );
+ value--;
+
+ if ( value > 0 )
+ classification.put( comparison, value );
+ else
+ classification.remove( comparison );
+ }
+
+
+ return classification.keySet().iterator().next();
+
+ }
+}
diff --git a/challenge-255/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-255/luca-ferrari/pljava/src/main/java/Task2.java
new file mode 100644
index 0000000000..0a8655ca2a
--- /dev/null
+++ b/challenge-255/luca-ferrari/pljava/src/main/java/Task2.java
@@ -0,0 +1,62 @@
+package PWC255;
+
+/**
+ * PL/Java implementation for PWC 255
+ * Task 1
+ * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+ *
+ *
+ * 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/PWC253-1.jar luca@venkman:/tmp
+
+
+ * To install into PostgreSQL execute:
+
+ select sqlj.install_jar( 'file:///tmp/PWC255-1.jar', 'PWC255', true );
+ select sqlj.set_classpath( 'public', 'PWC255' );
+
+ select task1_pljava();
+
+ and then to redeploy:
+
+ select sqlj.replace_jar( 'file:///tmp/PWC255-1.jar', 'PWC255', 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.*;
+
+public class Task2 {
+ private final static Logger logger = Logger.getAnonymousLogger();
+
+ @Function( onNullInput = RETURNS_NULL, effects = IMMUTABLE )
+ public static final String task2_pljava( String paragraph, String banned ) throws SQLException {
+ logger.log( Level.INFO, "Entering task2_pljava" );
+
+ Map<String, Integer> classification = new HashMap<String, Integer>();
+ for ( String word : paragraph.split( "\\W+" ) ) {
+ if ( word.equals( banned ) )
+ continue;
+
+ int value = classification.containsKey( word ) ? classification.get( word ) : 0;
+ classification.put( word, ++value );
+ }
+
+ String topValue = classification.entrySet()
+ .stream()
+ .sorted( Collections.reverseOrder( Map.Entry.comparingByValue() ) )
+ .findFirst().get().getKey();
+
+ return topValue;
+ }
+}
diff --git a/challenge-255/luca-ferrari/plperl/ch-1.plperl b/challenge-255/luca-ferrari/plperl/ch-1.plperl
new file mode 100644
index 0000000000..8423dacf63
--- /dev/null
+++ b/challenge-255/luca-ferrari/plperl/ch-1.plperl
@@ -0,0 +1,29 @@
+--
+-- Perl Weekly Challenge 255
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc255;
+
+CREATE OR REPLACE FUNCTION
+pwc255.task1_plperl( text, text )
+RETURNS text
+AS $CODE$
+
+ my ( $origin, $shuffled ) = @_;
+ die "Shuffled string [$shuffled] should be one char lengther than original [$origin]"
+ if ( length( $shuffled ) != length( $origin ) + 1 );
+
+
+ my $classification = {};
+ $classification->{ $_ }++ for ( split //, $shuffled );
+ $classification->{ $_ }-- for ( split //, $origin );
+
+ for ( keys $classification->%* ) {
+ return $_ if ( $classification->{ $_ } > 0 );
+ }
+
+ return undef;
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-255/luca-ferrari/plperl/ch-2.plperl b/challenge-255/luca-ferrari/plperl/ch-2.plperl
new file mode 100644
index 0000000000..1f73f3688f
--- /dev/null
+++ b/challenge-255/luca-ferrari/plperl/ch-2.plperl
@@ -0,0 +1,28 @@
+--
+-- Perl Weekly Challenge 255
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc255;
+
+CREATE OR REPLACE FUNCTION
+pwc255.task2_plperl( text, text )
+RETURNS text
+AS $CODE$
+
+ my ( $paragraph, $banned ) = @_;
+
+ $paragraph =~ s/\W*$banned\W*/ /g;
+
+ my $classification = {};
+ $classification->{ $_ }++ for ( split /\W/, $paragraph );
+ my $max = ( reverse sort values $classification->%* )[ 0 ];
+ for ( keys $classification->%* ) {
+ return $_ if ( $classification->{ $_ } == $max );
+ }
+
+ return undef;
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-255/luca-ferrari/plpgsql/ch-1.sql b/challenge-255/luca-ferrari/plpgsql/ch-1.sql
new file mode 100644
index 0000000000..fba7114f5f
--- /dev/null
+++ b/challenge-255/luca-ferrari/plpgsql/ch-1.sql
@@ -0,0 +1,18 @@
+--
+-- Perl Weekly Challenge 255
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc255;
+
+CREATE OR REPLACE FUNCTION
+pwc255.task1_plpgsql( origin text, shuffled text )
+RETURNS char
+AS $CODE$
+ SELECT s
+ FROM regexp_split_to_table( shuffled, '' ) s
+ WHERE s NOT IN ( SELECT v
+ FROM regexp_split_to_table( origin, '' ) v );
+$CODE$
+LANGUAGE sql;
diff --git a/challenge-255/luca-ferrari/plpgsql/ch-2.sql b/challenge-255/luca-ferrari/plpgsql/ch-2.sql
new file mode 100644
index 0000000000..33f80c3ddd
--- /dev/null
+++ b/challenge-255/luca-ferrari/plpgsql/ch-2.sql
@@ -0,0 +1,35 @@
+--
+-- Perl Weekly Challenge 255
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc255;
+
+CREATE OR REPLACE FUNCTION
+pwc255.task2_plpgsql( p text, b text )
+RETURNS SETOF text
+AS $CODE$
+DECLARE
+
+BEGIN
+ CREATE TEMPORARY TABLE IF NOT EXISTS classification( w text );
+ TRUNCATE TABLE classification;
+
+ INSERT INTO classification
+ SELECT v
+ FROM regexp_split_to_table( p, '\W+' ) v;
+
+ RETURN QUERY
+ SELECT w FROM (
+ SELECT w, count(*) AS c
+ FROM classification
+ WHERE w <> b
+ GROUP BY w
+ ORDER BY c DESC
+ LIMIT 1
+ );
+
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-255/luca-ferrari/python/ch-1.python b/challenge-255/luca-ferrari/python/ch-1.python
index a0533551d8..d5e0960574 100644
--- a/challenge-255/luca-ferrari/python/ch-1.python
+++ b/challenge-255/luca-ferrari/python/ch-1.python
@@ -1,24 +1,36 @@
#!python
#
-# Perl Weekly Challenge 254
+# Perl Weekly Challenge 255
# Task 1
#
# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254>
#
import sys
-import math
# task implementation
# the return value will be printed
def task_1( args ):
- num = int( args[ 0 ] )
- for i in range( 2, int( math.sqrt( num ) ) ):
- if ( i ** 3 ) == num:
- return True
+ origin = args[ 0 ]
+ shuffled = args[ 1 ]
+ if len( shuffled ) != len( origin ) + 1:
+ return "Shuffled string must be one character longer than original string"
- return False
+ classification = {}
+
+ for needle in shuffled:
+ if not needle in classification:
+ classification[ needle ] = 0
+
+ classification[ needle ] += 1
+
+ for needle in origin:
+ classification[ needle ] -= 1
+ if classification[ needle ] <= 0:
+ del classification[ needle ]
+
+ return list( classification.keys() )[ 0 ]
# invoke the main without the command itself
diff --git a/challenge-255/luca-ferrari/python/ch-2.python b/challenge-255/luca-ferrari/python/ch-2.python
index dc37e2ad93..6493199950 100644
--- a/challenge-255/luca-ferrari/python/ch-2.python
+++ b/challenge-255/luca-ferrari/python/ch-2.python
@@ -1,7 +1,7 @@
#!python
#
-# Perl Weekly Challenge 254
+# Perl Weekly Challenge 255
# Task 2
#
# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-254>
@@ -12,17 +12,16 @@ import sys
# task implementation
# the return value will be printed
def task_2( args ):
- word = args[ 0 ].lower()
- vowels = list( reversed( list( filter( lambda x: x in ('a','e','i','o','u'), word ) ) ) )
- output = ''
- for letter in word:
- if letter not in ( 'a', 'e', 'i', 'o', 'u' ) or len( vowels ) == 0
- output += letter
- else:
- output += vowels.pop( 0 )
-
- return output
+ paragraph = args[ 0 ]
+ banned = args[ 1 ]
+ classification = {}
+ for w in paragraph.split():
+ if w != banned:
+ if not w in classification:
+ classification[ w ] = 0
+ classification[ w ] += 1
+ return sorted( classification.items() )[ -1 ][ 0 ]
# invoke the main without the command itself
if __name__ == '__main__':
diff --git a/challenge-255/luca-ferrari/raku/ch-1.raku b/challenge-255/luca-ferrari/raku/ch-1.raku
new file mode 100644
index 0000000000..9fe8930de6
--- /dev/null
+++ b/challenge-255/luca-ferrari/raku/ch-1.raku
@@ -0,0 +1,18 @@
+#!raku
+
+#
+# Perl Weekly Challenge 255
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+#
+
+sub MAIN( Str :$a, Str :$b where { $b.chars == $a.chars + 1 } ) {
+ my $classification = BagHash.new: $b.comb;
+ for $a.comb {
+ $classification{ $_ }--;
+ $classification{ $_ }:delete if ( $classification{ $_ } <= 0 );
+ }
+
+ $classification.keys.head.say;
+}
diff --git a/challenge-255/luca-ferrari/raku/ch-2.raku b/challenge-255/luca-ferrari/raku/ch-2.raku
new file mode 100644
index 0000000000..ab74cfd5d9
--- /dev/null
+++ b/challenge-255/luca-ferrari/raku/ch-2.raku
@@ -0,0 +1,16 @@
+#!raku
+
+#
+# Perl Weekly Challenge 255
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-255>
+#
+
+sub MAIN( Str :$p is copy, Str :$w ) {
+
+ $p ~~ s:g/ \W* $w \W+/ /;
+ my $classification = Bag.new: $p.split( / \W+ /, :skip-empty );
+ $classification.keys.grep( { $classification{ $_ } == $classification.values.max } ).first.say;
+
+}