diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-04 20:50:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-04 20:50:33 +0000 |
| commit | 912884c211b6479acb33d6ca1230e6484c935d09 (patch) | |
| tree | 25ef096ecd1ea8e70e5b225e8718b823a45715ae | |
| parent | 58472618567092bb0efc28edd5bd6531bad8d9c2 (diff) | |
| parent | 1ee7a405a8953aae0c07d148b99480a976a5121e (diff) | |
| download | perlweeklychallenge-club-912884c211b6479acb33d6ca1230e6484c935d09.tar.gz perlweeklychallenge-club-912884c211b6479acb33d6ca1230e6484c935d09.tar.bz2 perlweeklychallenge-club-912884c211b6479acb33d6ca1230e6484c935d09.zip | |
Merge pull request #5472 from fluca1978/pwc146
Pwc146
| -rw-r--r-- | challenge-146/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-146/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-146/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-146/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-146/luca-ferrari/postgresql/ch-1.sql | 32 | ||||
| -rw-r--r-- | challenge-146/luca-ferrari/postgresql/ch-2.sql | 145 | ||||
| -rwxr-xr-x | challenge-146/luca-ferrari/raku/ch-1.p6 | 6 | ||||
| -rwxr-xr-x | challenge-146/luca-ferrari/raku/ch-2.p6 | 60 |
8 files changed, 247 insertions, 0 deletions
diff --git a/challenge-146/luca-ferrari/blog-1.txt b/challenge-146/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..af17183edb --- /dev/null +++ b/challenge-146/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task1 diff --git a/challenge-146/luca-ferrari/blog-2.txt b/challenge-146/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..5e5d379514 --- /dev/null +++ b/challenge-146/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task2 diff --git a/challenge-146/luca-ferrari/blog-3.txt b/challenge-146/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..aa716b41b9 --- /dev/null +++ b/challenge-146/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task1pg diff --git a/challenge-146/luca-ferrari/blog-4.txt b/challenge-146/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..520b859582 --- /dev/null +++ b/challenge-146/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task2pg diff --git a/challenge-146/luca-ferrari/postgresql/ch-1.sql b/challenge-146/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..766cf944f0 --- /dev/null +++ b/challenge-146/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,32 @@ +CREATE OR REPLACE FUNCTION +f_is_prime( val int ) +RETURNS bool +AS $CODE$ +DECLARE + i int; +BEGIN + IF val <= 0 THEN + RAISE EXCEPTION 'Cannot use a number less than 1!'; + END IF; + + FOR i IN 2 .. ( val - 1 ) LOOP + IF val % i = 0 THEN + RETURN false; + END IF; + END LOOP; + + RETURN true; +END +$CODE$ +LANGUAGE plpgsql; + + +WITH primes AS ( +SELECT n as needle, row_number() OVER( PARTITION BY f_is_prime( n ) ) as idx +FROM generate_series( 1, 10000 ) n +WHERE f_is_prime( n ) +ORDER BY n +) +SELECT * +FROM primes +WHERE idx = 1001; diff --git a/challenge-146/luca-ferrari/postgresql/ch-2.sql b/challenge-146/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..39d2cd1aac --- /dev/null +++ b/challenge-146/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,145 @@ +DROP TABLE IF EXISTS fraction_tree; +CREATE TABLE fraction_tree ( + pk int GENERATED ALWAYS AS IDENTITY + , numerator int default 1 + , denominator int default 1 + , child_of int + , level int default 1 + , PRIMARY KEY( pk ) + , FOREIGN KEY (child_of) REFERENCES fraction_tree( pk ) +); + + +TRUNCATE TABLE fraction_tree; +ALTER TABLE fraction_tree ALTER COLUMN pk RESTART; + + +INSERT INTO fraction_tree( numerator, denominator ) +VALUES( 1, 1 ); + + + + + +CREATE OR REPLACE FUNCTION +f_add_one_level_fraction_tree() +RETURNS INT +AS $CODE$ +DECLARE + current_left fraction_tree%rowtype; + current_right fraction_tree%rowtype; + previous_tuple fraction_tree%rowtype; + nodes_added int := 0; +BEGIN + + FOR previous_tuple IN SELECT * FROM fraction_tree + WHERE level = ( SELECT max( level ) FROM fraction_tree ) + LOOP + + + current_left.numerator := previous_tuple.numerator; + current_left.denominator := ( previous_tuple.numerator + previous_tuple.denominator ); + current_left.child_of := previous_tuple.pk; + current_left.level := previous_tuple.level + 1; + current_left.pk := nextval( 'fraction_tree_pk_seq' ); + + current_right.numerator := ( previous_tuple.numerator + previous_tuple.denominator ); + current_right.denominator := previous_tuple.denominator; + current_right.child_of := previous_tuple.pk; + current_right.level := previous_tuple.level + 1; + current_right.pk := nextval( 'fraction_tree_pk_seq' ); + + INSERT INTO fraction_tree + OVERRIDING SYSTEM VALUE + SELECT current_left.*; + + INSERT INTO fraction_tree + OVERRIDING SYSTEM VALUE + SELECT current_right.*; + + nodes_added := nodes_added + 2; + + END LOOP; + + RETURN nodes_added; +END +$CODE$ +LANGUAGE plpgsql; + + + +CREATE OR REPLACE FUNCTION +f_populate_fraction_tree( levels int default 4 ) +RETURNS int +AS $CODE$ +DECLARE + i int := 0; + nodes_added int := 0; +BEGIN + FOR i IN 1 .. levels LOOP + nodes_added := nodes_added + f_add_one_level_fraction_tree(); + END LOOP; + + RETURN nodes_added; +END +$CODE$ +LANGUAGE plpgsql; + + +/* +testdb=> select * from f_search_for_fraction_tree( 3, 5 ); +description | fraction +-------------+---------- +child | 3/5 +parent | 3/2 +grandparent | 1/2 + +*/ + +CREATE OR REPLACE FUNCTION +f_search_for_fraction_tree( numer int, denomin int ) +RETURNS TABLE ( description text, fraction text ) +AS $CODE$ +DECLARE + current_tuple fraction_tree%rowtype; +BEGIN + SELECT 'child', numerator || '/' || denominator + INTO description, fraction + FROM fraction_tree + WHERE numerator = numer + AND denominator = denomin; + + IF FOUND THEN + RETURN NEXT; + + + SELECT 'parent', numerator || '/' || denominator + INTO description, fraction + FROM fraction_tree + WHERE pk = ( SELECT child_of + FROM fraction_tree + WHERE numerator = numer + AND denominator = denomin ); + + RETURN NEXT; + + + + SELECT 'grandparent', numerator || '/' || denominator + INTO description, fraction + FROM fraction_tree + WHERE pk = ( SELECT child_of + FROM fraction_tree + WHERE pk = ( SELECT child_of + FROM fraction_tree + WHERE numerator = numer + AND denominator = denomin ) ); + + RETURN NEXT; + + END IF; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-146/luca-ferrari/raku/ch-1.p6 b/challenge-146/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..2b9b4de3d3 --- /dev/null +++ b/challenge-146/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,6 @@ +#!raku + +sub MAIN( Int $which where { $which > 0 } = 1001 ) { + my @primes = lazy { ( 1 .. Inf ).grep( *.is-prime ); } + @primes[ $which ].say; +} diff --git a/challenge-146/luca-ferrari/raku/ch-2.p6 b/challenge-146/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..04588636ee --- /dev/null +++ b/challenge-146/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,60 @@ +#!raku + + +class Node { + has Rat $.member; + has Int $.level; + has Node $.left; + has Node $.right; + has Node $.parent is rw; + + submethod BUILD( Rat :$member, Int :$level = 1, Int :$stop-at = 4 ) { + $!member = $member; + $!level = $level; + + if ( $level < $stop-at ) { + my $sum = $!member.numerator + $!member.denominator; + $!left = Node.new: member => $member.numerator / $sum, + level => $level + 1, + stop-at => $stop-at; + $!right = Node.new: member => $sum / $member.denominator, + level => $level + 1, + stop-at => $stop-at; + } + } + + + method adjust() { + $!left.parent = self if $!left; + $!right.parent = self if $!right; + $!left.adjust if $!left; + $!right.adjust if $!right; + } + + method search-from-here ( Rat $needle ) { + return self if $!member ~~ $needle; + + if ( $!left ) { + my $left = $!left.search-from-here( $needle ); + return $left if $left; + } + if ( $!right ) { + my $right = $!right.search-from-here( $needle ); + return $right if $right; + } + return Nil; + } + + method Str(){ $!member.numerator ~ '/' ~ $!member.denominator } +} + +sub MAIN( Rat $member ) { + my $level = 1; + my $root = Node.new: member => 1.Rat; + $root.adjust; + + + my Node $which = $root.search-from-here( $member ); + "Not found $member " and exit if ! $which; + "Node $member found: { $which.Str } with parent { $which.parent.Str } and grandparent { $which.parent.parent.Str }".say; +} |
