From c059b0cd682251245b1884778f6dbd791225a8ed Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Nov 2023 11:33:23 +0100 Subject: PWC 245 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 --- challenge-245/luca-ferrari/blog-1.txt | 1 + challenge-245/luca-ferrari/blog-2.txt | 1 + challenge-245/luca-ferrari/blog-3.txt | 1 + challenge-245/luca-ferrari/blog-4.txt | 1 + challenge-245/luca-ferrari/blog-5.txt | 1 + challenge-245/luca-ferrari/blog-6.txt | 1 + challenge-245/luca-ferrari/blog-7.txt | 1 + challenge-245/luca-ferrari/blog-8.txt | 1 + challenge-245/luca-ferrari/postgresql/ch-1.plperl | 30 +++++++++++++++++++ challenge-245/luca-ferrari/postgresql/ch-1.sql | 25 ++++++++++++++++ challenge-245/luca-ferrari/postgresql/ch-2.plperl | 31 +++++++++++++++++++ challenge-245/luca-ferrari/postgresql/ch-2.sql | 16 ++++++++++ challenge-245/luca-ferrari/python/ch-1.py | 31 +++++++++++++++++++ challenge-245/luca-ferrari/python/ch-2.py | 36 +++++++++++++++++++++++ challenge-245/luca-ferrari/raku/ch-1.p6 | 22 ++++++++++++++ challenge-245/luca-ferrari/raku/ch-2.p6 | 21 +++++++++++++ 16 files changed, 220 insertions(+) create mode 100644 challenge-245/luca-ferrari/blog-1.txt create mode 100644 challenge-245/luca-ferrari/blog-2.txt create mode 100644 challenge-245/luca-ferrari/blog-3.txt create mode 100644 challenge-245/luca-ferrari/blog-4.txt create mode 100644 challenge-245/luca-ferrari/blog-5.txt create mode 100644 challenge-245/luca-ferrari/blog-6.txt create mode 100644 challenge-245/luca-ferrari/blog-7.txt create mode 100644 challenge-245/luca-ferrari/blog-8.txt create mode 100644 challenge-245/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-245/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-245/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-245/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-245/luca-ferrari/python/ch-1.py create mode 100644 challenge-245/luca-ferrari/python/ch-2.py create mode 100644 challenge-245/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-245/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-245/luca-ferrari/blog-1.txt b/challenge-245/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..2a976c1f21 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1 diff --git a/challenge-245/luca-ferrari/blog-2.txt b/challenge-245/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..34de71d420 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2 diff --git a/challenge-245/luca-ferrari/blog-3.txt b/challenge-245/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..5807d09548 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1plperl diff --git a/challenge-245/luca-ferrari/blog-4.txt b/challenge-245/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..835c77240b --- /dev/null +++ b/challenge-245/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2plperl diff --git a/challenge-245/luca-ferrari/blog-5.txt b/challenge-245/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..7321c71d7b --- /dev/null +++ b/challenge-245/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1plpgsql diff --git a/challenge-245/luca-ferrari/blog-6.txt b/challenge-245/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..f9276df669 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2plpgsql diff --git a/challenge-245/luca-ferrari/blog-7.txt b/challenge-245/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..486ba8e95a --- /dev/null +++ b/challenge-245/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1python diff --git a/challenge-245/luca-ferrari/blog-8.txt b/challenge-245/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..eb15ccc483 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2python diff --git a/challenge-245/luca-ferrari/postgresql/ch-1.plperl b/challenge-245/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..8742a00d10 --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task1_plperl( text[], int[] ) +RETURNS SETOF text +AS $CODE$ + my ( $langs, $popularity ) = @_; + + die "Not same length arrays!" if ( $langs->@* != $popularity->@* ); + + my $sorting = {}; + + for my $index ( 0 .. $popularity->@* - 1 ) { + $sorting->{ $popularity->[ $index ] } = $langs->[ $index ]; + } + + for ( sort { $b <=> $a } $popularity->@* ) { + return_next( $sorting->{ $_ } ); + } + + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-245/luca-ferrari/postgresql/ch-1.sql b/challenge-245/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..236af438b8 --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,25 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task1_plpgsql( langs text[], popularity int[] ) +RETURNS SETOF text +AS $CODE$ + + WITH sorting AS ( + SELECT l + FROM unnest( langs ) l + , unnest( popularity ) p + ORDER BY p DESC + ) + SELECT distinct( l ) + FROM sorting; + +$CODE$ +LANGUAGE sql; diff --git a/challenge-245/luca-ferrari/postgresql/ch-2.plperl b/challenge-245/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..f7ac4c602f --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,31 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + use Algorithm::Combinatorics qw/ permutations /; + my ( $digits ) = @_; + + die "Digits must be between 0 and 9" if ( grep( { $_ > 9 || $_ < 0 } $digits->@* ) ); + + my $result = -1; + for my $k ( 0 .. $digits->@* ) { + my $permutations = permutations( \ $digits->@*, $k ); + while ( my $iter = $permutations->next ) { + my $value = join('', $iter->@* ); + next if ( $value % 3 != 0 ); + $result = $value if ( $value > $result ); + } + } + + return $result; + +$CODE$ +LANGUAGE plperlu; diff --git a/challenge-245/luca-ferrari/postgresql/ch-2.sql b/challenge-245/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..9f3e8da91f --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task2_plpgsql( digits int[] ) +RETURNS int +AS $CODE$ + SELECT pwc245.task2_plperl( digits ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-245/luca-ferrari/python/ch-1.py b/challenge-245/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..4e63b95892 --- /dev/null +++ b/challenge-245/luca-ferrari/python/ch-1.py @@ -0,0 +1,31 @@ +#!python + +# +# Perl Weekly Challenge 245 +# Task 1 +# +# See +# + +import sys + +# task implementation +def main( argv ): + langs = argv[ 0 ].split( "|" ) + popularity = list( map( int, argv[1].split( "|" ) ) ) + + sorting = {} + for i in range( 0, len( popularity ) ): + sorting[ langs[ i ] ] = popularity[ i ] + + + for v in sorted( sorting.items(), key=lambda x: x[1], reverse=True ): + print( v[0] ) + + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-245/luca-ferrari/python/ch-2.py b/challenge-245/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..a6a7d4ecde --- /dev/null +++ b/challenge-245/luca-ferrari/python/ch-2.py @@ -0,0 +1,36 @@ +#!python + +# +# Perl Weekly Challenge 245 +# Task 2 +# +# See +# + +import sys +from itertools import permutations + +# task implementation +def main( argv ): + digits = list( map( int, argv ) ) + + result = -1 + + for p in permutations( digits ): + value = int( "".join( map( str, p ) ) ) + + if value % 3 != 0: + continue + + if value > result: + result = value + + print( result ) + + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-245/luca-ferrari/raku/ch-1.p6 b/challenge-245/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..dd55dadcf5 --- /dev/null +++ b/challenge-245/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!raku + +# +# Perl Weekly Challenge 245 +# Task 1 +# +# See +# + +# $ raku raku/ch-1.p6 --langs="c" --langs="java" --langs="python" --popularity=2 --popularity=3 --popularity=5 + +sub MAIN( :@langs where { @langs.elems == @langs.grep( * ~~ Str ).elems } + , :@popularity where { @popularity.elems == @langs.elems == @popularity.grep( * ~~ Int ).elems } ) { + + my %sorted; + %sorted{ @popularity[ $_ ] } = @langs[ $_ ] for 0 ..^ @langs.elems; + + my @output; + @output.push: %sorted{ $_ } for %sorted.keys.sort( { $^b <=> $^a } ); + @output.join( ', ' ).say; + +} diff --git a/challenge-245/luca-ferrari/raku/ch-2.p6 b/challenge-245/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..aad5089c8e --- /dev/null +++ b/challenge-245/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,21 @@ +#!raku + +# +# Perl Weekly Challenge 245 +# Task 2 +# +# See +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems + && @nums.grep( 0 <= * <= 9 ).elems } ) { + + my $largest = -1; + for @nums.permutations { + my $value = $_.join.Int; + next if $value !%% 3; + $largest = $value if ( $value > $largest ); + } + + $largest.say; +} -- cgit From 604e7c781e9220952021980cb1379242d5873b62 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Nov 2023 15:36:11 +0100 Subject: Another implementation in Raku --- challenge-245/luca-ferrari/raku/ch-1b.p6 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 challenge-245/luca-ferrari/raku/ch-1b.p6 diff --git a/challenge-245/luca-ferrari/raku/ch-1b.p6 b/challenge-245/luca-ferrari/raku/ch-1b.p6 new file mode 100644 index 0000000000..f1fb96d322 --- /dev/null +++ b/challenge-245/luca-ferrari/raku/ch-1b.p6 @@ -0,0 +1,16 @@ +#!raku + +# +# Perl Weekly Challenge 245 +# Task 1 +# +# See +# + +# $ raku raku/ch-1.p6 --langs="c" --langs="java" --langs="python" --popularity=2 --popularity=3 --popularity=5 + +sub MAIN( :@langs where { @langs.elems == @langs.grep( * ~~ Str ).elems } + , :@popularity where { @popularity.elems == @langs.elems == @popularity.grep( * ~~ Int ).elems } ) { + + ( @langs [Z] @popularity ).sort( { $^b[1] <=> $^a[1] } ).map( *[ 0 ] ).join( ',' ).say; +} -- cgit From d75e53fddfcada34523e16efded96b7e5b895528 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Nov 2023 15:40:16 +0100 Subject: Another approach blog reference --- challenge-245/luca-ferrari/blog-9.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-245/luca-ferrari/blog-9.txt diff --git a/challenge-245/luca-ferrari/blog-9.txt b/challenge-245/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..91d5c777fa --- /dev/null +++ b/challenge-245/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1b -- cgit