diff options
| -rw-r--r-- | challenge-236/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/postgresql/ch-1.plperl | 45 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/postgresql/ch-1.sql | 79 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/postgresql/ch-2.plperl | 37 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/postgresql/ch-2.sql | 37 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/raku/ch-1.p6 | 43 | ||||
| -rw-r--r-- | challenge-236/luca-ferrari/raku/ch-2.p6 | 34 |
12 files changed, 281 insertions, 0 deletions
diff --git a/challenge-236/luca-ferrari/blog-1.txt b/challenge-236/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..19b1fa82db --- /dev/null +++ b/challenge-236/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/09/25/PerlWeeklyChallenge236.html#task1 diff --git a/challenge-236/luca-ferrari/blog-2.txt b/challenge-236/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..867d27819a --- /dev/null +++ b/challenge-236/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/09/25/PerlWeeklyChallenge236.html#task2 diff --git a/challenge-236/luca-ferrari/blog-3.txt b/challenge-236/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..227df3c9e2 --- /dev/null +++ b/challenge-236/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/09/25/PerlWeeklyChallenge236.html#task1plperl diff --git a/challenge-236/luca-ferrari/blog-4.txt b/challenge-236/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..6b621e8fa8 --- /dev/null +++ b/challenge-236/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/09/25/PerlWeeklyChallenge236.html#task2plperl diff --git a/challenge-236/luca-ferrari/blog-5.txt b/challenge-236/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..418ce07acd --- /dev/null +++ b/challenge-236/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/09/25/PerlWeeklyChallenge236.html#task1plpgsql diff --git a/challenge-236/luca-ferrari/blog-6.txt b/challenge-236/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..baf62bbc74 --- /dev/null +++ b/challenge-236/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/09/25/PerlWeeklyChallenge236.html#task2plpgsql diff --git a/challenge-236/luca-ferrari/postgresql/ch-1.plperl b/challenge-236/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..9bfa6653dc --- /dev/null +++ b/challenge-236/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,45 @@ +-- +-- Perl Weekly Challenge 236 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-236/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc236; + +CREATE OR REPLACE FUNCTION +pwc236.task1_plperl( int[] ) +RETURNS boolean +AS $CODE$ + my ( $cash ) = @_; + + my $remainder = {}; + + for my $current_cash ( $cash->@* ) { + $remainder->{ $current_cash }++; + next if $current_cash == 5; + + if ( $current_cash == 10 ) { + return 0 if ( $remainder->{ 5 } == 0 ); + $remainder->{ 5 }--; + } + + if ( $current_cash == 20 ) { + if ( $remainder->{ 10 } > 0 && $remainder->{ 5 } > 0 ) { + $remainder->{ 5 }--; + $remainder->{ 10 }--; + } + elsif ( $remainder->{ 10 } == 0 && $remainder->{ 5 } >= 3 ) { + $remainder->{ 5 } -= 3; + } + else { + return 0; + } + + + } + + } + + return 1; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-236/luca-ferrari/postgresql/ch-1.sql b/challenge-236/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..21408fd5a6 --- /dev/null +++ b/challenge-236/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,79 @@ +-- +-- Perl Weekly Challenge 236 +-- Task 1 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-236/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc236; + +CREATE OR REPLACE FUNCTION +pwc236.task1_plpgsql( cashing int[] ) +RETURNS boolean +AS $CODE$ +DECLARE + current_cash int; + available_qty_10 int; + available_qty_5 int; +BEGIN + + CREATE TEMPORARY TABLE IF NOT EXISTS remainder( cash int, quantity int default 1 ); + TRUNCATE TABLE remainder; + INSERT INTO remainder( cash, quantity ) + VALUES + (5, 0 ), ( 10, 0 ), ( 15, 0 ); + + FOREACH current_cash IN ARRAY cashing LOOP + UPDATE remainder + SET quantity = quantity + 1 + WHERE cash = current_cash; + + IF current_cash = 5 THEN + CONTINUE; + END IF; + + IF current_cash = 10 THEN + SELECT quantity + INTO available_qty_5 + FROM remainder + WHERE cash = 5; + + IF available_qty_5 > 0 THEN + UPDATE remainder + SET quantity = quantity - 1 + WHERE cash = 5; + ELSE + RETURN false; + END IF; + END IF; + + + IF current_cash = 20 THEN + SELECT quantity + INTO available_qty_10 + FROM remainder + WHERE cash = 10; + + SELECT quantity + INTO available_qty_5 + FROM remainder + WHERE cash = 5; + + IF available_qty_10 > 0 AND available_qty_5 > 0 THEN + UPDATE remainder + SET quantity = quantity - 1 + WHERE cash IN ( 5, 10 ); + ELSIF available_qty_10 = 0 and available_qty_5 >= 3 THEN + UPDATE remainder + SET quantity = quantity - 3 + WHERE cash = 5; + ELSE + RETURN false; + END IF; + END IF; + END LOOP; + + return true; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-236/luca-ferrari/postgresql/ch-2.plperl b/challenge-236/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..2113dfdb81 --- /dev/null +++ b/challenge-236/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 236 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-236/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc236; + +CREATE OR REPLACE FUNCTION +pwc236.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $nums ) = @_; + my @loops; + + for my $current_start ( 0 .. $nums->@* ) { + my @current_path = (); + + push @current_path, $nums->[ $current_start ]; + my $next = $nums->[ $current_start ]; + + while ( 0 < $next < scalar( $nums->@* ) ) { + push @current_path, $nums->[ $next ]; + + if ( $nums->[ $next ] == $current_start ) { + push @loops, \@current_path; + last; + } + + $next = $nums->[ $next ]; + } + } + + + return scalar @loops; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-236/luca-ferrari/postgresql/ch-2.sql b/challenge-236/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..1a06b50f9b --- /dev/null +++ b/challenge-236/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 236 +-- Task 2 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-236/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc236; + +CREATE OR REPLACE FUNCTION +pwc236.task2_plpgsql( nums int[] ) +RETURNS int +AS $CODE$ +DECLARE + current_start int; + loops int := 0; + next int; +BEGIN + + FOR current_start in 1 .. array_length( nums, 1 ) LOOP + next := nums[ current_start ]; + + WHILE next >= 1 AND next <= array_length( nums, 1 ) LOOP + IF nums[ next ] = current_start THEN + loops := loops + 1; + EXIT; + END IF; + + next := nums[ next ]; + END LOOP; + END LOOP; + + RETURN loops; +END + +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-236/luca-ferrari/raku/ch-1.p6 b/challenge-236/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..fa6797a8be --- /dev/null +++ b/challenge-236/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,43 @@ +#!raku + +# +# Perl Weekly Challenge 236 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-236/> +# + +sub MAIN( *@cash where { @cash.grep( * % 5 == 0 ).elems == @cash.elems } ) { + + my %remainder; + %remainder{ $_ } = 0 for 5,10,20; + + for @cash -> $current_cash { + %remainder{ $current_cash }++; + next if $current_cash == 5; + + + if ( $current_cash == 10 and %remainder{ 5 } > 0 ) { + %remainder{ 5 }--; + } + elsif $current_cash == 20 { + if %remainder{ 10 } > 0 && %remainder{ 5 } > 0 { + %remainder{ 5 }--; + %remainder{ 10 }--; + } + elsif %remainder{ 10 } == 0 && %remainder{ 5 } > 3 { + %remainder{ 5 } -= 3; + } + else { + 'False'.say and exit; + } + } + else { + # cannot proceed + 'False'.say and exit; + } + + } + + 'True'.say; +} diff --git a/challenge-236/luca-ferrari/raku/ch-2.p6 b/challenge-236/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..3b5618640b --- /dev/null +++ b/challenge-236/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,34 @@ +#!raku + +# +# Perl Weekly Challenge 236 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-236/> +# + +sub MAIN( *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + + my @loops; + for 0 ..^ @nums.elems -> $current-start { + my @current-path = (); + + @current-path.push: @nums[ $current-start ]; + + my $next = @nums[ $current-start ]; + while ( 0 < $next < @nums.elems ) { + @current-path.push: @nums[ $next ]; + + if @nums[ $next ] == $current-start { + # loop detected + @loops.push: @current-path; + last; + } + + $next = @nums[ $next ]; + } + + } + + @loops.elems.say; +} |
