aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/luca-ferrari/postgresql
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2024-01-08 10:17:22 +0100
committerLuca Ferrari <fluca1978@gmail.com>2024-01-08 11:12:16 +0100
commit080fde56fb9825f48e63c97840234fbde25fedaa (patch)
treed8ca72df8e07e7645657406b7638c113f3abc008 /challenge-251/luca-ferrari/postgresql
parent9a485c9bac8e3887b165d67c9aa81d71cdd42f01 (diff)
downloadperlweeklychallenge-club-080fde56fb9825f48e63c97840234fbde25fedaa.tar.gz
perlweeklychallenge-club-080fde56fb9825f48e63c97840234fbde25fedaa.tar.bz2
perlweeklychallenge-club-080fde56fb9825f48e63c97840234fbde25fedaa.zip
PWC 251
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
Diffstat (limited to 'challenge-251/luca-ferrari/postgresql')
-rw-r--r--challenge-251/luca-ferrari/postgresql/ch-1.plperl23
-rw-r--r--challenge-251/luca-ferrari/postgresql/ch-1.sql29
-rw-r--r--challenge-251/luca-ferrari/postgresql/ch-2.plperl63
-rw-r--r--challenge-251/luca-ferrari/postgresql/ch-2.sql16
4 files changed, 131 insertions, 0 deletions
diff --git a/challenge-251/luca-ferrari/postgresql/ch-1.plperl b/challenge-251/luca-ferrari/postgresql/ch-1.plperl
new file mode 100644
index 0000000000..7152e60c8a
--- /dev/null
+++ b/challenge-251/luca-ferrari/postgresql/ch-1.plperl
@@ -0,0 +1,23 @@
+--
+-- Perl Weekly Challenge 251
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc251;
+
+CREATE OR REPLACE FUNCTION
+pwc251.task1_plperl( int[] )
+RETURNS int
+AS $CODE$
+ my ( $nums ) = @_;
+ my $sum = 0;
+
+ for ( 0 .. $nums->@* - 2 ) {
+ next if $_ % 2 != 0;
+ $sum += $nums->@[ $_ ] . $nums->@[ $_ + 1 ];
+ }
+
+ return $sum;
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-251/luca-ferrari/postgresql/ch-1.sql b/challenge-251/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..83c215c5f6
--- /dev/null
+++ b/challenge-251/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,29 @@
+--
+-- Perl Weekly Challenge 251
+-- Task 1
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc251;
+
+CREATE OR REPLACE FUNCTION
+pwc251.task1_plpgsql( nums int[] )
+RETURNS int
+AS $CODE$
+DECLARE
+ s text;
+ v int := 0;
+BEGIN
+ FOR i IN 1 .. array_length( nums, 1 ) - 1 LOOP
+ IF i % 2 = 0 THEN
+ CONTINUE;
+ END IF;
+
+ v := v + ( nums[ i ]::text || nums[ i + 1 ]::text )::int;
+ END LOOP;
+
+ RETURN v;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-251/luca-ferrari/postgresql/ch-2.plperl b/challenge-251/luca-ferrari/postgresql/ch-2.plperl
new file mode 100644
index 0000000000..32d43ba6ef
--- /dev/null
+++ b/challenge-251/luca-ferrari/postgresql/ch-2.plperl
@@ -0,0 +1,63 @@
+--
+-- Perl Weekly Challenge 251
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc251;
+
+/**
+ * example
+ testdb=> select pwc251.task2_plperl( array[ [ 3, 7, 8],
+ [ 9, 11, 13],
+ [15, 16, 17]] );
+ task2_plperl
+--------------
+ 15
+(1 row)
+*/
+CREATE OR REPLACE FUNCTION
+pwc251.task2_plperl( int[][] )
+RETURNS int
+AS $CODE$
+ my ( $matrix ) = @_;
+
+ my $max_col = undef;
+
+ # search the max column value
+ my @max_col_indexes;
+ for my $col ( 0 .. $matrix->@[ 0 ]->@* - 1 ) {
+ $max_col_index[ $col ] = undef;
+
+ for my $row ( 0 .. $matrix->@* - 1 ) {
+ $max_col_index[ $col ] = $matrix->@[ $row ]->[ $col ] if ( ! $max_col_index[ $col ] || $max_col_index[ $col ] < $matrix->@[ $row ]->[ $col ] );
+ }
+ }
+ for my $row ( 0 .. $matrix->@* - 1 ) {
+ for my $col ( 0 .. $matrix->@[ $row ]->@* - 1 ) {
+ $max_col = $matrix->@[ $row ]->[ $col ] if ( ! $max_col || $max_col < $matrix->@[ $row ]->[ $col ] );
+ }
+ }
+
+ for my $row ( 0 .. $matrix->@* - 1 ) {
+ my $current_min = undef;
+ my $current_min_index = 0;
+
+ for my $col ( 0 .. $matrix->@[ $row ]->@* - 1 ) {
+ if ( ! $current_min || $matrix->@[ $row ]->[ $col ] < $current_min ) {
+ $current_min = $matrix->@[ $row ]->[ $col ];
+ $current_min_index = $col;
+ }
+ }
+
+ if ( $current_min == $max_col_index[ $current_min_index ] ) {
+ return $current_min;
+ }
+
+
+ }
+
+ return -1;
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-251/luca-ferrari/postgresql/ch-2.sql b/challenge-251/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..fcdf6a6f9a
--- /dev/null
+++ b/challenge-251/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,16 @@
+--
+-- Perl Weekly Challenge 251
+-- Task 2
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc251;
+
+CREATE OR REPLACE FUNCTION
+pwc251.task2_plpgsql( matrix int[][] )
+RETURNS int
+AS $CODE$
+ SELECT pwc251.task2_plperl( matrix );
+$CODE$
+LANGUAGE sql;