diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2024-01-08 10:17:22 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2024-01-08 11:12:16 +0100 |
| commit | 080fde56fb9825f48e63c97840234fbde25fedaa (patch) | |
| tree | d8ca72df8e07e7645657406b7638c113f3abc008 | |
| parent | 9a485c9bac8e3887b165d67c9aa81d71cdd42f01 (diff) | |
| download | perlweeklychallenge-club-080fde56fb9825f48e63c97840234fbde25fedaa.tar.gz perlweeklychallenge-club-080fde56fb9825f48e63c97840234fbde25fedaa.tar.bz2 perlweeklychallenge-club-080fde56fb9825f48e63c97840234fbde25fedaa.zip | |
PWC 251
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
| -rw-r--r-- | challenge-251/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/blog-7.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/blog-8.txt | 1 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/postgresql/ch-1.plperl | 23 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/postgresql/ch-1.sql | 29 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/postgresql/ch-2.plperl | 63 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/postgresql/ch-2.sql | 16 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/python/ch-2.py | 59 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/raku/ch-1.p6 | 17 | ||||
| -rw-r--r-- | challenge-251/luca-ferrari/raku/ch-2.p6 | 37 |
16 files changed, 277 insertions, 0 deletions
diff --git a/challenge-251/luca-ferrari/blog-1.txt b/challenge-251/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..ef5631ba42 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1 diff --git a/challenge-251/luca-ferrari/blog-2.txt b/challenge-251/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..40f36701a6 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2 diff --git a/challenge-251/luca-ferrari/blog-3.txt b/challenge-251/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..d8cf826c69 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1plperl diff --git a/challenge-251/luca-ferrari/blog-4.txt b/challenge-251/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..8bc8154b8b --- /dev/null +++ b/challenge-251/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2plperl diff --git a/challenge-251/luca-ferrari/blog-5.txt b/challenge-251/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..e8326a85ec --- /dev/null +++ b/challenge-251/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1plpgsql diff --git a/challenge-251/luca-ferrari/blog-6.txt b/challenge-251/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..57b87c236c --- /dev/null +++ b/challenge-251/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2plpgsql diff --git a/challenge-251/luca-ferrari/blog-7.txt b/challenge-251/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..9437b6564e --- /dev/null +++ b/challenge-251/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task1python diff --git a/challenge-251/luca-ferrari/blog-8.txt b/challenge-251/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..f653f319b0 --- /dev/null +++ b/challenge-251/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/08/PerlWeeklyChallenge251.html#task2python diff --git a/challenge-251/luca-ferrari/postgresql/ch-1.plperl b/challenge-251/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..7152e60c8a --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,23 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +CREATE OR REPLACE FUNCTION +pwc251.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $nums ) = @_; + my $sum = 0; + + for ( 0 .. $nums->@* - 2 ) { + next if $_ % 2 != 0; + $sum += $nums->@[ $_ ] . $nums->@[ $_ + 1 ]; + } + + return $sum; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-251/luca-ferrari/postgresql/ch-1.sql b/challenge-251/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..83c215c5f6 --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 1 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +CREATE OR REPLACE FUNCTION +pwc251.task1_plpgsql( nums int[] ) +RETURNS int +AS $CODE$ +DECLARE + s text; + v int := 0; +BEGIN + FOR i IN 1 .. array_length( nums, 1 ) - 1 LOOP + IF i % 2 = 0 THEN + CONTINUE; + END IF; + + v := v + ( nums[ i ]::text || nums[ i + 1 ]::text )::int; + END LOOP; + + RETURN v; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-251/luca-ferrari/postgresql/ch-2.plperl b/challenge-251/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..32d43ba6ef --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,63 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +/** + * example + testdb=> select pwc251.task2_plperl( array[ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17]] ); + task2_plperl +-------------- + 15 +(1 row) +*/ +CREATE OR REPLACE FUNCTION +pwc251.task2_plperl( int[][] ) +RETURNS int +AS $CODE$ + my ( $matrix ) = @_; + + my $max_col = undef; + + # search the max column value + my @max_col_indexes; + for my $col ( 0 .. $matrix->@[ 0 ]->@* - 1 ) { + $max_col_index[ $col ] = undef; + + for my $row ( 0 .. $matrix->@* - 1 ) { + $max_col_index[ $col ] = $matrix->@[ $row ]->[ $col ] if ( ! $max_col_index[ $col ] || $max_col_index[ $col ] < $matrix->@[ $row ]->[ $col ] ); + } + } + for my $row ( 0 .. $matrix->@* - 1 ) { + for my $col ( 0 .. $matrix->@[ $row ]->@* - 1 ) { + $max_col = $matrix->@[ $row ]->[ $col ] if ( ! $max_col || $max_col < $matrix->@[ $row ]->[ $col ] ); + } + } + + for my $row ( 0 .. $matrix->@* - 1 ) { + my $current_min = undef; + my $current_min_index = 0; + + for my $col ( 0 .. $matrix->@[ $row ]->@* - 1 ) { + if ( ! $current_min || $matrix->@[ $row ]->[ $col ] < $current_min ) { + $current_min = $matrix->@[ $row ]->[ $col ]; + $current_min_index = $col; + } + } + + if ( $current_min == $max_col_index[ $current_min_index ] ) { + return $current_min; + } + + + } + + return -1; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-251/luca-ferrari/postgresql/ch-2.sql b/challenge-251/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..fcdf6a6f9a --- /dev/null +++ b/challenge-251/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 251 +-- Task 2 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc251; + +CREATE OR REPLACE FUNCTION +pwc251.task2_plpgsql( matrix int[][] ) +RETURNS int +AS $CODE$ + SELECT pwc251.task2_plperl( matrix ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-251/luca-ferrari/python/ch-1.py b/challenge-251/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..e72f979066 --- /dev/null +++ b/challenge-251/luca-ferrari/python/ch-1.py @@ -0,0 +1,25 @@ +#!python + +# +# Perl Weekly Challenge 251 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +# + +import sys + +# task implementation +def main( argv ): + sum = 0 + for i in range( 0, len( argv ) - 1 ): + if i % 2 != 0: + continue + sum += int( argv[ i ] + argv[ i + 1 ] ) + + return sum + + +# invoke the main without the command itself +if __name__ == '__main__': + print( main( sys.argv[ 1: ] ) ) diff --git a/challenge-251/luca-ferrari/python/ch-2.py b/challenge-251/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..a06c706b28 --- /dev/null +++ b/challenge-251/luca-ferrari/python/ch-2.py @@ -0,0 +1,59 @@ +#!python + +# +# Perl Weekly Challenge 251 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +# + +import sys + +# task implementation +def main( argv ): + matrix = [] + row = 0 + matrix.append( [] ) + + for x in argv[ 0 ]: + if x == ' ': + continue + + # change row + if x == '|': + row += 1 + matrix.append( [] ) + continue + + matrix[ row ].append( int( x ) ) + + # make indexes of max columns + max_cols = [] + for col in range( 0, len( matrix ) ): + current_max = None + for row in range( 0, len( matrix[ 0 ] ) ): + if not current_max or current_max < matrix[ row ][ col ] : + current_max = matrix[ row ][ col ] + + max_cols.append( current_max ) + + for row in range( 0, len( matrix ) ): + current_min = None + current_min_col = None + + for col in range( 0, len( matrix[ row ] ) ): + if not current_min or current_min > matrix[ row ][ col ]: + current_min = matrix[ row ][ col ] + current_min_col = col + + if current_min == max_cols[ current_min_col ]: + return current_min + + return -1 + + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( main( sys.argv[ 1: ] ) ) diff --git a/challenge-251/luca-ferrari/raku/ch-1.p6 b/challenge-251/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..c0ba780273 --- /dev/null +++ b/challenge-251/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 251 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems and @nums.elems %% 2 } ) { + my $sum = 0; + for @nums -> $l, $r { + $sum += $l ~ $r; + } + + $sum.say; +} diff --git a/challenge-251/luca-ferrari/raku/ch-2.p6 b/challenge-251/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..cbfdd4baab --- /dev/null +++ b/challenge-251/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,37 @@ +#!raku + +# +# Perl Weekly Challenge 251 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/> +# + +sub MAIN() { + + my $matrix = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]; + + my @max-col; + for 0 ..^ $matrix[ 0 ].elems -> $col { + my @current-col.push: $matrix[ $_ ][ $col ] for 0 ..^ $matrix.elems; + @max-col[ $col ] = @current-col.max; + } + + for 0 ..^ $matrix.elems -> $row { + my $min = Nil; + my $min-col = Nil; + + for 0 ..^ $matrix[ $row ].elems -> $col { + if ( ! $min || $matrix[ $row ][ $col ] < $min ) { + $min-col = $col; + $min = $matrix[ $row ][ $col ]; + } + } + + $min.say and exit if ( @max-col[ $min-col ] == $min ); + } + + '-1'.say; +} |
