aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-195/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-195/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-195/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-195/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-195/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-195/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-195/luca-ferrari/postgresql/ch-1.plperl40
-rw-r--r--challenge-195/luca-ferrari/postgresql/ch-1.sql31
-rw-r--r--challenge-195/luca-ferrari/postgresql/ch-2.plperl29
-rw-r--r--challenge-195/luca-ferrari/postgresql/ch-2.sql42
-rw-r--r--challenge-195/luca-ferrari/raku/ch-1.p613
-rw-r--r--challenge-195/luca-ferrari/raku/ch-2.p615
12 files changed, 176 insertions, 0 deletions
diff --git a/challenge-195/luca-ferrari/blog-1.txt b/challenge-195/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..2afdd83ab8
--- /dev/null
+++ b/challenge-195/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task1
diff --git a/challenge-195/luca-ferrari/blog-2.txt b/challenge-195/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..124f33a97c
--- /dev/null
+++ b/challenge-195/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task2
diff --git a/challenge-195/luca-ferrari/blog-3.txt b/challenge-195/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..fb3572a632
--- /dev/null
+++ b/challenge-195/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task1plperl
diff --git a/challenge-195/luca-ferrari/blog-4.txt b/challenge-195/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..d8193434ac
--- /dev/null
+++ b/challenge-195/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task2plperl
diff --git a/challenge-195/luca-ferrari/blog-5.txt b/challenge-195/luca-ferrari/blog-5.txt
new file mode 100644
index 0000000000..1864572ac2
--- /dev/null
+++ b/challenge-195/luca-ferrari/blog-5.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task1plpgsql
diff --git a/challenge-195/luca-ferrari/blog-6.txt b/challenge-195/luca-ferrari/blog-6.txt
new file mode 100644
index 0000000000..bc5e6b6762
--- /dev/null
+++ b/challenge-195/luca-ferrari/blog-6.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task2plpgsql
diff --git a/challenge-195/luca-ferrari/postgresql/ch-1.plperl b/challenge-195/luca-ferrari/postgresql/ch-1.plperl
new file mode 100644
index 0000000000..3ea8594add
--- /dev/null
+++ b/challenge-195/luca-ferrari/postgresql/ch-1.plperl
@@ -0,0 +1,40 @@
+-- Perl Weekly Challenge 195
+-- Task 1
+
+CREATE SCHEMA IF NOT EXISTS pwc195;
+
+CREATE OR REPLACE FUNCTION
+pwc195.task1_plperl( int, bool default true )
+RETURNS int
+AS $CODE$
+ my ( $n, $verbose ) = @_;
+ my @special_integers;
+
+ my $baggify = sub {
+ my ( $n ) = @_;
+ my $bag = {};
+ for my $digit ( split '', $n ) {
+ $bag->{ $digit }++;
+ }
+
+ return $bag;
+ };
+
+ my $has_no_repetitions = sub {
+ my ( $bag ) = @_;
+ for ( keys $bag->%* ) {
+ return 0 if $bag->{ $_ } != 1;
+ }
+
+ return 1;
+ };
+
+ for ( 1 .. $n ) {
+ push @special_integers, $_ if ( $has_no_repetitions->( $baggify->( $_ ) ) );
+ }
+
+ elog( INFO, "Found: " . join( ',', @special_integers ) ) if ( $ verbose );
+ return scalar @special_integers;
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-195/luca-ferrari/postgresql/ch-1.sql b/challenge-195/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..c6cbc28437
--- /dev/null
+++ b/challenge-195/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,31 @@
+-- Perl Weekly Challenge 195
+-- Task 1
+
+CREATE SCHEMA IF NOT EXISTS pwc195;
+
+CREATE OR REPLACE FUNCTION
+pwc195.task1_plpgsql( n int )
+RETURNS int
+AS $CODE$
+DECLARE
+ i int;
+ freq int;
+ counter int := 0;
+BEGIN
+ FOR i IN 1 .. n LOOP
+ SELECT count(*)
+ INTO freq
+ FROM regexp_split_to_table( i::text, '' ) as n(d)
+ GROUP BY d
+ ORDER BY 1 DESC;
+
+ IF freq = 1 THEN
+ counter := counter + 1;
+ END IF;
+
+ END LOOP;
+
+ RETURN counter;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-195/luca-ferrari/postgresql/ch-2.plperl b/challenge-195/luca-ferrari/postgresql/ch-2.plperl
new file mode 100644
index 0000000000..b15f6b215b
--- /dev/null
+++ b/challenge-195/luca-ferrari/postgresql/ch-2.plperl
@@ -0,0 +1,29 @@
+-- Perl Weekly Challenge 195
+-- Task 2
+
+CREATE SCHEMA IF NOT EXISTS pwc195;
+
+CREATE OR REPLACE FUNCTION
+pwc195.task2_plperl( int[] )
+RETURNS int
+AS $CODE$
+ my ( $array ) = @_;
+ # extract only evens
+ my @evens = grep { $_ % 2 == 0 } $array->@*;
+ # classify frequency
+ my $bag = {};
+ $bag->{ $_ }++ for ( @evens );
+
+
+ # sort by frequency and value
+ my @sorted_bag =
+ map { $_->[1] }
+ sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] }
+ map { [ $bag->{ $_ }, $_ ] } keys $bag->%*;
+
+
+ # the first value in the list is the
+ # one with the max frequency and the lowest value
+ return $sorted_bag[ 0 ];
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-195/luca-ferrari/postgresql/ch-2.sql b/challenge-195/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..633ffc8c4f
--- /dev/null
+++ b/challenge-195/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,42 @@
+-- Perl Weekly Challenge 195
+-- Task 2
+
+CREATE SCHEMA IF NOT EXISTS pwc195;
+
+/*
+testdb=> select pwc195.task2_plpgsql( array[1,2,2,3,4,4,4,5,6,6,6,6]::int[] );
+NOTICE: relation "nums" already exists, skipping
+ task2_plpgsql
+---------------
+ 6
+
+*/
+
+CREATE OR REPLACE FUNCTION
+pwc195.task2_plpgsql( list int[] )
+RETURNS int
+AS $CODE$
+DECLARE
+ current int;
+BEGIN
+ CREATE TEMPORARY TABLE IF NOT EXISTS nums( v int, f int default 1, primary key( v ) );
+ TRUNCATE TABLE nums;
+
+ FOREACH current IN ARRAY list LOOP
+ INSERT INTO nums AS frequency
+ SELECT current, 1
+ ON CONFLICT (v)
+ DO UPDATE SET f = frequency.f + 1;
+ END LOOP;
+
+ SELECT v
+ INTO current
+ FROM nums
+ WHERE v % 2 = 0
+ ORDER BY f DESC, v ASC
+ LIMIT 1;
+
+ RETURN current;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-195/luca-ferrari/raku/ch-1.p6 b/challenge-195/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..15012afdf2
--- /dev/null
+++ b/challenge-195/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,13 @@
+#!raku
+
+# Perl Weekly Challenge 195
+
+sub MAIN( Int $n where { $n > 0 }, Bool :$verbose = False ) {
+ my @special-integers;
+ for 1 .. $n {
+ @special-integers.push: $_ if $_.comb.Bag.values.max <= 1;
+ }
+
+ @special-integers.join( ',' ).say if ( $verbose );
+ @special-integers.elems.say;
+}
diff --git a/challenge-195/luca-ferrari/raku/ch-2.p6 b/challenge-195/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..707f429ed1
--- /dev/null
+++ b/challenge-195/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,15 @@
+#!raku
+
+# Perl Weekly Challenge 195
+
+sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) {
+ my $bag = @list.grep( * %% 2 ).Bag;
+ my $most-frequency = $bag.values.max;
+ my @most-frequent-evens;
+ for $bag.keys {
+ next if $bag{ $_ } != $most-frequency;
+ @most-frequent-evens.push: $_;
+ }
+
+ @most-frequent-evens.min.say;
+}