diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-27 13:01:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-27 13:01:31 +0000 |
| commit | 574136ce7a06629883d75b8fd43460041835abf7 (patch) | |
| tree | 705a01666434e703c5a9c58693130628db2f88d8 | |
| parent | 433e80fd58fce354052c32ea6e4308a8bac69719 (diff) | |
| parent | 8e0af9c25bb164f276229b9288f762b23c6dbdbb (diff) | |
| download | perlweeklychallenge-club-574136ce7a06629883d75b8fd43460041835abf7.tar.gz perlweeklychallenge-club-574136ce7a06629883d75b8fd43460041835abf7.tar.bz2 perlweeklychallenge-club-574136ce7a06629883d75b8fd43460041835abf7.zip | |
Merge pull request #7317 from fluca1978/PWC197
Pwc197
| -rw-r--r-- | challenge-197/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/blog-5.txt | 1 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/blog-6.txt | 1 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/postgresql/ch-1.plperl | 23 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/postgresql/ch-1.sql | 32 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/postgresql/ch-2.plperl | 33 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/postgresql/ch-2.sql | 49 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/raku/ch-1.p6 | 9 | ||||
| -rw-r--r-- | challenge-197/luca-ferrari/raku/ch-2.p6 | 38 |
12 files changed, 190 insertions, 0 deletions
diff --git a/challenge-197/luca-ferrari/blog-1.txt b/challenge-197/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..e77d7d7e28 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task1 diff --git a/challenge-197/luca-ferrari/blog-2.txt b/challenge-197/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e736905357 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task2 diff --git a/challenge-197/luca-ferrari/blog-3.txt b/challenge-197/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..70e98fc193 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task1plperl diff --git a/challenge-197/luca-ferrari/blog-4.txt b/challenge-197/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..84f58849b0 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task2plperl diff --git a/challenge-197/luca-ferrari/blog-5.txt b/challenge-197/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..d6fdc85651 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task1plpgsql diff --git a/challenge-197/luca-ferrari/blog-6.txt b/challenge-197/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..9f4359a5a3 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task2plpgsql diff --git a/challenge-197/luca-ferrari/postgresql/ch-1.plperl b/challenge-197/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..b1c1ac7c4c --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,23 @@ +-- Perl Weekly Challenge 197 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +/* +estdb=> select pwc197.task1_plperl( array[1,2,3,0,4,5,0,9,0,10]::int[] ); + task1_plperl +------------------------ + {1,2,3,4,5,9,10,0,0,0} + +*/ + +CREATE OR REPLACE FUNCTION +pwc197.task1_plperl( int[] ) +RETURNS int[] +AS $CODE$ +my ( $list ) = @_; +my @sorted = ( grep( { $_ != 0 } $list->@* ), + grep( { $_ == 0 } $list->@* ) ); +return [ @sorted ]; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-197/luca-ferrari/postgresql/ch-1.sql b/challenge-197/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..e96464bc11 --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,32 @@ +-- Perl Weekly Challenge 197 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +CREATE OR REPLACE FUNCTION +pwc197.task1_plpgsql( l int[] ) +RETURNS int[] +AS $CODE$ +DECLARE + i int; + v int[]; + zeros int := 0; +BEGIN + FOREACH i IN ARRAY l LOOP + IF i = 0 THEN + zeros := zeros + 1; + CONTINUE; + END IF; + + v := v || i; + END LOOP; + + WHILE zeros > 0 LOOP + v := v || 0; + zeros := zeros - 1; + END LOOP; + + RETURN v; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-197/luca-ferrari/postgresql/ch-2.plperl b/challenge-197/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..6da49c355b --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,33 @@ +-- Perl Weekly Challenge 197 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +CREATE OR REPLACE FUNCTION +pwc197.task2_plperl( int[] ) +RETURNS int[] +AS $CODE$ +my ( $array ) = @_; +my $sorted = [ $array->@* ]; +my $need_swap = 1; + +while ( $need_swap ) { + $need_swap = 0; + for my $i ( 0 .. $sorted->@* - 1 ) { + my $need_swap = ( ( $i % 2 == 0 ) && ( $sorted->[ $i ] >= $sorted->[ $i + 1 ] ) ) + || ( ( $i % 2 != 0 ) && ( $sorted->[ $i ] <= $sorted->[ $i + 1 ] ) ); + + if ( $need_swap ) { + my $temp = $sorted->[ $i ]; + $sorted->[ $i ] = $sorted->[ $i + 1 ]; + $sorted->[ $i + 1 ] = $temp; + } + + + } +} + +return $sorted; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-197/luca-ferrari/postgresql/ch-2.sql b/challenge-197/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..b68f56beea --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,49 @@ +-- Perl Weekly Challenge 197 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +CREATE OR REPLACE FUNCTION +pwc197.task2_plpgsql( l int[] ) +RETURNS int[] +AS $CODE$ +DECLARE + i int; + v int[]; + need_change bool := false; + t int; +BEGIN + need_change := true; + v := l; + raise info 'array %', v; + + WHILE need_change LOOP + need_change := false; + FOR i IN 0 .. array_length( v, 1 ) - 1 LOOP + + IF i % 2 = 0 THEN + IF v[ i ] <= v[ i + 1 ] THEN + need_change := true; + END IF; + ELSE + IF v[i] >= v[ i + 1 ] THEN + need_change := true; + END IF; + END IF; + + + IF need_change THEN + t := v[i]; + v[i] := v[i + 1]; + v[ i + 1 ]:= t; + END IF; + END LOOP; + + END LOOP; + + + + RETURN v; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-197/luca-ferrari/raku/ch-1.p6 b/challenge-197/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..2bf7eb3e3c --- /dev/null +++ b/challenge-197/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,9 @@ +#!raku + +# Perl Weekly Challenge 197 + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my ( @swapped ); + @swapped = | @list.grep( * !~~ 0 ), | @list.grep( * ~~ 0 ); + @swapped.join( ',' ).say; +} diff --git a/challenge-197/luca-ferrari/raku/ch-2.p6 b/challenge-197/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..050a1d9c1a --- /dev/null +++ b/challenge-197/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,38 @@ +#!raku + +# Perl Weekly Challenge 197 + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my @sorted = @list; + my $done = False; + + # list[0] < list[1] > list[2] < list[3]…. + while ( ! $done ) { + $done = True; + for 0 ..^ @sorted.elems - 1 -> $i { + if ( $i %% 2 ) { + if ( @sorted[ $i ] >= @sorted[ $i + 1 ] ) { + # need to change + my $temp = @sorted[ $i ]; + @sorted[ $i ] = @sorted[ $i + 1 ]; + @sorted[ $i + 1 ] = $temp; + $done = False; + } + } + else { + if ( @sorted[ $i ] <= @sorted[ $i + 1 ] ) { + # need to change + my $temp = @sorted[ $i ]; + @sorted[ $i ] = @sorted[ $i + 1 ]; + @sorted[ $i + 1 ] = $temp; + $done = False; + + } + } + + } + } + + @list.join( ',' ).say; + @sorted.join(',').say; +} |
