aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/luca-ferrari/postgresql/ch-1.sql
blob: 477bf9703f33ce9cf596199606fd911dcf455f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
--
-- Perl Weekly Challenge 207
-- Task 1
--
-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-207/>
--

CREATE SCHEMA IF NOT EXISTS pwc207;

CREATE OR REPLACE FUNCTION
pwc207.task1_plpgsql( w text[] )
RETURNS SETOF text
AS $CODE$
DECLARE
	current_word text;
	current_row  text;
	letters_found int;
BEGIN

	CREATE TEMPORARY TABLE IF NOT EXISTS keyboard( k text );
	TRUNCATE keyboard;
	INSERT INTO keyboard( k )
	VALUES( 'qwertyuiop' ), ( 'asdfghjkl' ), ( 'zxcvbnm' );

	FOREACH current_word IN ARRAY w LOOP
		FOR current_row IN SELECT k FROM keyboard LOOP
			letters_found := 0;

			SELECT count(*)
			INTO   letters_found
			FROM   regexp_split_to_table( current_word, '' ) ww
			JOIN   regexp_split_to_table( current_row, '' ) kk
			ON     ww = kk;

			IF letters_found = length( current_word ) THEN
			   RETURN NEXT current_word;
			   EXIT;
			END IF;
		END LOOP;
	END LOOP;

RETURN;
END
$CODE$
LANGUAGE plpgsql;