From 093553020c7cf39461a2be0e1c8851c983ba863b Mon Sep 17 00:00:00 2001
From: Luca Ferrari <fluca1978@gmail.com>
Date: Mon, 26 Feb 2024 09:28:02 +0100
Subject: PWC 258

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 Python done
Task 2 Python done

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

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

diff --git a/challenge-258/luca-ferrari/plpgsql/ch-2.sql b/challenge-258/luca-ferrari/plpgsql/ch-2.sql
new file mode 100644
index 0000000000..3241facae4
--- /dev/null
+++ b/challenge-258/luca-ferrari/plpgsql/ch-2.sql
@@ -0,0 +1,32 @@
+--
+-- Perl Weekly Challenge 258
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-258>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc258;
+
+CREATE OR REPLACE FUNCTION
+pwc258.task2_plpgsql( k int, nums int[] )
+RETURNS int
+AS $CODE$
+DECLARE
+	v_sum int := 0;
+	count_of_ones int := 0;
+BEGIN
+	FOR i IN 1 .. array_length( nums, 1 ) LOOP
+		SELECT sum( v::int )
+		INTO count_of_ones
+		FROM regexp_split_to_table( i::bit( 8 )::text, '' ) v;
+
+		IF count_of_ones <> k THEN
+		   continue;
+		END IF;
+
+		v_sum := v_sum + nums[ i ];
+	END LOOP;
+
+	RETURN v_sum;
+END
+$CODE$
+LANGUAGE plpgsql;
-- 
cgit 

