diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-23 10:41:59 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-23 10:41:59 +0000 |
| commit | 7d4b749f794162198db90d8446eacffe9de9dd8e (patch) | |
| tree | 811d9d3216025bbfed8d258d79a625411d7709fc | |
| parent | b63eb01f6936c623baf9ee7d0d8303b549d62c7d (diff) | |
| parent | 575f281e15518a427ed061abb61e4cc79751de37 (diff) | |
| download | perlweeklychallenge-club-7d4b749f794162198db90d8446eacffe9de9dd8e.tar.gz perlweeklychallenge-club-7d4b749f794162198db90d8446eacffe9de9dd8e.tar.bz2 perlweeklychallenge-club-7d4b749f794162198db90d8446eacffe9de9dd8e.zip | |
Merge pull request #7146 from fluca1978/PWC192
Pwc192
| -rw-r--r-- | challenge-192/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/postgresql/ch-1.plperl | 18 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/postgresql/ch-1.sql | 27 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/postgresql/ch-2.plperl | 58 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/postgresql/ch-2.sql | 12 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/raku/ch-1.p6 | 7 | ||||
| -rw-r--r-- | challenge-192/luca-ferrari/raku/ch-2.p6 | 34 |
12 files changed, 162 insertions, 0 deletions
diff --git a/challenge-192/luca-ferrari/blog-1.txt b/challenge-192/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..3ff8049249 --- /dev/null +++ b/challenge-192/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/22/PerlWeeklyChallenge192.html#task1 diff --git a/challenge-192/luca-ferrari/blog-2.txt b/challenge-192/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..fbb1835040 --- /dev/null +++ b/challenge-192/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/22/PerlWeeklyChallenge192.html#task2 diff --git a/challenge-192/luca-ferrari/blog-3.txt b/challenge-192/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..c053c3f4a8 --- /dev/null +++ b/challenge-192/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/22/PerlWeeklyChallenge192.html#task1plperl diff --git a/challenge-192/luca-ferrari/blog-4.txt b/challenge-192/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..ad6ba7c4c9 --- /dev/null +++ b/challenge-192/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/22/PerlWeeklyChallenge192.html#task2plperl diff --git a/challenge-192/luca-ferrari/blog-5.txt b/challenge-192/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..a8ae43c176 --- /dev/null +++ b/challenge-192/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/22/PerlWeeklyChallenge192.html#task1plpgsql diff --git a/challenge-192/luca-ferrari/blog-6.txt b/challenge-192/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..71514366ef --- /dev/null +++ b/challenge-192/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/22/PerlWeeklyChallenge192.html#task2plpgsql diff --git a/challenge-192/luca-ferrari/postgresql/ch-1.plperl b/challenge-192/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..0d235f8fcc --- /dev/null +++ b/challenge-192/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,18 @@ +-- Perl Weekly Challenge 192 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc192; + +CREATE OR REPLACE FUNCTION +pwc192.task1_plperl( int) +RETURNS int +AS $CODE$ + my ($n) = @_; + + + my @bits = map { $_ == 0 ? 1 : 0 } split( '', sprintf( "%b", $n ) ); + my $binary = join( '', @bits ); + my $flipped = eval( "0b$binary" ); + return $flipped; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-192/luca-ferrari/postgresql/ch-1.sql b/challenge-192/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..a13e6329ee --- /dev/null +++ b/challenge-192/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,27 @@ +-- Perl Weekly Challenge 192 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc192; + +CREATE OR REPLACE FUNCTION +pwc192.task1_plpgsql( n int ) +RETURNS int +AS $CODE$ +DECLARE + bb text; + b bit; +BEGIN + bb := '0'; -- needed for the conversion + FOREACH b IN ARRAY regexp_split_to_array( n::bit(8)::text, '' ) LOOP + IF b THEN + bb := bb || 0; + ELSE + bb := bb || 1; + END IF; + END LOOP; + + RAISE INFO '%', bb; + RETURN bb::bit(8)::int; +END +$CODE$ +LANGUAGE plpgsql; 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; diff --git a/challenge-192/luca-ferrari/postgresql/ch-2.sql b/challenge-192/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..efd3646ff8 --- /dev/null +++ b/challenge-192/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,12 @@ +-- Perl Weekly Challenge 192 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc192; + +CREATE OR REPLACE FUNCTION +pwc192.task2_plpgsql( n int[] ) +RETURNS int +AS $CODE$ + SELECT pwc192.task2_plperl( n ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-192/luca-ferrari/raku/ch-1.p6 b/challenge-192/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..cb7f1b6139 --- /dev/null +++ b/challenge-192/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,7 @@ +#!raku + +# Perl Weekly Challenge 192 + +sub MAIN( Int $n where { $n > 0 } ) { + $n.base(2).comb.map( { $_ == 0 ?? 1 !! 0 } ).join.parse-base(2).say; +} diff --git a/challenge-192/luca-ferrari/raku/ch-2.p6 b/challenge-192/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..44ddbb5c40 --- /dev/null +++ b/challenge-192/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,34 @@ +#!raku + +# Perl Weekly Challenge 192 + +sub MAIN( *@n is copy where { @n.elems == @n.grep( * ~~ Int ).elems } + , :$verbose = False ) { + my $elem = @n.sum / @n.elems; + '-1'.say and exit if ( $elem.Int !~~ $elem ); + + my @moves; + @moves.push: [@n]; + while ( @n.grep( * ~~ $elem ).elems != @n.elems ) { + for 0 ..^ @n.elems -> $index { + if ( @n[ $index ] == @n.max ) { + for 0 ..^ @n.elems -> $borrow { + next if $borrow == $index; + next if @n[ $borrow ] >= @n[ $index ]; + next if @n[ $borrow ] >= $elem; + @n[ $borrow ]++; + last; + } + + @n[ $index ]--; + } + + } + + @moves.push: [@n]; + } + + + @moves.join( "\n" ).say if $verbose; + @moves.elems.say; +} |
