aboutsummaryrefslogtreecommitdiff
path: root/challenge-145
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2021-12-29 11:24:17 +0100
committerLuca Ferrari <fluca1978@gmail.com>2021-12-29 11:24:17 +0100
commitc735740ed7d4730b1ffeeae9c42353e5a3f0603c (patch)
tree5e92fc3731fb396a5fa4de73879f4890477097bf /challenge-145
parent4d9d354f7e8fb47afbd3ed6c5805dc837a9e8d29 (diff)
downloadperlweeklychallenge-club-c735740ed7d4730b1ffeeae9c42353e5a3f0603c.tar.gz
perlweeklychallenge-club-c735740ed7d4730b1ffeeae9c42353e5a3f0603c.tar.bz2
perlweeklychallenge-club-c735740ed7d4730b1ffeeae9c42353e5a3f0603c.zip
Task 2 in PostgreSQL done
Diffstat (limited to 'challenge-145')
-rw-r--r--challenge-145/luca-ferrari/postgresql/ch-2.sql50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-145/luca-ferrari/postgresql/ch-2.sql b/challenge-145/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..0319ca15d8
--- /dev/null
+++ b/challenge-145/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,50 @@
+/**
+testdb=> select * from f_eertree( 'redivider' );
+current_root | string
+--------------+-----------
+r | redivider
+e | edivide
+d | divid
+i | ivi
+(4 rows)
+
+testdb=> select * from f_eertree( 'hello' );
+current_root | string
+--------------+--------
+l | ll
+(1 row)
+
+*/
+CREATE OR REPLACE FUNCTION
+f_eertree( s text )
+RETURNS TABLE( current_root char, string text )
+AS $CODE$
+DECLARE
+ current int;
+ other int;
+ other_root char;
+BEGIN
+
+ FOR current IN 1 .. length( s ) LOOP
+ current_root := substring( s FROM current FOR 1 );
+
+ FOR other IN current + 1 .. length( s ) LOOP
+ other_root := substring( s FROM other FOR 1 );
+ IF other_root <> current_root THEN
+ CONTINUE;
+ END IF;
+
+ string := substring( s, current, other - current + 1 );
+
+ IF string = reverse( string ) THEN
+ RETURN NEXT;
+ END IF;
+
+ END LOOP;
+ END LOOP;
+
+ RETURN;
+
+END
+$CODE$
+LANGUAGE plpgsql;