diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-28 11:08:00 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-28 11:08:00 +0000 |
| commit | d13e14c8f86e076f4c55b09a954ccb9d3ac83304 (patch) | |
| tree | f1f1d8a486ccd91c03703fabb7d3c60f7c6841d4 | |
| parent | 67b7d597ec7d3f055aa994a9b21f25bce5254c5a (diff) | |
| parent | d75e53fddfcada34523e16efded96b7e5b895528 (diff) | |
| download | perlweeklychallenge-club-d13e14c8f86e076f4c55b09a954ccb9d3ac83304.tar.gz perlweeklychallenge-club-d13e14c8f86e076f4c55b09a954ccb9d3ac83304.tar.bz2 perlweeklychallenge-club-d13e14c8f86e076f4c55b09a954ccb9d3ac83304.zip | |
Merge pull request #9147 from fluca1978/PWC245
PWC 245
| -rw-r--r-- | challenge-245/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-7.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-8.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/blog-9.txt | 1 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/postgresql/ch-1.plperl | 30 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/postgresql/ch-1.sql | 25 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/postgresql/ch-2.plperl | 31 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/postgresql/ch-2.sql | 16 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/python/ch-1.py | 31 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/python/ch-2.py | 36 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/raku/ch-1.p6 | 22 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/raku/ch-1b.p6 | 16 | ||||
| -rw-r--r-- | challenge-245/luca-ferrari/raku/ch-2.p6 | 21 |
18 files changed, 237 insertions, 0 deletions
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/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 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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +-- + +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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +-- + +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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +-- + +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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +-- + +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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +# + +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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +# + +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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +# + +# $ 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-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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +# + +# $ 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; +} 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 <https://perlweeklychallenge.org/blog/perl-weekly-challenge-245/> +# + +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; +} |
