diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2022-11-20 14:22:01 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2022-11-20 14:22:01 -0500 |
| commit | dd682dfee966fe63cbfbbbf6a9cb903b1d831416 (patch) | |
| tree | a71619e10c8dcd29fc13a08beb1325f4a7bc5a84 /challenge-191/luca-ferrari/postgresql | |
| parent | d6d01468fd7a5647b9ba96ebf7a0157ff79f3352 (diff) | |
| parent | bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (diff) | |
| download | perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.tar.gz perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.tar.bz2 perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-191/luca-ferrari/postgresql')
| -rw-r--r-- | challenge-191/luca-ferrari/postgresql/ch-1.plperl | 36 | ||||
| -rw-r--r-- | challenge-191/luca-ferrari/postgresql/ch-1.sql | 33 | ||||
| -rw-r--r-- | challenge-191/luca-ferrari/postgresql/ch-2.plperl | 44 | ||||
| -rw-r--r-- | challenge-191/luca-ferrari/postgresql/ch-2.sql | 71 |
4 files changed, 184 insertions, 0 deletions
diff --git a/challenge-191/luca-ferrari/postgresql/ch-1.plperl b/challenge-191/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..321ae4b3a8 --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,36 @@ +-- Perl Weekly Challenge 191 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc191; + + +/* +testdb=> select * from pwc191.task1_plperl( ARRAY[1,2,3,6]::int[] ); + task1_plperl +-------------- + 1 + +*/ +CREATE OR REPLACE FUNCTION +pwc191.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ($l) = @_; + + my $max = 0; + + # compute the max element + for ( $l->@* ) { + $max = $_ if ( $max < $_ ); + } + + # iterate on all elements and see + # if one of the is doubly greater than the max + for ( $l->@* ) { + next if $_ == $max; + return -1 if $_ * 2 > $max; + } + + return 1; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-191/luca-ferrari/postgresql/ch-1.sql b/challenge-191/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..a1378c4676 --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,33 @@ +-- Perl Weekly Challenge 191 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc191; + +CREATE OR REPLACE FUNCTION +pwc191.task1_plpgsql( l int[] ) +RETURNS int +AS $CODE$ +DECLARE + current_max int; + wrong int := 0; +BEGIN + -- compute the max + SELECT max( v ) + INTO current_max + FROM unnest( l ) v; + + SELECT count(*) + INTO wrong + FROM unnest( l ) v + WHERE ( v * 2 ) > current_max + AND v <> current_max; + + IF wrong > 0 THEN + RETURN -1; + ELSE + RETURN 1; + END IF; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-191/luca-ferrari/postgresql/ch-2.plperl b/challenge-191/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..98cfcd741b --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,44 @@ +-- Perl Weekly Challenge 191 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc191; + +CREATE OR REPLACE FUNCTION +pwc191.task2_plperl( int ) +RETURNS int +AS $CODE$ + use List::Permute::Limit qw(permute_iter permute); + + my ($n) = @_; + my $cute_counter = 0; + my @l = ( 1 .. $n ); + + my @permutations = permute( items => [ @l ], nitems => $n ); + for my $current_list ( @permutations ) { + + my $is_cute = 1; + for my $i ( 0 .. $current_list->@* ) { + + if ( $current_list->[ $i ] % ( $i + 1 ) != 0 ) { + $is_cute = 0; + last; + } + } + + $cute_counter++ if ( $is_cute ); + + $is_cute = 1; + for my $i ( 0 .. $current_list->@* ) { + + if ( ( $i + 1 ) % $current_list->[ $i ] != 0 ) { + $is_cute = 0; + last; + } + } + + $cute_counter++ if ( $is_cute ); + } + + return $cute_counter; +$CODE$ +LANGUAGE plperlu; diff --git a/challenge-191/luca-ferrari/postgresql/ch-2.sql b/challenge-191/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..7b4755b34c --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,71 @@ +-- Perl Weekly Challenge 191 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc191; + +CREATE OR REPLACE FUNCTION +pwc191.task2_plpgsql( n int ) +RETURNS int +AS $CODE$ +DECLARE + cute_counter int := 0; + i int; + src int[]; + permutation int[]; + is_cute bool; +BEGIN + + FOR i IN 1 .. n LOOP + src = src || i; + END LOOP; + + + + FOR permutation IN + with recursive + data as (select src as arr), + keys as (select generate_subscripts(d.arr, 1) as rn from data d), + cte as ( + select d.arr initial_arr, array[d.arr[k.rn]] new_arr, array[k.rn] used_rn + from data d + cross join keys k + union all + select initial_arr, c.new_arr || c.initial_arr[k.rn], used_rn || k.rn + from cte c + inner join keys k on not (k.rn = any(c.used_rn)) + ) + select new_arr + from cte + WHERE array_length( new_arr, 1 ) = n + LOOP + + is_cute := 1; + FOR i IN 1 .. array_length( permutation, 1 ) LOOP + IF permutation[i] % i <> 0 THEN + is_cute = false; + EXIT; + END IF; + END LOOP; + + IF is_cute THEN + cute_counter := cute_counter + 1; + END IF; + + is_cute := 1; + FOR i IN 1 .. array_length( permutation, 1 ) LOOP + IF i % permutation[i] <> 0 THEN + is_cute = false; + EXIT; + END IF; + END LOOP; + IF is_cute THEN + cute_counter := cute_counter + 1; + END IF; + + END LOOP; + + +RETURN cute_counter; +END +$CODE$ +LANGUAGE plpgsql; |
