diff options
| -rw-r--r-- | challenge-238/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/blog-7.txt | 1 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/postgresql/ch-1.plperl | 24 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/postgresql/ch-1.sql | 18 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/postgresql/ch-2.2.sql | 53 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/postgresql/ch-2.plperl | 50 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/postgresql/ch-2.sql | 57 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/python/ch-2.py | 42 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/raku/ch-1.p6 | 17 | ||||
| -rw-r--r-- | challenge-238/luca-ferrari/raku/ch-2.p6 | 26 |
16 files changed, 316 insertions, 0 deletions
diff --git a/challenge-238/luca-ferrari/blog-1.txt b/challenge-238/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..934bfbed25 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task1 diff --git a/challenge-238/luca-ferrari/blog-2.txt b/challenge-238/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..b99739a572 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2 diff --git a/challenge-238/luca-ferrari/blog-3.txt b/challenge-238/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..4dfe404108 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task1plperl diff --git a/challenge-238/luca-ferrari/blog-4.txt b/challenge-238/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..2f0f8a781f --- /dev/null +++ b/challenge-238/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2plperl diff --git a/challenge-238/luca-ferrari/blog-5.txt b/challenge-238/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..6e864012bf --- /dev/null +++ b/challenge-238/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task1plpgsql diff --git a/challenge-238/luca-ferrari/blog-6.txt b/challenge-238/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..7893f25aa6 --- /dev/null +++ b/challenge-238/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2plpgsql diff --git a/challenge-238/luca-ferrari/blog-7.txt b/challenge-238/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..281cdbbfda --- /dev/null +++ b/challenge-238/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/10/09/PerlWeeklyChallenge238.html#task2plpgsql_b diff --git a/challenge-238/luca-ferrari/postgresql/ch-1.plperl b/challenge-238/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..d3c64a1cf1 --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-238/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task1_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + my ( $nums ) = @_; + + for my $index ( 0 .. $nums->@* - 1 ) { + my $sum = 0; + + $sum += $_ for ( $nums->@[ 0 .. $index ] ); + return_next( $sum ); + } + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-238/luca-ferrari/postgresql/ch-1.sql b/challenge-238/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..3663306f9f --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,18 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 1 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-238/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task1_plpgsql( nums int[] ) +RETURNS TABLE( v int, s int ) +AS $CODE$ + + SELECT v, sum( v ) OVER ( ORDER BY v ) + FROM unnest( nums ) v; +$CODE$ +LANGUAGE sql; diff --git a/challenge-238/luca-ferrari/postgresql/ch-2.2.sql b/challenge-238/luca-ferrari/postgresql/ch-2.2.sql new file mode 100644 index 0000000000..d1456ab3ee --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-2.2.sql @@ -0,0 +1,53 @@ +CREATE OR REPLACE FUNCTION pwc238.reduce( n int ) +RETURNS int +AS $CODE$ +DECLARE + current_value int; + step_counter int; + digit text; + multiplication int; + +BEGIN + current_value := n; + step_counter := 0; + + WHILE current_value > 9 LOOP + multiplication := 1; + step_counter := step_counter + 1; + + FOREACH digit IN ARRAY regexp_split_to_array( current_value::text, '' ) LOOP + multiplication := multiplication * digit::int; + END LOOP; + + current_value := multiplication; + END LOOP; + + RETURN step_counter; +END +$CODE$ +LANGUAGE plpgsql; + + + +-- +-- Function task2_plpgsql +-- Schema pwc238 +-- +-- Description: +-- +-- +-- Return Type: SETOF INT +-- +CREATE OR REPLACE FUNCTION +pwc238.task2_plpgsql( nums int[] ) +RETURNS SETOF INT +AS $CODE$ + +SELECT v +FROM unnest( nums ) v +ORDER BY pwc238.reduce( v ), v; + +$CODE$ +LANGUAGE sql +VOLATILE +; diff --git a/challenge-238/luca-ferrari/postgresql/ch-2.plperl b/challenge-238/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..9a2cbcccbd --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,50 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 2 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-238/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + my ( $nums ) = @_; + + my $steps = {}; + + # utility function to reduce a number + # does only one pass so that I can counter + # how many passes are required + my $reduce = sub { + my ( $number ) = @_; + return $number if ( $number <= 9 ); + + my $value = 1; + for my $digit ( split( '', $number ) ) { + $value *= $digit; + } + + return $value; + }; + + for ( $nums->@* ) { + my $step_counter = 0; + my $value = $_; + + while ( $value > 9 ) { + $value = $reduce->( $value ); + $step_counter++; + } + + push $steps->{ $step_counter }->@*, $_; + } + + for my $key ( sort keys $steps->%* ) { + return_next( $_ ) for ( sort $steps->{ $key }->@* ) + } + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-238/luca-ferrari/postgresql/ch-2.sql b/challenge-238/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..95d6555633 --- /dev/null +++ b/challenge-238/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,57 @@ +-- +-- Perl Weekly Challenge 238 +-- Task 2 +-- +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-238/> +-- + +CREATE SCHEMA IF NOT EXISTS pwc238; + +CREATE OR REPLACE FUNCTION +pwc238.task2_plpgsql( nums int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current_value int; + digit text; + multiplication int; + step_counter int; + value_to_insert int; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS mul( v int, mul int, steps int DEFAULT 0 ); + TRUNCATE mul; + + FOREACH current_value IN ARRAY nums LOOP + IF current_value < 9 THEN + INSERT INTO mul( v, mul ) + VALUES( current_value, current_value ); + CONTINUE; + END IF; + + -- if here the number is at least two digits long + step_counter := 0; + value_to_insert := current_value; + WHILE current_value > 9 LOOP + multiplication := 1; + step_counter := step_counter + 1; + + FOREACH digit IN ARRAY regexp_split_to_array( current_value::text, '' ) LOOP + multiplication := multiplication * digit::int; + END LOOP; + + current_value := multiplication; + END LOOP; + + INSERT INTO mul( v, mul, steps ) + VALUES( value_to_insert, multiplication, step_counter ); + + END LOOP; + + + RETURN QUERY SELECT v + FROM mul + ORDER BY steps ASC, v ASC; +END + +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-238/luca-ferrari/python/ch-1.py b/challenge-238/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..67507eb558 --- /dev/null +++ b/challenge-238/luca-ferrari/python/ch-1.py @@ -0,0 +1,22 @@ +#!python + +import sys + +def main( argv ): + running_sum = [] + current_index = 0 + while current_index < len( argv ): + running_sum.insert( current_index, 0 ) + + for n in argv[ 0 : current_index ]: + running_sum[ current_index ] += int( n ) + + current_index += 1 + + print( ", ".join( map( str, running_sum ) ) ) + + + + +if __name__ == '__main__': + main( sys.argv[ 1: ] ) diff --git a/challenge-238/luca-ferrari/python/ch-2.py b/challenge-238/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..183d67e65d --- /dev/null +++ b/challenge-238/luca-ferrari/python/ch-2.py @@ -0,0 +1,42 @@ +#!python + +import sys +import collections + +def reduce( n ): + if n <= 9: + return n + + multiplication = 1 + for digit in map( int, str( n ) ): + multiplication *= digit + + return multiplication + + +def main( argv ): + steps = {} + for n in map( int, argv ): + current_step = 0 + if n > 9: + # need reduction + current_value = n + while current_value > 9: + current_value = reduce( current_value ) + current_step += 1 + + # if the key is not here, create a list + if not str( current_step ) in steps: + steps[ str( current_step ) ] = [] + + steps[ str(current_step) ].append( n ) + + # now traverse the dictionary and sort the array + # and print it + for k, v in collections.OrderedDict(sorted(steps.items())).items(): + print( ", ".join( map( str, v ) ) ) + + + +if __name__ == '__main__': + main( sys.argv[ 1: ] ) diff --git a/challenge-238/luca-ferrari/raku/ch-1.p6 b/challenge-238/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..2d6d317076 --- /dev/null +++ b/challenge-238/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 238 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-238/> +# + +sub MAIN( *@nums where { @nums.grep( * ~~ Int ).elems == @nums.elems } ) { + my @running-sum; + for 0 ..^ @nums.elems -> $index { + @running-sum[ $index ] = [+] @nums[ 0 .. $index ]; + } + + @running-sum.join( ', ' ).say; +} diff --git a/challenge-238/luca-ferrari/raku/ch-2.p6 b/challenge-238/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..fe8a4438a4 --- /dev/null +++ b/challenge-238/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#!raku + +# +# Perl Weekly Challenge 238 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-238/> +# + +sub MAIN( *@nums where { @nums.grep( { $_ ~~ Int && $_ > 0 } ).elems == @nums.elems } ) { + my %steps; + for @nums { + my $step-counter = 0; + my $value = $_; + while ( $value > 9 ) { + $value = [*] $value.comb; + $step-counter++; + } + + %steps{ $step-counter }.push: $_; + } + + my @running-sort.push: | %steps{ $_ }.sort for %steps.keys.sort; + @running-sort.join( ', ' ).say; + +} |
