diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2023-12-04 09:25:28 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2023-12-04 10:45:50 +0100 |
| commit | 82cc2a66e5d066cdded6a7f4dbed7fccd23c2c11 (patch) | |
| tree | fb76ca1ec54dbc24af9f69d5e18d8500e612ef2e /challenge-246 | |
| parent | b41848354dc1a09ae68e01e91b551a36d13f8bda (diff) | |
| download | perlweeklychallenge-club-82cc2a66e5d066cdded6a7f4dbed7fccd23c2c11.tar.gz perlweeklychallenge-club-82cc2a66e5d066cdded6a7f4dbed7fccd23c2c11.tar.bz2 perlweeklychallenge-club-82cc2a66e5d066cdded6a7f4dbed7fccd23c2c11.zip | |
PWC 246
Task 1 Raku done
Task 2 Raku done
Task 1 PL/Perl done
Task 2 PL/Perl done
Task 1 PL/PgSQL done
Task 2 PL/PgSQL done
Task 1 Python done
Task 2 Pythomn done
Diffstat (limited to 'challenge-246')
| -rw-r--r-- | challenge-246/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/blog-7.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/blog-8.txt | 1 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/postgresql/ch-1.plperl | 25 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/postgresql/ch-1.sql | 42 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/postgresql/ch-2.plperl | 39 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/postgresql/ch-2.sql | 49 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/python/ch-1.py | 28 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/python/ch-2.py | 43 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/raku/ch-1.p6 | 18 | ||||
| -rw-r--r-- | challenge-246/luca-ferrari/raku/ch-2.p6 | 35 |
16 files changed, 287 insertions, 0 deletions
diff --git a/challenge-246/luca-ferrari/blog-1.txt b/challenge-246/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..3a5f348032 --- /dev/null +++ b/challenge-246/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task1 diff --git a/challenge-246/luca-ferrari/blog-2.txt b/challenge-246/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..340eea71f4 --- /dev/null +++ b/challenge-246/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task2 diff --git a/challenge-246/luca-ferrari/blog-3.txt b/challenge-246/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..4910540ed3 --- /dev/null +++ b/challenge-246/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task1plperl diff --git a/challenge-246/luca-ferrari/blog-4.txt b/challenge-246/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..36ff6aaa4b --- /dev/null +++ b/challenge-246/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task2plperl diff --git a/challenge-246/luca-ferrari/blog-5.txt b/challenge-246/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..8240159cf8 --- /dev/null +++ b/challenge-246/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task1plpgsql diff --git a/challenge-246/luca-ferrari/blog-6.txt b/challenge-246/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..6fe895e7cd --- /dev/null +++ b/challenge-246/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task2plpgsql diff --git a/challenge-246/luca-ferrari/blog-7.txt b/challenge-246/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..42768777f8 --- /dev/null +++ b/challenge-246/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task1python diff --git a/challenge-246/luca-ferrari/blog-8.txt b/challenge-246/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..a4be490f17 --- /dev/null +++ b/challenge-246/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/04/PerlWeeklyChallenge246.html#task2python diff --git a/challenge-246/luca-ferrari/postgresql/ch-1.plperl b/challenge-246/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..4b63049238 --- /dev/null +++ b/challenge-246/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,25 @@ +-- +-- Perl Weekly Challenge 246 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc246; + +CREATE OR REPLACE FUNCTION +pwc246.task1_plperl() +RETURNS SETOF int +AS $CODE$ + my @lottery; + + while ( @lottery < 6 ) { + my $value = int ( rand( 49 ) ); + if ( ! grep( { $_ == $value } @lottery ) ) { + push @lottery, $value; + return_next( $value ); + } + } + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-246/luca-ferrari/postgresql/ch-1.sql b/challenge-246/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..18b201248f --- /dev/null +++ b/challenge-246/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,42 @@ +-- +-- Perl Weekly Challenge 246 +-- Task 1 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc246; + +CREATE OR REPLACE FUNCTION +pwc246.task1_plpgsql() +RETURNS SETOF INT +AS $CODE$ +DECLARE + counting int; + current_value int; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS lottery( v int, CHECK( v <= 49 ), PRIMARY KEY( v ) ); + TRUNCATE lottery; + + counting := 0; + WHILE counting < 6 LOOP + current_value := ( random() * 100 )::int; + + IF current_value <= 49 THEN + INSERT INTO lottery( v ) + VALUES( current_value ) + ON CONFLICT ( v ) DO NOTHING; + END IF; + + + SELECT count(*) + INTO counting + FROM lottery; + + END LOOP; + + RETURN QUERY + SELECT * FROM lottery; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-246/luca-ferrari/postgresql/ch-2.plperl b/challenge-246/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..620df1d4e9 --- /dev/null +++ b/challenge-246/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,39 @@ +-- +-- Perl Weekly Challenge 246 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc246; + +CREATE OR REPLACE FUNCTION +pwc246.task2_plperl( int[] ) +RETURNS bool +AS $CODE$ + my ( $nums ) = @_; + + die "Need 5 numbers" if ( $nums->@* != 5 ); + my $ok = 0; + my $ko = $nums->@* - 2; + + for ( 2 .. $nums->@* - 1 ) { + for ( my $p = 1; ; $p++ ) { + for ( my $q = 1; ; $q++ ) { + if ( $nums->@[ $_ ] == ( $p * $nums->[ $_ - 2 ] + $q * $nums->[ $_ - 1 ] ) + || $nums->@[ $_ ] == ( $p * -1 * $nums->[ $_ - 2 ] + $q * $nums->[ $_ - 1 ] ) + || $nums->@[ $_ ] == ( $p * -1 * $nums->[ $_ - 2 ] + $q * -1 * $nums->[ $_ - 1 ] ) + || $nums->@[ $_ ] == ( $p * $nums->[ $_ - 2 ] + $q * -1 * $nums->[ $_ - 1 ] ) ) { + + $ok = 1; + $ko--; + last; + } + } + + last if $ok; + } + } + + return ( $ko == 0 ); +$CODE$ +LANGUAGE plperl; diff --git a/challenge-246/luca-ferrari/postgresql/ch-2.sql b/challenge-246/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..245db32687 --- /dev/null +++ b/challenge-246/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,49 @@ +-- +-- Perl Weekly Challenge 246 +-- Task 2 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc246; + +CREATE OR REPLACE FUNCTION +pwc246.task2_plpgsql( nums int[] ) +RETURNS bool +AS $CODE$ +DECLARE + current_index int; + p int; + q int; + ok bool := false; + ko int; +BEGIN + + ko := array_length( nums, 1 ) - 3 + 1; + + FOR current_index IN 3 .. array_length( nums, 1 ) LOOP + ok := false; + + FOR p IN 1 .. 99999 LOOP + FOR q IN 1 .. 9999 LOOP + IF nums[ current_index ] = ( p * nums[ current_index - 2 ] + q * nums[ current_index - 1 ] ) + OR nums[ current_index ] = ( p * -1 * nums[ current_index - 2 ] + q * nums[ current_index - 1 ] ) + OR nums[ current_index ] = ( p * -1 * nums[ current_index - 2 ] + q * -1 *nums[ current_index - 1 ] ) + OR nums[ current_index ] = ( p * nums[ current_index - 2 ] + q * -1 * nums[ current_index - 1 ] ) THEN + ok := true; + ko := ko - 1; + EXIT; + END IF; + END LOOP; + + IF ok THEN + EXIT; + END IF; + + END LOOP; + END LOOP; + + RETURN ko = 0; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-246/luca-ferrari/python/ch-1.py b/challenge-246/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..98b32d6125 --- /dev/null +++ b/challenge-246/luca-ferrari/python/ch-1.py @@ -0,0 +1,28 @@ +#!python + +# +# Perl Weekly Challenge 246 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +# + +import sys +import random + +# task implementation +def main( argv ): + values = [] + while len( values ) < 6: + current_value = random.randint( 1, 49 ) + if not current_value in values: + values.append( current_value ) + + print( "\n".join( map( str, values ) ) ) + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-246/luca-ferrari/python/ch-2.py b/challenge-246/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..dbd0395ca7 --- /dev/null +++ b/challenge-246/luca-ferrari/python/ch-2.py @@ -0,0 +1,43 @@ +#!python + +# +# Perl Weekly Challenge 246 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +# + +import sys +from itertools import count + +# task implementation +def main( argv ): + nums = list( map( int, argv ) ) + ko = len( nums ) - 2 + + for current_index in range( 2, len( nums ) ): + for p in count( start = 1 ): + for q in count( start = 1 ): + if nums[ current_index ] == ( p * nums[ current_index - 2 ] + q * nums[ current_index - 1 ] ) \ + or nums[ current_index ] == ( p * -1 * nums[ current_index - 2 ] + q * nums[ current_index - 1 ] ) \ + or nums[ current_index ] == ( p * -1 * nums[ current_index - 2 ] + q * -1 * nums[ current_index - 1 ] ) \ + or nums[ current_index ] == ( p * nums[ current_index - 2 ] + q * -1 * nums[ current_index - 1 ] ): + ok = True + ko = ko - 1 + break + + if ok: + break + + if ko == 0: + print( 'True' ) + else: + print( 'False' ) + + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-246/luca-ferrari/raku/ch-1.p6 b/challenge-246/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..59c15b6561 --- /dev/null +++ b/challenge-246/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,18 @@ +#!raku + +# +# Perl Weekly Challenge 246 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +# + +sub MAIN() { + + my @lottery; + while ( @lottery.elems < 6 ) { + @lottery.push: $_ if ( ! @lottery.grep( $_ ) ) given ( 49.rand.Int ); + } + + @lottery.join( "\n" ).say; +} diff --git a/challenge-246/luca-ferrari/raku/ch-2.p6 b/challenge-246/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..86b550e9f6 --- /dev/null +++ b/challenge-246/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,35 @@ +#!raku + +# +# Perl Weekly Challenge 246 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-246/> +# + +sub MAIN( *@nums where { @nums.elems == 5 && @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + + for 2 ..^ @nums.elems { + # a[n] = p * a[n-2] + q * a[n-1] with n > 1 + + my $ok = False; + for 1 .. Inf -> $p { + for 1 .. Inf -> $q { + if ( @nums[ $_ ] == ( $p * @nums[ $_ - 2 ] + $q * @nums[ $_ - 1 ] ) + || @nums[ $_ ] == ( $p * -1 * @nums[ $_ - 2 ] + $q * @nums[ $_ - 1 ] ) + || @nums[ $_ ] == ( $p * -1 * @nums[ $_ - 2 ] + $q * -1 * @nums[ $_ - 1 ] ) + || @nums[ $_ ] == ( $p * @nums[ $_ - 2 ] + $q * -1 * @nums[ $_ - 1 ] ) ) { + $ok = True; + last; + } + } + + last if $ok; + } + + + 'False'.say and exit if ! $ok; + } + + 'True'.say; +} |
