aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-04 11:40:57 +0000
committerGitHub <noreply@github.com>2023-03-04 11:40:57 +0000
commit320fe6658f101a0e2b650a5c00b4fe0cc2eeb75c (patch)
treee0436259f40ec214b478228b45dd34f4a6d1b15a
parent7b8c8b2bdf655dfaf9a1f4a851c47f2ed5344922 (diff)
parentb0c85458d55af55be49e2f5a3632c730aad8e7cd (diff)
downloadperlweeklychallenge-club-320fe6658f101a0e2b650a5c00b4fe0cc2eeb75c.tar.gz
perlweeklychallenge-club-320fe6658f101a0e2b650a5c00b4fe0cc2eeb75c.tar.bz2
perlweeklychallenge-club-320fe6658f101a0e2b650a5c00b4fe0cc2eeb75c.zip
Merge pull request #7649 from fluca1978/PWC206
Pwc206
-rw-r--r--challenge-206/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-206/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-206/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-206/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-206/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-206/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-206/luca-ferrari/postgresql/ch-1.plperl52
-rw-r--r--challenge-206/luca-ferrari/postgresql/ch-1.sql37
-rw-r--r--challenge-206/luca-ferrari/postgresql/ch-2.plperl23
-rw-r--r--challenge-206/luca-ferrari/postgresql/ch-2.sql30
-rw-r--r--challenge-206/luca-ferrari/raku/ch-1.p638
-rw-r--r--challenge-206/luca-ferrari/raku/ch-2.p620
12 files changed, 206 insertions, 0 deletions
diff --git a/challenge-206/luca-ferrari/blog-1.txt b/challenge-206/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..b4e5f2d251
--- /dev/null
+++ b/challenge-206/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task1
diff --git a/challenge-206/luca-ferrari/blog-2.txt b/challenge-206/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..bbeb3b8149
--- /dev/null
+++ b/challenge-206/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task2
diff --git a/challenge-206/luca-ferrari/blog-3.txt b/challenge-206/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..ba8342161d
--- /dev/null
+++ b/challenge-206/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task1plperl
diff --git a/challenge-206/luca-ferrari/blog-4.txt b/challenge-206/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..072ce91cbe
--- /dev/null
+++ b/challenge-206/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task2plperl
diff --git a/challenge-206/luca-ferrari/blog-5.txt b/challenge-206/luca-ferrari/blog-5.txt
new file mode 100644
index 0000000000..d444dccdd3
--- /dev/null
+++ b/challenge-206/luca-ferrari/blog-5.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task1plpgsql
diff --git a/challenge-206/luca-ferrari/blog-6.txt b/challenge-206/luca-ferrari/blog-6.txt
new file mode 100644
index 0000000000..ffb4ce2f96
--- /dev/null
+++ b/challenge-206/luca-ferrari/blog-6.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task2plpgsql
diff --git a/challenge-206/luca-ferrari/postgresql/ch-1.plperl b/challenge-206/luca-ferrari/postgresql/ch-1.plperl
new file mode 100644
index 0000000000..aa69f27629
--- /dev/null
+++ b/challenge-206/luca-ferrari/postgresql/ch-1.plperl
@@ -0,0 +1,52 @@
+--
+-- Perl Weekly Challenge 206
+-- Task 1
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-206/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc206;
+
+CREATE OR REPLACE FUNCTION
+pwc206.task1_plperl( text[] )
+RETURNS int
+AS $CODE$
+ my ( $times ) = @_;
+ my $computations = {};
+ my $min;
+
+ my $diff = sub {
+ my ( $start, $end ) = @_;
+ $start =~ /^(\d{2}):(\d{2})$/;
+ my ( $start_hours, $start_mins ) = ( $1, $2 );
+
+ $end =~ /^(\d{2}):(\d{2})$/;
+ my ( $end_hours, $end_mins ) = ( $1, $2 );
+
+ if ( $start_hours == 0) {
+ $start_hours = 23;
+ $start_mins += 60;
+ }
+
+ if ( $end_hours == 0) {
+ $end_hours = 23;
+ $end_mins += 60;
+ }
+
+ return abs( $end_hours - $start_hours ) * 60 + abs( $end_mins - $start_mins ) % 60;
+ };
+
+
+ for my $begin ( sort $times->@* ) {
+ for my $end ( sort $times->@* ) {
+ next if ( $begin eq $end );
+
+ my $difference = $diff->( $end, $begin );
+ $computations->{ $difference } = [ $begin, $end ];
+ $min = $difference if ( ! $min || $difference < $min );
+ }
+ }
+
+ elog(INFO, "Min is $min minutes from " . join( ',', $computations->{ $min }->@* ) );
+ return $min;
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-206/luca-ferrari/postgresql/ch-1.sql b/challenge-206/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..3f3e36f47b
--- /dev/null
+++ b/challenge-206/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,37 @@
+--
+-- Perl Weekly Challenge 206
+-- Task 1
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-206/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc206;
+
+CREATE OR REPLACE FUNCTION
+pwc206.task1_plpgsql( t text[] )
+RETURNS time
+AS $CODE$
+
+DECLARE
+ m time;
+ t1 text;
+ t2 text;
+BEGIN
+
+ FOREACH t1 IN ARRAY t LOOP
+ FOREACH t2 IN ARRAY t LOOP
+ IF t1 = t2 THEN
+ CONTINUE;
+ END IF;
+
+ IF m IS NULL OR ( t2::time - t1::time ) < m THEN
+ m := ( t2::time - t1::time );
+ END IF;
+
+ END LOOP;
+ END LOOP;
+
+ RETURN m;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-206/luca-ferrari/postgresql/ch-2.plperl b/challenge-206/luca-ferrari/postgresql/ch-2.plperl
new file mode 100644
index 0000000000..d9cbbe220c
--- /dev/null
+++ b/challenge-206/luca-ferrari/postgresql/ch-2.plperl
@@ -0,0 +1,23 @@
+--
+-- Perl Weekly Challenge 206
+-- Task 2
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-206/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc206;
+
+CREATE OR REPLACE FUNCTION
+pwc206.task2_plperl( int[] )
+RETURNS int
+AS $CODE$
+ my ( $list ) = sort $_[ 0 ];
+ my $sum = 0;
+
+ while ( $list->@* ) {
+ $sum += shift $list->@*;
+ shift $list->@*;
+ }
+
+ return $sum;
+$CODE$
+LANGUAGE plperl;
diff --git a/challenge-206/luca-ferrari/postgresql/ch-2.sql b/challenge-206/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..0c73ec4c72
--- /dev/null
+++ b/challenge-206/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,30 @@
+--
+-- Perl Weekly Challenge 206
+-- Task 1
+--
+-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-206/>
+--
+
+CREATE SCHEMA IF NOT EXISTS pwc206;
+
+CREATE OR REPLACE FUNCTION
+pwc206.task2_plpgsql( l int[] )
+RETURNS int
+AS $CODE$
+DECLARE
+ res int;
+BEGIN
+ WITH data AS (
+ SELECT v, row_number() OVER ( ORDER BY v ) r
+ FROM unnest( l ) v
+ )
+ SELECT sum( v )
+ INTO res
+ FROM data
+ WHERE r % 2 <> 0
+ ;
+
+ RETURN res;
+END
+$CODE$
+LANGUAGE plpgsql;
diff --git a/challenge-206/luca-ferrari/raku/ch-1.p6 b/challenge-206/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..e9e31dddec
--- /dev/null
+++ b/challenge-206/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,38 @@
+#!raku
+
+#
+# Perl Weekly Challenge 206
+# Task 1
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-206/>
+#
+
+sub diff ( $start, $end ) {
+ my ( $start-hours, $start-mins ) = $start.chomp.split( ':' );
+ my ( $end-hours, $end-mins ) = $end.chomp.split( ':' );
+
+ if ( $start-hours == 0 ) {
+ $start-hours = 23;
+ $start-mins += 60;
+ }
+
+ if ( $end-hours == 0 ) {
+ $end-hours = 23;
+ $end-mins += 60;
+ }
+
+ my $diff-hours = abs( $end-hours - $start-hours );
+ my $diff-mins = abs( $end-mins - $start-mins ) % 60;
+
+ return $diff-hours * 60 + $diff-mins;
+
+}
+
+sub MAIN( :$verbose = True, *@times where { @times.grep( * ~~ / ^ \d ** 2 ':' \d ** 2 $ / ).elems == @times.elems } ) {
+
+ my %diffs;
+ %diffs{ diff( $_[ 1 ], $_[ 0 ] ) } = [ $_[0], $_[1] ] for @times.sort.combinations( 2 );
+
+ %diffs.keys.map( *.Int ).min.say;
+ %diffs{ %diffs.keys.map( *.Int ).min }.join( ' - ' ).say if ( $verbose );
+}
diff --git a/challenge-206/luca-ferrari/raku/ch-2.p6 b/challenge-206/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..2c4376e5b1
--- /dev/null
+++ b/challenge-206/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,20 @@
+#!raku
+
+#
+# Perl Weekly Challenge 206
+# Task 2
+#
+# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-206/>
+#
+
+sub MAIN( *@list where { @list.elems %% 2 && @list.grep( * ~~ Int ).elems == @list.elems } ) {
+ my @sums;
+
+ for @list.permutations {
+ for $_.rotor( 2 ) -> $a, $b {
+ @sums.push: sum( min( $a ) + min( $b ) );
+ }
+ }
+
+ @sums.min.say;
+}