aboutsummaryrefslogtreecommitdiff
path: root/challenge-277/luca-ferrari/plpgsql
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-07-29 11:46:29 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-07-29 11:46:29 +0100
commit7d6e5a9e7646e9254fdbaf7f3a3cac47c567f44f (patch)
treeca8617a29d925b53219b818ada7534e89cb2786d /challenge-277/luca-ferrari/plpgsql
parent5eca255ddb9d8edbf062392e471d92273e412d7d (diff)
downloadperlweeklychallenge-club-7d6e5a9e7646e9254fdbaf7f3a3cac47c567f44f.tar.gz
perlweeklychallenge-club-7d6e5a9e7646e9254fdbaf7f3a3cac47c567f44f.tar.bz2
perlweeklychallenge-club-7d6e5a9e7646e9254fdbaf7f3a3cac47c567f44f.zip
- Added solutions by Eric Cheung.
- Added solutions by Thomas Kohler. - Added solutions by PokGoPun. - Added solutions by Mark Anderson. - Added solutions by Feng Chang. - Added solutions by E. Choroba.
Diffstat (limited to 'challenge-277/luca-ferrari/plpgsql')
-rw-r--r--challenge-277/luca-ferrari/plpgsql/ch-1.sql25
-rw-r--r--challenge-277/luca-ferrari/plpgsql/ch-2.sql28
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-277/luca-ferrari/plpgsql/ch-1.sql b/challenge-277/luca-ferrari/plpgsql/ch-1.sql
new file mode 100644
index 0000000000..70bfb4aab7
--- /dev/null
+++ b/challenge-277/luca-ferrari/plpgsql/ch-1.sql
@@ -0,0 +1,25 @@
+CREATE OR REPLACE FUNCTION
+pwc277.task1_plpgsql( words1 text[], words2 text[] )
+RETURNS int
+AS $CODE$
+
+ WITH w1 AS (
+ SELECT w::text
+ FROM unnest( words1 ) w
+ GROUP BY w
+ HAVING count(*) = 1
+ ),
+ w2 AS (
+ SELECT w::text
+ FROM unnest( words2 ) w
+ GROUP BY w
+ HAVING count(*) = 1
+ )
+ SELECT count( l.w )
+ FROM w1 l, w2 r
+ WHERE l.w = r.w
+
+$CODE$
+LANGUAGE sql;
+
+
diff --git a/challenge-277/luca-ferrari/plpgsql/ch-2.sql b/challenge-277/luca-ferrari/plpgsql/ch-2.sql
new file mode 100644
index 0000000000..84f99da312
--- /dev/null
+++ b/challenge-277/luca-ferrari/plpgsql/ch-2.sql
@@ -0,0 +1,28 @@
+CREATE OR REPLACE FUNCTION
+pwc277.task2_plpgsql( nums int[] )
+RETURNS int
+AS $CODE$
+DECLARE
+ strong_counter int := 0;
+ current int := 0;
+BEGIN
+
+ FOR i IN 1 .. array_length( nums, 1 ) - 1 LOOP
+ FOR j in ( i + 1 ) .. array_length( nums, 1 ) LOOP
+ SELECT min( x )
+ INTO current
+ FROM unnest( array[ nums[ i ], nums[ j ] ] ) x;
+
+ IF current > abs( nums[ i ] - nums[ j ] ) AND nums[ i ] <> nums[ j ] THEN
+ strong_counter := strong_counter + 1;
+ END IF;
+ END LOOP;
+ END LOOP;
+
+ RETURN strong_counter;
+
+END
+$CODE$
+LANGUAGE plpgsql;
+
+