aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-17 17:08:00 +0100
committerGitHub <noreply@github.com>2022-10-17 17:08:00 +0100
commit29796882a2a205902861998445f34e0845cae8bc (patch)
treebf41310c3053c69a9356a5ee844f48e6f23a5315
parentb747e58709bb8648f0856141ec8848dc76cc9d02 (diff)
parent3a90b6c674db1f362ffc3ce64c5b2618d118383b (diff)
downloadperlweeklychallenge-club-29796882a2a205902861998445f34e0845cae8bc.tar.gz
perlweeklychallenge-club-29796882a2a205902861998445f34e0845cae8bc.tar.bz2
perlweeklychallenge-club-29796882a2a205902861998445f34e0845cae8bc.zip
Merge pull request #6924 from fluca1978/PWC187
Pwc187
-rw-r--r--challenge-187/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-187/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-187/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-187/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-187/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-187/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-187/luca-ferrari/postgresql/ch-1.plperl45
-rw-r--r--challenge-187/luca-ferrari/postgresql/ch-1.sql43
-rw-r--r--challenge-187/luca-ferrari/postgresql/ch-2.plperl51
-rw-r--r--challenge-187/luca-ferrari/postgresql/ch-2.sql15
-rw-r--r--challenge-187/luca-ferrari/raku/ch-1.p630
-rw-r--r--challenge-187/luca-ferrari/raku/ch-2.p630
12 files changed, 220 insertions, 0 deletions
diff --git a/challenge-187/luca-ferrari/blog-1.txt b/challenge-187/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..17ddd2e421
--- /dev/null
+++ b/challenge-187/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task1
diff --git a/challenge-187/luca-ferrari/blog-2.txt b/challenge-187/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..5af7d87940
--- /dev/null
+++ b/challenge-187/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task2
diff --git a/challenge-187/luca-ferrari/blog-3.txt b/challenge-187/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..0ac70011c4
--- /dev/null
+++ b/challenge-187/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task1plperl
diff --git a/challenge-187/luca-ferrari/blog-4.txt b/challenge-187/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..ff3239f6d5
--- /dev/null
+++ b/challenge-187/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task2plperl
diff --git a/challenge-187/luca-ferrari/blog-5.txt b/challenge-187/luca-ferrari/blog-5.txt
new file mode 100644
index 0000000000..06274847f6
--- /dev/null
+++ b/challenge-187/luca-ferrari/blog-5.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task1plpgsql
diff --git a/challenge-187/luca-ferrari/blog-6.txt b/challenge-187/luca-ferrari/blog-6.txt
new file mode 100644
index 0000000000..f073f961bf
--- /dev/null
+++ b/challenge-187/luca-ferrari/blog-6.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task2plpgsql
diff --git a/challenge-187/luca-ferrari/postgresql/ch-1.plperl b/challenge-187/luca-ferrari/postgresql/ch-1.plperl
new file mode 100644
index 0000000000..b4f2b0f93b
--- /dev/null
+++ b/challenge-187/luca-ferrari/postgresql/ch-1.plperl
@@ -0,0 +1,45 @@
+-- Perl Weekly Challenge 187
+-- Task 1
+
+CREATE SCHEMA IF NOT EXISTS pwc187;
+
+CREATE OR REPLACE FUNCTION
+pwc187.task1_plperl( text, text, text, text )
+RETURNS INT
+AS $CODE$
+use DateTime;
+my ( $foo_start, $foo_end, $bar_start, $bar_end ) = @_;
+my @dates;
+
+for ( $foo_start, $foo_end, $bar_start, $bar_end ) {
+ $_ =~ / ^ (\d{2}) - (\d{2}) $ /x;
+ push @dates, DateTime->new( year => 2022, day => $1, month => $2 );
+}
+
+my $days = 0;
+
+if ( DateTime->compare( $dates[ 0 ], $dates[ 3 ] ) > 0
+ || DateTime->compare( $dates[ 1 ], $dates[ 2 ] ) < 0 ) {
+ # one ends before the begin of the other or viceversa
+ $days = 0;
+}
+else {
+ my ( $start, $end );
+ $start = DateTime->compare( $dates[ 0 ], $dates[ 2 ] ) <= 0 ? $dates[ 0 ] : $dates[ 2 ];
+ $end = DateTime->compare( $dates[ 1 ], $dates[ 3 ] ) <= 0 ? $dates[ 1 ] : $dates[ 3 ];
+
+ while ( DateTime->compare( $start, $end ) <= 0 ) {
+ $days++ if ( DateTime->compare( $start, $dates[ 0 ] ) >= 0
+ && DateTime->compare( $start, $dates[ 1 ] ) <= 0
+ && DateTime->compare( $start, $dates[ 2 ] ) >= 0
+ && DateTime->compare( $start, $dates[ 3 ] ) <= 0 );
+ $start = $start->add( days => 1 );
+ }
+}
+
+
+
+return $days;
+
+$CODE$
+LANGUAGE plperlu;
diff --git a/challenge-187/luca-ferrari/postgresql/ch-1.sql b/challenge-187/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..7398ebc930
--- /dev/null
+++ b/challenge-187/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,43 @@
+-- Perl Weekly Challenge 187
+-- Task 1
+
+CREATE SCHEMA IF NOT EXISTS pwc187;
+
+CREATE OR REPLACE FUNCTION
+pwc187.task1_plpgsql( foo_start date, foo_end date, bar_start date, bar_end date )
+RETURNS int
+AS $CODE$
+DECLARE
+ day_start date;
+ day_end date;
+ day_count int := 0;
+BEGIN
+
+ IF NOT (foo_start, foo_end) OVERLAPS (bar_start, bar_end) THEN
+ RETURN 0;
+ END IF;
+
+ IF foo_start > bar_start THEN
+ day_start := bar_start;
+ ELSE
+ day_start := foo_start;
+ END IF;
+
+ IF foo_end > bar_start THEN
+ day_end := foo_end;
+ ELSE
+ day_end := bar_end;
+ END IF;
+
+ WHILE day_start <= day_end LOOP
+ IF day_start >= foo_start AND day_start <= foo_end AND day_start >= bar_start AND day_end <= bar_end THEN
+ day_count := day_count + 1 ;
+ END IF;
+ day_start := day_start + 1;
+ END LOOP;
+
+ RETURN day_count;
+
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-187/luca-ferrari/postgresql/ch-2.plperl b/challenge-187/luca-ferrari/postgresql/ch-2.plperl
new file mode 100644
index 0000000000..cb642d4673
--- /dev/null
+++ b/challenge-187/luca-ferrari/postgresql/ch-2.plperl
@@ -0,0 +1,51 @@
+-- Perl Weekly Challenge 187
+-- Task 2
+
+CREATE SCHEMA IF NOT EXISTS pwc187;
+
+CREATE OR REPLACE FUNCTION
+pwc187.task2_plperl()
+RETURNS VOID
+AS $CODE$
+$CODE$
+LANGUAGE plperl;
+-- Perl Weekly Challenge 187
+-- Task 2
+
+CREATE SCHEMA IF NOT EXISTS pwc187;
+
+CREATE OR REPLACE FUNCTION
+pwc187.task2_plperl( int[] )
+RETURNS int[]
+AS $CODE$
+my ( @n ) = $_[0]->@*;
+my @triplets;
+my $max = 0;
+
+
+for my $index_a ( 0 .. $#n ) {
+ my $a = $n[ $index_a ];
+
+ for my $index_b ( ( $index_a + 1 ) .. $#n ) {
+ my $b = $n[ $index_b ];
+
+ for my $index_c ( ( $index_b + 1 ) .. $#n ) {
+ my $c = $n[ $index_c ];
+
+ if ( $max <= $a + $b + $c
+ && ( $a + $b ) > $c
+ && ( $b + $c ) > $a
+ && ( $a + $c ) > $b ) {
+
+ @triplets = ( $a, $b, $c );
+ $max = $a + $b + $c;
+ }
+ }
+ }
+}
+
+
+return [@triplets];
+
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-187/luca-ferrari/postgresql/ch-2.sql b/challenge-187/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..f9659e9ac2
--- /dev/null
+++ b/challenge-187/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,15 @@
+-- Perl Weekly Challenge 187
+-- Task 2
+
+
+CREATE SCHEMA IF NOT EXISTS pwc187;
+
+CREATE OR REPLACE FUNCTION
+pwc187.task2_plpgsql( a int[] )
+RETURNS int[]
+AS $CODE$
+BEGIN
+ RETURN pwc187.task2_plperl( a );
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-187/luca-ferrari/raku/ch-1.p6 b/challenge-187/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..792c650429
--- /dev/null
+++ b/challenge-187/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,30 @@
+#!raku
+
+# Perl Weekly Challenge 187
+
+sub MAIN( Str $start-foo, Str $end-foo, Str $start-bar, Str $end-bar ) {
+
+ $start-foo ~~ / ^ ( \d ** 2 ) '-' ( \d ** 2 ) $ /;
+ my $foo-days-start = DateTime.new: year => 2022, day => $/[0], month => $/[1];
+ $end-foo ~~ / ^ (\d ** 2) '-' (\d **2) $ /;
+ my $foo-days-end = DateTime.new: year => 2022, day => $/[0], month => $/[1];
+
+ $start-bar ~~ / ^ ( \d ** 2 ) '-' ( \d ** 2 ) $ /;
+ my $bar-days-start = DateTime.new: year => 2022, day => $/[0], month => $/[1];
+ $end-bar ~~ / ^ ( \d ** 2 ) '-' ( \d ** 2 ) $ /;
+ my $bar-days-end = DateTime.new: year => 2022, day => $/[0], month => $/[1];
+
+ my $days = 0;
+ my $start = $foo-days-start >= $bar-days-start ?? $bar-days-start !! $foo-days-start;
+ my $end = $foo-days-end >= $bar-days-end ?? $foo-days-end !! $bar-days-end;
+
+ while ( $start < $end ) {
+ $days++ if ( $start >= $foo-days-start
+ && $start <= $foo-days-end
+ && $start >= $bar-days-start
+ && $start <= $bar-days-end );
+ $start .= later( days => 1 );
+ }
+
+ say $days;
+}
diff --git a/challenge-187/luca-ferrari/raku/ch-2.p6 b/challenge-187/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..9253317f2d
--- /dev/null
+++ b/challenge-187/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,30 @@
+#!raku
+
+# Perl Weekly Challenge 187
+
+sub MAIN( *@n where { @n.grep( * ~~ Int ).elems == @n.elems } ) {
+
+ my $max = 0;
+ my @triplet;
+ for 0 ..^ @n.elems -> $index-a {
+ my $a = @n[ $index-a ].Int;
+
+ for $index-a ^..^ @n.elems -> $index-b {
+ my $b = @n[ $index-b ].Int;
+
+ for $index-b ^..^ @n.elems -> $index-c {
+ my $c = @n[ $index-c ].Int;
+
+ if ( $max <= $a + $b + $c
+ && ( $a + $b ) > $c
+ && ( $b + $c ) > $a
+ && ( $a + $c ) > $b ) {
+ @triplet = $a, $b, $c;
+ $max = $a + $b + $c;
+ }
+ }
+ }
+ }
+
+ @triplet.sort.say if ( @triplet );
+}