diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-24 22:28:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-24 22:28:03 +0100 |
| commit | 516e5f90c2e01f445c9d554282fc6bec60a79672 (patch) | |
| tree | 26d07cb3b9ce89bcd533e0a30eba7545466d3ee4 /challenge-275 | |
| parent | efeb4b1121b3df61e39c3ea65d82245ad0ac7359 (diff) | |
| parent | e3413e5215e89c8512218669be7701def126c9cd (diff) | |
| download | perlweeklychallenge-club-516e5f90c2e01f445c9d554282fc6bec60a79672.tar.gz perlweeklychallenge-club-516e5f90c2e01f445c9d554282fc6bec60a79672.tar.bz2 perlweeklychallenge-club-516e5f90c2e01f445c9d554282fc6bec60a79672.zip | |
Merge pull request #10315 from fluca1978/PWC275
PWC 275
Diffstat (limited to 'challenge-275')
21 files changed, 484 insertions, 0 deletions
diff --git a/challenge-275/luca-ferrari/blog-1.txt b/challenge-275/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..db1be1abf8 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1 diff --git a/challenge-275/luca-ferrari/blog-10.txt b/challenge-275/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..6bd6046323 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge275.html#task2pljava diff --git a/challenge-275/luca-ferrari/blog-2.txt b/challenge-275/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..34fc986234 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2 diff --git a/challenge-275/luca-ferrari/blog-3.txt b/challenge-275/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..0ba72be87f --- /dev/null +++ b/challenge-275/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1plperl diff --git a/challenge-275/luca-ferrari/blog-4.txt b/challenge-275/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..bc30ee141c --- /dev/null +++ b/challenge-275/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2plperl diff --git a/challenge-275/luca-ferrari/blog-5.txt b/challenge-275/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..3a96070b9e --- /dev/null +++ b/challenge-275/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1plpgsql diff --git a/challenge-275/luca-ferrari/blog-6.txt b/challenge-275/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..da55d40190 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2plpgsql diff --git a/challenge-275/luca-ferrari/blog-7.txt b/challenge-275/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..637169aa7a --- /dev/null +++ b/challenge-275/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1python diff --git a/challenge-275/luca-ferrari/blog-8.txt b/challenge-275/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..5070b3d968 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task2python diff --git a/challenge-275/luca-ferrari/blog-9.txt b/challenge-275/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..6d122588c0 --- /dev/null +++ b/challenge-275/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/06/24/PerlWeeklyChallenge275.html#task1pljava diff --git a/challenge-275/luca-ferrari/pljava/pom.xml b/challenge-275/luca-ferrari/pljava/pom.xml new file mode 100644 index 0000000000..ea10099725 --- /dev/null +++ b/challenge-275/luca-ferrari/pljava/pom.xml @@ -0,0 +1,72 @@ + +<project + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > + <modelVersion>4.0.0</modelVersion> + + <groupId>PWC</groupId> + <artifactId> + PWC275 + </artifactId> + <version> + 1 + </version> + + <name>Perl Weekly Challenge 275 with package PWC275</name> + <description>Implementation of the tasks in PL/Java for PWC 275</description> + + <properties> + <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>pljava-api</artifactId> + <version>1.6.6</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.1</version> + <configuration> + <release>9</release> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>2.6</version> + <configuration> + <archive> + <manifest> + <!-- This identifies and version-stamps the jar. + Not essential, but easy and useful. --> + <addDefaultImplementationEntries> + true + </addDefaultImplementationEntries> + </manifest> + + <manifestSections> + <!-- This identifies a file in the jar named + pljava.ddr as an SQLJDeploymentDescriptor. --> + <manifestSection> + <name>pljava.ddr</name> + <manifestEntries> + <SQLJDeploymentDescriptor> + true + </SQLJDeploymentDescriptor> + </manifestEntries> + </manifestSection> + </manifestSections> + </archive> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/challenge-275/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-275/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..2e27bd455b --- /dev/null +++ b/challenge-275/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,69 @@ + + + +package PWC275; + +/** + * PL/Java implementation for PWC 275 + * Task 1 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> + * + * + * 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/PWC275-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', true ); + select sqlj.set_classpath( 'public', 'PWC275' ); + + select pwc275.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', 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.regex.*; +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 = "pwc275", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( String sentence, String[] keys ) throws SQLException { + logger.log( Level.INFO, "Entering pwc275.task1_pljava" ); + + List<Pattern> patterns = Stream.of( keys ) + .map( letter -> { return Pattern.compile( String.format( "%s", letter ), Pattern.CASE_INSENSITIVE ); } ) + .collect( Collectors.toList() ); + + return (int) Stream.of( sentence.split( "\\s+" ) ) + .filter( word -> { + for ( Pattern p : patterns ) + if ( p.matcher( word ).find() ) + return false; + + return true; + } ).count(); + + } +} diff --git a/challenge-275/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-275/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..9778016704 --- /dev/null +++ b/challenge-275/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,74 @@ + + + +package PWC275; + +/** + * PL/Java implementation for PWC 275 + * Task 2 + * See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> + * + * + * 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/PWC275-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', true ); + select sqlj.set_classpath( 'public', 'PWC275' ); + + select pwc275.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC275-1.jar', 'PWC275', 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.regex.*; +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 = "pwc275", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String task2_pljava( String text ) throws SQLException { + logger.log( Level.INFO, "Entering pwc275.task2_pljava" ); + + Pattern digit = Pattern.compile( "\\d", Pattern.CASE_INSENSITIVE ); + final String[] previous = new String[ 1 ]; + + return Stream.of( text.split( "" ) ) + .map( current -> { + if ( digit.matcher( current ).find() ) { + // a digit + + current = String.format( "%c", ( (int) previous[ 0 ].charAt( 0 ) + Integer.parseInt( current ) ) ); + + } + else { + previous[ 0 ] = current; + } + + return current; + } ) + .collect( Collectors.joining("") ); + } +} diff --git a/challenge-275/luca-ferrari/plperl/ch-1.plperl b/challenge-275/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..6fb40b9988 --- /dev/null +++ b/challenge-275/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task1_plperl( text, text[] ) +RETURNS int +AS $CODE$ + + my ( $string, $keys ) = @_; + my @ok_words; + + for my $word ( split /\s+/, lc $string ) { + my $ok = 1; + for my $wrong_key ( $keys->@* ) { + $ok = 0 if ( $word =~ /$wrong_key/ ); + last if ( ! $ok ); + } + + push @ok_words, $word if ( $ok ); + } + + return scalar( @ok_words ); + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-275/luca-ferrari/plperl/ch-2.plperl b/challenge-275/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..1325119d11 --- /dev/null +++ b/challenge-275/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,34 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task2_plperl( text ) +RETURNS text +AS $CODE$ + + my ( $string ) = @_; + my @alphabet = 'a' .. 'z'; + $string = lc $string; + my @result; + my $previous; + + for my $letter ( split //, $string ) { + if ( $letter =~ /[a-z]/ ) { + $previous = $letter; + } + else { + $letter = chr( ord( 'a' ) + int( $letter ) ); + } + + push @result, $letter; + } + + return join( '', @result ); + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-275/luca-ferrari/plpgsql/ch-1.sql b/challenge-275/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..4be873b80d --- /dev/null +++ b/challenge-275/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task1_plpgsql( s text, k text[] ) +RETURNS int +AS $CODE$ +DECLARE + ok boolean; + current_k text; + current text; + word_counter int := 0; +BEGIN + + FOR current IN SELECT word FROM regexp_split_to_table( s, '\s+' ) word LOOP + ok := true; + + FOREACH current_k IN ARRAY k LOOP + IF current ~ current_k THEN + ok := false; + END IF; + END LOOP; + + IF ok THEN + word_counter := word_counter + 1; + END IF; + END LOOP; + + RETURN word_counter; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-275/luca-ferrari/plpgsql/ch-2.sql b/challenge-275/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..3d5642972d --- /dev/null +++ b/challenge-275/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,32 @@ +-- +-- Perl Weekly Challenge 275 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +-- + +CREATE SCHEMA IF NOT EXISTS pwc275; + +CREATE OR REPLACE FUNCTION +pwc275.task2_plpgsql( s text ) +RETURNS text +AS $CODE$ +DECLARE + output text := ''; + previous text; + c text; +BEGIN + + FOR c IN SELECT v FROM regexp_split_to_table( s, '' ) v LOOP + IF c ~ '[a-z]' THEN + previous := c; + ELSE + c := chr( c::int + ascii( 'a' ) ); + END IF; + + output := output || c; + END LOOP; + + return output; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-275/luca-ferrari/python/ch-1.py b/challenge-275/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..f131b1e602 --- /dev/null +++ b/challenge-275/luca-ferrari/python/ch-1.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 275 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + sentence = args[ 0 ] + keys = args[ 1: ] + words_ok = 0 + + for word in sentence.lower().split(): + bad = 0 + for k in keys: + k = k.lower() + if k in word: + bad += 1 + break + + if bad == 0: + words_ok += 1 + + return words_ok + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-275/luca-ferrari/python/ch-2.py b/challenge-275/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..ebe16641b0 --- /dev/null +++ b/challenge-275/luca-ferrari/python/ch-2.py @@ -0,0 +1,32 @@ +#!python + +# +# Perl Weekly Challenge 275 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + string = args[ 0 ] + previous = None + result = "" + + for c in string: + if c.isdigit(): + c = chr( int( c ) + 97 ) + else: + previous = c + + result += c + + return result + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-275/luca-ferrari/raku/ch-1.raku b/challenge-275/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..6d8a2faac3 --- /dev/null +++ b/challenge-275/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,25 @@ +#!raku + +# +# Perl Weekly Challenge 275 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +# + +sub MAIN( Str $sentence, *@keys ) { + my @ok-words; + + for $sentence.lc.split( / \s+ / ) -> $word { + my $ok = True; + + for @keys -> $needle { + $ok = False if ( $word ~~ / $needle / ); + last if ! $ok; + } + + @ok-words.push: $word if $ok; + } + + @ok-words.elems.say; +} diff --git a/challenge-275/luca-ferrari/raku/ch-2.raku b/challenge-275/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..a3f40351f0 --- /dev/null +++ b/challenge-275/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!raku + +# +# Perl Weekly Challenge 275 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-275> +# + +sub MAIN( Str $string where { $string ~~ / ^ <[a..zA..Z]> <[a..zA..Z0..9]>+ $ / } ) { + my $previous; + my @result; + + my $index = 0; + my %alphabet; + %alphabet{ $_ } = $index++ for 'a' .. 'z'; + + + + $string.comb.map( -> $current is copy { + if ( $current.lc ~~ / <[a..z]> / ) { + # it is a letter + $previous = $current; + } + else { + # it is a number + $current = %alphabet.pairs.grep( { $_.value == ( %alphabet{ $previous } + $current.Int ) } )[ 0 ].key; + } + + @result.push: $current; + } ); + + + @result.join.say; +} |
