aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2023-12-18 09:34:01 +0100
committerLuca Ferrari <fluca1978@gmail.com>2023-12-18 10:27:13 +0100
commite5c6087814a9ecd97539337c40f29f68c718aa40 (patch)
tree5f8a6319e9e4fb54cf9737e8b5388170427adc2e
parenta09beeb342d8937c474e7356b6c6dba565108065 (diff)
downloadperlweeklychallenge-club-e5c6087814a9ecd97539337c40f29f68c718aa40.tar.gz
perlweeklychallenge-club-e5c6087814a9ecd97539337c40f29f68c718aa40.tar.bz2
perlweeklychallenge-club-e5c6087814a9ecd97539337c40f29f68c718aa40.zip
PWC 248
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 Python done Task 2 Python done
-rw-r--r--challenge-248/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-7.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-8.txt1
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-1.plperl52
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-1.sql31
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-2.plperl28
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-2.sql32
-rw-r--r--challenge-248/luca-ferrari/python/ch-1.py37
-rw-r--r--challenge-248/luca-ferrari/python/ch-2.py42
-rw-r--r--challenge-248/luca-ferrari/raku/ch-1.p624
-rw-r--r--challenge-248/luca-ferrari/raku/ch-2.p627
16 files changed, 281 insertions, 0 deletions
diff --git a/challenge-248/luca-ferrari/blog-1.txt b/challenge-248/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..5b808ae612
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1
diff --git a/challenge-248/luca-ferrari/blog-2.txt b/challenge-248/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..9c7687d808
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2
diff --git a/challenge-248/luca-ferrari/blog-3.txt b/challenge-248/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..d253c3c55e
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1plperl
diff --git a/challenge-248/luca-ferrari/blog-4.txt b/challenge-248/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..a061387068
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2plperl
diff --git a/challenge-248/luca-ferrari/blog-5.txt b/challenge-248/luca-ferrari/blog-5.txt
new file mode 100644
index 0000000000..b25bbbb900
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-5.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1plpgsql
diff --git a/challenge-248/luca-ferrari/blog-6.txt b/challenge-248/luca-ferrari/blog-6.txt
new file mode 100644
index 0000000000..b598a001f1
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-6.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2plpgsql
diff --git a/challenge-248/luca-ferrari/blog-7.txt b/challenge-248/luca-ferrari/blog-7.txt
new file mode 100644
index 0000000000..1aa9fe6d54
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-7.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1python
diff --git a/challenge-248/luca-ferrari/blog-8.txt b/challenge-248/luca-ferrari/blog-8.txt
new file mode 100644
index 0000000000..726a79305a
--- /dev/null
+++ b/challenge-248/luca-ferrari/blog-8.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2python
diff --git a/challenge-248/luca-ferrari/postgresql/ch-1.plperl b/challenge-248/luca-ferrari/postgresql/ch-1.plperl
new file mode 100644
index 0000000000..9a818cbc44
--- /dev/null
+++ b/challenge-248/luca-ferrari/postgresql/ch-1.plperl
@@ -0,0 +1,52 @@
+--
+-- Perl Weekly Challenge 248
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc248;
+
+CREATE OR REPLACE FUNCTION
+pwc248.task1_plperl( text, text )
+RETURNS TABLE( c char, distance int )
+AS $CODE$
+ my ( $string, $char ) = @_;
+
+ die "Use a single char!" if ( length( $char ) > 1 );
+ die "Not matching at all!" if ( $string !~ / $char /x );
+
+
+ my $min = sub {
+ my $current_min = $_[ 0 ];
+ shift;
+ for ( @_ ) {
+ $current_min = $_ if ( $_ < $current_min );
+ }
+
+ return $current_min;
+ };
+
+ my @distances;
+ my @matching;
+
+ my @letters = split //, $string;
+ for ( 0 .. @letters - 1 ) {
+ next if $letters[ $_ ] ne $char;
+ push @matching, $_;
+ }
+
+
+ for my $me ( 0 .. @letters - 1 ) {
+
+ $distances[ $me ] = 0 if ( $letters[ $me ] eq $char );
+ $distances[ $me ] = $min->( map { abs( $me - $_ ) } @matching ) if ( $letters[ $me ] ne $char );
+
+ return_next( { c => $letters[ $me ],
+ distance => $distances[ $me ] } );
+
+ }
+
+ return undef;
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-248/luca-ferrari/postgresql/ch-1.sql b/challenge-248/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..62d2638eb4
--- /dev/null
+++ b/challenge-248/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,31 @@
+--
+-- Perl Weekly Challenge 248
+-- Task 1
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc248;
+
+CREATE OR REPLACE FUNCTION
+pwc248.task1_plpgsql( s text, c char )
+RETURNS TABLE ( cc char, distance int )
+AS $CODE$
+DECLARE
+BEGIN
+ CREATE TEMPORARY TABLE IF NOT EXISTS distances( cc char, ind int );
+ TRUNCATE distances;
+
+ INSERT INTO distances
+ SELECT v, row_number() over ()
+ FROM regexp_split_to_table( s, '' ) v;
+
+
+ RETURN QUERY
+ SELECT d.cc, ( SELECT min( abs( d2.ind - d.ind ) )
+ FROM distances d2
+ WHERE d2.cc = c )
+ FROM distances d;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-248/luca-ferrari/postgresql/ch-2.plperl b/challenge-248/luca-ferrari/postgresql/ch-2.plperl
new file mode 100644
index 0000000000..324f20f373
--- /dev/null
+++ b/challenge-248/luca-ferrari/postgresql/ch-2.plperl
@@ -0,0 +1,28 @@
+--
+-- Perl Weekly Challenge 248
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc248;
+
+CREATE OR REPLACE FUNCTION
+pwc248.task2_plperl( int[] )
+RETURNS int[]
+AS $CODE$
+ my ( $matrix ) = @_;
+
+ my $result = [];
+
+ for my $row ( 0 .. $matrix->@* - 1 ) {
+ $result->[ $row ] = [];
+
+ for my $col ( 0 .. $matrix->@* - 1 ) {
+ $result->[ $row ][ $col ] = $matrix->[ $row ][ $col ] + $matrix->[ $row ][ $col + 1 ] + $matrix->[ $row + 1 ][ $col ] + $matrix->[ $row + 1 ][ $col + 1 ];
+ }
+ }
+
+ return $result;
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-248/luca-ferrari/postgresql/ch-2.sql b/challenge-248/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..c8d84fe66b
--- /dev/null
+++ b/challenge-248/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,32 @@
+--
+-- Perl Weekly Challenge 248
+-- Task 2
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc248;
+
+CREATE OR REPLACE FUNCTION
+pwc248.task2_plpgsql( matrix int[] )
+RETURNS SETOF int[]
+AS $CODE$
+DECLARE
+ current_row int[];
+BEGIN
+
+ FOR r IN 1 .. array_length( matrix, 1 ) - 1 LOOP
+
+ current_row = array[ array_length( matrix, 1 ) ];
+
+ FOR c IN 1 .. array_length( matrix, 2 ) - 1 LOOP
+ current_row[ c ] = matrix[ r ][ c ] + matrix[ r ][ c + 1 ] + matrix[ r + 1 ][ c ] + matrix[ r + 1 ][ c + 1 ];
+ END LOOP;
+
+ RETURN NEXT current_row;
+ END LOOP;
+
+RETURN;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-248/luca-ferrari/python/ch-1.py b/challenge-248/luca-ferrari/python/ch-1.py
new file mode 100644
index 0000000000..e43a827ec1
--- /dev/null
+++ b/challenge-248/luca-ferrari/python/ch-1.py
@@ -0,0 +1,37 @@
+#!python
+
+#
+# Perl Weekly Challenge 248
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+#
+
+import sys
+
+# task implementation
+def main( argv ):
+ string = argv[ 0 ]
+ char = argv[ 1 ]
+
+ matching = []
+ for i in range( 0, len( string ) ):
+ c = string[ i ]
+ if c == char:
+ matching.append( i )
+
+ distances = []
+ for i in range( 0, len( string ) ):
+ if string[ i ] == char:
+ distances.append( 0 )
+ else:
+ distances.append( min( map( lambda x: abs( i - x ), matching ) ) )
+
+ print( ', '.join( map( str, distances ) ) )
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ main( sys.argv[ 1: ] )
+
+
diff --git a/challenge-248/luca-ferrari/python/ch-2.py b/challenge-248/luca-ferrari/python/ch-2.py
new file mode 100644
index 0000000000..e056a1b792
--- /dev/null
+++ b/challenge-248/luca-ferrari/python/ch-2.py
@@ -0,0 +1,42 @@
+#!python
+
+#
+# Perl Weekly Challenge 248
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+#
+
+import sys
+
+# task implementation
+def main( argv ):
+ matrix = argv
+ output = []
+
+ for r in range( 0, len( matrix ) - 1 ):
+
+ output.append( [] )
+
+ for c in range( 0, len( matrix[ r ] ) - 1 ):
+ output[ r ].append( matrix[ r ][ c ] + matrix[ r ][ c + 1 ] + matrix[ r + 1 ][ c ] + matrix[ r + 1 ][ c + 1 ] )
+
+ print( "\n".join( map( str, output ) ) )
+
+
+# invoke the main without the command itself
+if __name__ == '__main__':
+ matrix = [
+ [1, 0, 0, 0],
+ [0, 1, 0, 0],
+ [0, 0, 1, 0],
+ [0, 0, 0, 1]
+ ]
+
+ matrix = [
+ [1, 2, 3, 4],
+ [5, 6, 7, 8],
+ [9, 10, 11, 12]
+ ]
+
+ main( matrix )
diff --git a/challenge-248/luca-ferrari/raku/ch-1.p6 b/challenge-248/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..893cd908a6
--- /dev/null
+++ b/challenge-248/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#!raku
+
+#
+# Perl Weekly Challenge 248
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+#
+
+sub MAIN( Str :$string, Str :$char where { $char.chars == 1 } ) {
+
+ my @distances;
+ my @matching;
+ my @letters = $string.comb;
+
+ @matching.push: $_ if ( $char eq @letters[ $_ ] ) for 0 ..^ @letters.elems;
+
+ for 0 ..^ @letters.elems -> $me {
+ @distances[ $me ] = 0 and next if ( $char ~~ @letters[ $me ] );
+ @distances[ $me ] = @matching.map( { abs( $_ - $me ) } ).min;
+ }
+
+ @distances.join( ', ' ).say;
+}
diff --git a/challenge-248/luca-ferrari/raku/ch-2.p6 b/challenge-248/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..7e5aee79e7
--- /dev/null
+++ b/challenge-248/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,27 @@
+#!raku
+
+#
+# Perl Weekly Challenge 248
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/>
+#
+
+sub MAIN() {
+ my $a = [
+ [1, 2, 3, 4],
+ [5, 6, 7, 8],
+ [9, 10, 11, 12]
+ ];
+
+
+ my $b = [];
+ for 0 ..^ $a.elems - 1 -> $row {
+ $b[ $row ].push: [];
+ for 0 ..^ $a[ $row ].elems - 1 -> $col {
+ $b[ $row ][ $col ] = $a[ $row ][ $col ] + $a[ $row ][ $col + 1 ] + $a[ $row + 1 ][ $col ] + $a[ $row + 1 ][ $col + 1 ];
+ }
+ }
+
+ $b.join( "\n" ).say;
+}