aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-216/luca-ferrari/postgresql/ch-2.plperl50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-216/luca-ferrari/postgresql/ch-2.plperl b/challenge-216/luca-ferrari/postgresql/ch-2.plperl
new file mode 100644
index 0000000000..5316537322
--- /dev/null
+++ b/challenge-216/luca-ferrari/postgresql/ch-2.plperl
@@ -0,0 +1,50 @@
+--
+-- Perl Weekly Challenge 216
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-216/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc216;
+
+CREATE OR REPLACE FUNCTION
+pwc216.task2_plperl( text, text[] )
+RETURNS TABLE ( sticker text, run int, letter text )
+AS $CODE$
+ my ( $needle, $words ) = @_;
+ my $searching_for = {};
+
+ # create the bag
+ for ( split //, $needle ) {
+ $searching_for->{ $_ }++;
+ }
+
+ elog(INFO, "Valori? " . grep( { $_ >= 1 } values( $searching_for->%* ) ) );
+ my $run = 0;
+ while ( grep( { $_ >= 1 } values( $searching_for->%* ) ) ) {
+ $run++;
+ my $found = 0;
+
+ for my $letter ( keys $searching_for->%* ) {
+ next if ! $searching_for->{ $letter };
+ for my $word ( $words->@* ) {
+ if ( grep( { $_ eq $letter } split( //, $word ) ) ) {
+ $searching_for->{ $letter }--;
+ return_next( { run => $run, sticker => $word, letter => $letter } );
+ $found++;
+ last;
+ }
+ }
+
+
+ }
+
+ if ( ! $found ) {
+ elog(INFO, "Cannot find match with letter $letter in any word!" );
+ return undef;
+ }
+ }
+
+return undef;
+
+$CODE$
+LANGUAGE plperl;