From c735740ed7d4730b1ffeeae9c42353e5a3f0603c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Wed, 29 Dec 2021 11:24:17 +0100 Subject: Task 2 in PostgreSQL done --- challenge-145/luca-ferrari/postgresql/ch-2.sql | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-145/luca-ferrari/postgresql/ch-2.sql 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; -- cgit