From ea525b69ae60150592aabbeef4fb6c6c4b656ee1 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 22 Nov 2022 09:13:10 +0100 Subject: Task 2 plperl --- challenge-192/luca-ferrari/postgresql/ch-2.plperl | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 challenge-192/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-192/luca-ferrari/postgresql/ch-2.plperl b/challenge-192/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..32c13b6000 --- /dev/null +++ b/challenge-192/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,58 @@ +-- Perl Weekly Challenge 192 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc192; + +CREATE OR REPLACE FUNCTION +pwc192.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + my @moves; + my ($array) = @_; + + # utility function to get the + # max value from the array + my $find_max = sub { + my $max = 0; + for ( @_ ) { + elog(INFO, "value $_" ); + $max = $_ if $_ > $max; + } + + return $max; + }; + + # utility function to get the sum of the array + my $sum_array = sub { + my $sum = 0; + $sum += $_ for ( @_ ); + return $sum; + }; + + my $item = $sum_array->( $array->@* ) / scalar( $array->@* ); + return -1 if ( $item != int($item) ); + + push @moves, $array; + + while ( scalar( grep( { $_ == $item } $array->@* ) ) != scalar( $array->@* ) ) { + my $max = $find_max->( $array->@* ); + for my $index ( 0 .. scalar $array->@* ) { + next if $array->[ $index ] != $max; + + for my $borrow ( 0 .. scalar $array->@* ) { + next if $borrow == $index; + next if $array->[ $borrow ] >= $array->[ $index ]; + next if $array->[ $borrow ] >= $item; + $array->[ $borrow ]++; + last; + } + + $array->[ $index ]--; + } + + push @moves, $array; + } + + return scalar @moves; +$CODE$ +LANGUAGE plperl; -- cgit