diff options
| -rw-r--r-- | challenge-213/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/postgresql/ch-1.plperl | 19 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/postgresql/ch-1.sql | 30 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/postgresql/ch-2.plperl | 59 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/postgresql/ch-2.sql | 64 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/raku/ch-1.p6 | 12 | ||||
| -rw-r--r-- | challenge-213/luca-ferrari/raku/ch-2.p6 | 61 |
12 files changed, 251 insertions, 0 deletions
diff --git a/challenge-213/luca-ferrari/blog-1.txt b/challenge-213/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..ed3b199837 --- /dev/null +++ b/challenge-213/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/17/PerlWeeklyChallenge213.html#task1 diff --git a/challenge-213/luca-ferrari/blog-2.txt b/challenge-213/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..b59a37dca6 --- /dev/null +++ b/challenge-213/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/17/PerlWeeklyChallenge213.html#task2 diff --git a/challenge-213/luca-ferrari/blog-3.txt b/challenge-213/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..56ee76c923 --- /dev/null +++ b/challenge-213/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/17/PerlWeeklyChallenge213.html#task1plperl diff --git a/challenge-213/luca-ferrari/blog-4.txt b/challenge-213/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..b6a7e179e4 --- /dev/null +++ b/challenge-213/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/17/PerlWeeklyChallenge213.html#task2plperl diff --git a/challenge-213/luca-ferrari/blog-5.txt b/challenge-213/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..0b9f4baef7 --- /dev/null +++ b/challenge-213/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/17/PerlWeeklyChallenge213.html#task1plpgsql diff --git a/challenge-213/luca-ferrari/blog-6.txt b/challenge-213/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..aa2fb4e372 --- /dev/null +++ b/challenge-213/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/04/17/PerlWeeklyChallenge213.html#task2plpgsql diff --git a/challenge-213/luca-ferrari/postgresql/ch-1.plperl b/challenge-213/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..b04bcbe768 --- /dev/null +++ b/challenge-213/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,19 @@ +-- +-- Perl Weekly Challenge 213 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-213/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc213; + +CREATE OR REPLACE FUNCTION +pwc213.task1_plperl( int[] ) +RETURNS int[] +AS $CODE$ + my ( $array ) = @_; + my @sorted; + @sorted = ( sort( grep( { $_ % 2 == 0 } $array->@* ) ), + sort( grep( { $_ % 2 != 0 } $array->@* ) ) ); + return [ @sorted ]; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-213/luca-ferrari/postgresql/ch-1.sql b/challenge-213/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..7f274c41e1 --- /dev/null +++ b/challenge-213/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 213 +-- Task 1 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-213/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc213; + +CREATE OR REPLACE FUNCTION +pwc213.task1_plpgsql( a int[] ) +RETURNS int[] +AS $CODE$ + WITH evens AS ( + SELECT array_agg( v ) as x + FROM ( SELECT v FROM unnest( a ) v + WHERE v % 2 = 0 + ORDER BY 1 + ) as v + ), odds AS ( + SELECT array_agg( v ) as x + FROM ( SELECT v FROM unnest( a ) v + WHERE v % 2 <> 0 + ORDER BY 1 ) as v + ) + SELECT array_cat( e.x, o.x ) + FROM evens e, odds o; + +$CODE$ +LANGUAGE sql; diff --git a/challenge-213/luca-ferrari/postgresql/ch-2.plperl b/challenge-213/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..5c826c931a --- /dev/null +++ b/challenge-213/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,59 @@ +-- +-- Perl Weekly Challenge 213 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-213/> +-- + +/* +select * from pwc213.task2_plperl( 1, 7, + array[ + array[1,2,3]::int[], + array[4,5,6]::int[], + array[3,8,9]::int[], + array[6,7,8]::int[] + ]::int[][] ); + +*/ + +CREATE SCHEMA IF NOT EXISTS pwc213; + +CREATE OR REPLACE FUNCTION +pwc213.task2_plperl( int, int, int[][] ) +RETURNS SETOF int +AS $CODE$ + my ( $source, $destination, $routes ) = @_; + my @path; + + push @path, $source; + my ( $loop ) = 1; + my ( $current_route_index ) = 0; + while ( $loop ) { + my ( $current_route ) = $routes->@[ $current_route_index ]; + + # skip this route if there is not a match + $current_route_index++ and next if ( ! grep( { $_ == $path[ -1 ] } $current_route->@* ) ); + + for my $node ( $current_route->@* ) { + push @path, $node if ( ! grep( { $node == $_ } @path ) ); + + $loop = 0; + + # search for the next route + for my $next_route_index ( ( $current_route_index + 1 ) .. scalar( $routes->@* ) ) { + next if ( ! grep( { $node == $_ } $routes->@[ $next_route_index ]->@* ) ); + $current_route_index = $next_route_index; + $loop = 1; + last; + } + + last if $loop; + } + + last if $current_route_index > scalar( $routes->@* ); + } + + return undef if $path[ -1 ] != $destination; + return [ @path ]; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-213/luca-ferrari/postgresql/ch-2.sql b/challenge-213/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..520debcff9 --- /dev/null +++ b/challenge-213/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,64 @@ +-- +-- Perl Weekly Challenge 213 +-- Task 2 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-213/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc213; + +/* +CREATE OR REPLACE FUNCTION +pwc213.task2_plpgsql( s int, d int, routes int[] ) +RETURNS SETOF int +AS $CODE$ + SELECT pwc213.task2_plperl( s, d, routes ); +$CODE$ +LANGUAGE sql; +*/ + +CREATE OR REPLACE FUNCTION +pwc213.task2_plpgsql( s int, d int, routes int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + slice_size int := 3; + current_route_index int; + current_route int[]; + next_route_index int; + next_node int; + need_loop boolean; + node int; + path int[]; +BEGIN + need_loop := true; + current_route_index := 1; +<<rescan>> + WHILE need_loop LOOP + FOREACH node IN ARRAY routes[ current_route_index : current_route_index ] LOOP + RETURN NEXT node; + IF node = d THEN + EXIT; + END IF; + + need_loop := false; + FOR next_route_index IN current_route_index + 1 .. array_length( routes, 1 ) LOOP + FOREACH next_node IN ARRAY routes[ next_route_index : next_route_index ] LOOP + IF next_node = node THEN + current_route_index := next_route_index; + need_loop := true; + CONTINUE rescan; + END IF; + END LOOP; + END LOOP; + END LOOP; + END LOOP; + + IF node <> d THEN + RAISE EXCEPTION 'Cannot find the path!'; + END IF; + + return; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-213/luca-ferrari/raku/ch-1.p6 b/challenge-213/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..ddb2238999 --- /dev/null +++ b/challenge-213/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 213 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-213/> +# + +sub MAIN( *@n where { @n.elems == @n.grep( { $_ ~~ Int && $_ > 0 } ).elems } ) { + ( @n.grep( * %% 2 ).sort.join( ',' ) ~ ',' ~ @n.grep( * !%% 2 ).sort.join( ',' ) ).say; +} diff --git a/challenge-213/luca-ferrari/raku/ch-2.p6 b/challenge-213/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..936a61356d --- /dev/null +++ b/challenge-213/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,61 @@ +#!raku + +# +# Perl Weekly Challenge 213 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-213/> +# +# +# raku raku/ch-2.p6 1 7 "1 2 3 | 4 5 6 | 3 8 9 | 7 8 " +# 1 -> 2 -> 3 -> 8 -> 7 + + +sub MAIN( Int $source, Int $destination, $r ) { + my @routes; + my @current; + for $r.comb( :skip-empty ) { + next if ! $_; + + if $_ ~~ "|" { + @routes.push: [@current]; + @current = (); + next; + } + + @current.push: $_.Int if ( $_.Int > 0 ); + + } + + @routes.push: [@current] if ( @current ); + + my ( $source-index, $destination-index ); + for 0 ..^ @routes.elems -> $index { + $source-index = $index and next if ( @routes[ $index ].grep( $source ) ); + $destination-index = $index and next if ( @routes[ $index ].grep( $destination ) ); + } + + + my @current-path; + my $next-route = $source-index; + my $loop = True; + while ( $loop ) { + for @routes[ $next-route ].Array -> $node { + @current-path.push: $node if ( ! @current-path.grep( * ~~ $node ) ); + + $loop = False; + for $next-route ^..^ @routes.elems -> $jump-to { + + if ( @routes[ $jump-to ].grep( { $_ ~~ $node } ) ) { + $next-route = $jump-to; + $loop = True; + last; + } + } + + last if $loop; + } + } + + @current-path.join( ' -> ' ).say; +} |
