From 978a6e83d295db79dfbbbb410bfb7024ee97c962 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:01:21 +0100 Subject: Task 1 done --- challenge-206/luca-ferrari/raku/ch-1.p6 | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-206/luca-ferrari/raku/ch-1.p6 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 +# + +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 ); +} -- cgit From 1a508068a3beeb171d974d7d68c9f86e5a91ec81 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:09:00 +0100 Subject: Task 2 done --- challenge-206/luca-ferrari/raku/ch-2.p6 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-206/luca-ferrari/raku/ch-2.p6 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 +# + +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; +} -- cgit From 1219992c1e63e6edea84b3b0c90cbad646545fb3 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:27:01 +0100 Subject: Task 1 plperl done --- challenge-206/luca-ferrari/postgresql/ch-1.plperl | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-1.plperl 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 +-- + +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; -- cgit From 0023994b2906e66bf0f170c0fb5b492bb9eea3fb Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:32:27 +0100 Subject: Task 2 plperl done --- challenge-206/luca-ferrari/postgresql/ch-2.plperl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-2.plperl 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 +-- + +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; -- cgit From f9d896bb38edec71ac1e2663636c140bb3f4abf0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:44:37 +0100 Subject: Task 1 plpgsql done --- challenge-206/luca-ferrari/postgresql/ch-1.sql | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-1.sql 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..3165bc3a97 --- /dev/null +++ b/challenge-206/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 206 +-- Task 1 +-- +-- See +-- + +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$ --foo +LANGUAGE plpgsql; -- cgit From d7bc63303af1388af838f77db741dede81f499ed Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:49:51 +0100 Subject: Task 2 plpgsql done --- challenge-206/luca-ferrari/postgresql/ch-2.sql | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-2.sql 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..3add7366d5 --- /dev/null +++ b/challenge-206/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 206 +-- Task 1 +-- +-- See +-- + +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; -- cgit From b0c85458d55af55be49e2f5a3632c730aad8e7cd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 28 Feb 2023 10:48:21 +0100 Subject: Blog references --- challenge-206/luca-ferrari/blog-1.txt | 1 + challenge-206/luca-ferrari/blog-2.txt | 1 + challenge-206/luca-ferrari/blog-3.txt | 1 + challenge-206/luca-ferrari/blog-4.txt | 1 + challenge-206/luca-ferrari/blog-5.txt | 1 + challenge-206/luca-ferrari/blog-6.txt | 1 + challenge-206/luca-ferrari/postgresql/ch-1.sql | 2 +- challenge-206/luca-ferrari/postgresql/ch-2.sql | 4 ++-- 8 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 challenge-206/luca-ferrari/blog-1.txt create mode 100644 challenge-206/luca-ferrari/blog-2.txt create mode 100644 challenge-206/luca-ferrari/blog-3.txt create mode 100644 challenge-206/luca-ferrari/blog-4.txt create mode 100644 challenge-206/luca-ferrari/blog-5.txt create mode 100644 challenge-206/luca-ferrari/blog-6.txt 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.sql b/challenge-206/luca-ferrari/postgresql/ch-1.sql index 3165bc3a97..3f3e36f47b 100644 --- a/challenge-206/luca-ferrari/postgresql/ch-1.sql +++ b/challenge-206/luca-ferrari/postgresql/ch-1.sql @@ -33,5 +33,5 @@ BEGIN RETURN m; END -$CODE$ --foo +$CODE$ LANGUAGE plpgsql; diff --git a/challenge-206/luca-ferrari/postgresql/ch-2.sql b/challenge-206/luca-ferrari/postgresql/ch-2.sql index 3add7366d5..0c73ec4c72 100644 --- a/challenge-206/luca-ferrari/postgresql/ch-2.sql +++ b/challenge-206/luca-ferrari/postgresql/ch-2.sql @@ -15,8 +15,8 @@ DECLARE res int; BEGIN WITH data AS ( - SELECT v, row_number() OVER ( ORDER BY v ) r - FROM unnest( l ) v + SELECT v, row_number() OVER ( ORDER BY v ) r + FROM unnest( l ) v ) SELECT sum( v ) INTO res -- cgit