From d5c82371fcc0e58f01b3457e71d7fff99a709eb4 Mon Sep 17 00:00:00 2001
From: Luca Ferrari <fluca1978@gmail.com>
Date: Mon, 15 Jul 2024 12:24:13 +0200
Subject: PWC 278

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 PL/Java done
Task 2 PL/Java done

Task 1 Python done
Task 2 Python done
---
 challenge-278/luca-ferrari/plpgsql/ch-2.sql | 44 +++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 challenge-278/luca-ferrari/plpgsql/ch-2.sql

(limited to 'challenge-278/luca-ferrari/plpgsql/ch-2.sql')

diff --git a/challenge-278/luca-ferrari/plpgsql/ch-2.sql b/challenge-278/luca-ferrari/plpgsql/ch-2.sql
new file mode 100644
index 0000000000..b271d36da0
--- /dev/null
+++ b/challenge-278/luca-ferrari/plpgsql/ch-2.sql
@@ -0,0 +1,44 @@
+--
+-- Perl Weekly Challenge 278
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-278>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc278;
+
+CREATE OR REPLACE FUNCTION
+pwc278.task2_plpgsql( w text, c char )
+RETURNS text
+AS $CODE$
+
+   WITH letters( l, i ) AS (
+   	SELECT *
+	FROM regexp_split_to_table( w, '' )
+	WITH ORDINALITY AS v( text, int )
+   )
+   , first_part AS (
+     SELECT l
+     FROM letters
+     WHERE i <= position( c IN w )
+     ORDER BY l
+   )
+   , second_part AS (
+
+     SELECT l
+     FROM letters
+     WHERE i > position( c IN w )
+     ORDER BY i
+   )
+   , all_letters AS (
+   SELECT l
+   FROM first_part
+   UNION ALL
+   SELECT l
+   FROM second_part
+   )
+   SELECT string_agg( l, '' )
+   FROM all_letters
+   ;
+
+$CODE$
+LANGUAGE sql;
-- 
cgit 

