diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-23 15:34:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-23 15:34:06 +0100 |
| commit | ddd97697aff504ef42bb561b9775d476fbb42d31 (patch) | |
| tree | 92dfddc3fdb3356800ec644b43b079e9405e5439 | |
| parent | 882ec347c56f7c9f566ceb156698a66fd9a05594 (diff) | |
| parent | a36715ce825652d70612ae50e4197792569ef3e9 (diff) | |
| download | perlweeklychallenge-club-ddd97697aff504ef42bb561b9775d476fbb42d31.tar.gz perlweeklychallenge-club-ddd97697aff504ef42bb561b9775d476fbb42d31.tar.bz2 perlweeklychallenge-club-ddd97697aff504ef42bb561b9775d476fbb42d31.zip | |
Merge pull request #10897 from fluca1978/PWC288
PWC 288
| -rw-r--r-- | challenge-288/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-288/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-288/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-288/luca-ferrari/pljava/pom.xml | 72 | ||||
| -rw-r--r-- | challenge-288/luca-ferrari/plperl/ch-1.plperl | 30 | ||||
| -rw-r--r-- | challenge-288/luca-ferrari/raku/ch-1.raku | 30 | ||||
| -rw-r--r-- | challenge-288/luca-ferrari/raku/ch-2.raku | 48 |
7 files changed, 111 insertions, 72 deletions
diff --git a/challenge-288/luca-ferrari/blog-1.txt b/challenge-288/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..78f66e6516 --- /dev/null +++ b/challenge-288/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/23/PerlWeeklyChallenge288.html#task1 diff --git a/challenge-288/luca-ferrari/blog-2.txt b/challenge-288/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..cfa3aba9e0 --- /dev/null +++ b/challenge-288/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/23/PerlWeeklyChallenge288.html#task2 diff --git a/challenge-288/luca-ferrari/blog-3.txt b/challenge-288/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..3de43b9c5e --- /dev/null +++ b/challenge-288/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/09/23/PerlWeeklyChallenge288.html#task1plperl diff --git a/challenge-288/luca-ferrari/pljava/pom.xml b/challenge-288/luca-ferrari/pljava/pom.xml deleted file mode 100644 index 47fa2f66a2..0000000000 --- a/challenge-288/luca-ferrari/pljava/pom.xml +++ /dev/null @@ -1,72 +0,0 @@ - -<project - xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > - <modelVersion>4.0.0</modelVersion> - - <groupId>PWC</groupId> - <artifactId> - PWC287 - </artifactId> - <version> - 1 - </version> - - <name>Perl Weekly Challenge 287 with package PWC287</name> - <description>Implementation of the tasks in PL/Java for PWC 287</description> - - <properties> - <project.build.sourceEncoding>US-ASCII</project.build.sourceEncoding> - </properties> - - <dependencies> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>pljava-api</artifactId> - <version>1.6.6</version> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.8.1</version> - <configuration> - <release>9</release> - </configuration> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-jar-plugin</artifactId> - <version>2.6</version> - <configuration> - <archive> - <manifest> - <!-- This identifies and version-stamps the jar. - Not essential, but easy and useful. --> - <addDefaultImplementationEntries> - true - </addDefaultImplementationEntries> - </manifest> - - <manifestSections> - <!-- This identifies a file in the jar named - pljava.ddr as an SQLJDeploymentDescriptor. --> - <manifestSection> - <name>pljava.ddr</name> - <manifestEntries> - <SQLJDeploymentDescriptor> - true - </SQLJDeploymentDescriptor> - </manifestEntries> - </manifestSection> - </manifestSections> - </archive> - </configuration> - </plugin> - </plugins> - </build> -</project> diff --git a/challenge-288/luca-ferrari/plperl/ch-1.plperl b/challenge-288/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..e6d115e169 --- /dev/null +++ b/challenge-288/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 288 +-- Task 1 +-- See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-288> +-- + +CREATE SCHEMA IF NOT EXISTS pwc288; + +CREATE OR REPLACE FUNCTION +pwc288.task1_plperl( int ) +RETURNS int +AS $CODE$ + + my ( $number ) = @_; + + my ( $left, $right ) = ( $number - 1, $number + 1 ); + + my $is_palindrome = sub { + return $_[ 0 ] == join '', reverse split //, $_[ 0 ]; + }; + + while ( ! $is_palindrome->( $left ) ) { $left--; } + while ( ! $is_palindrome->( $right ) ) { $right++; } + + return $number - $left < $right - $number + ? $left + : $right; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-288/luca-ferrari/raku/ch-1.raku b/challenge-288/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..4e309ccf7c --- /dev/null +++ b/challenge-288/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,30 @@ +#!raku + +# +# Perl Weekly Challenge 288 +# Task 1 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-288> +# + +sub MAIN( $number ) { + + # handling of special case: + # one digit only + if ( $number < 10 && $number >= 1 ) { + say $number - 1; + exit; + } + + my ( $left, $right ) = $number - 1, $number + 1; + while ( $right.Str.flip.Int != $right ) { + $right++; + } + + while ( $left.Str.flip.Int != $left ) { + $left--; + } + + # what is the one with the lowest difference? + say $right - $number < $number - $left ?? $right !! $left; +} diff --git a/challenge-288/luca-ferrari/raku/ch-2.raku b/challenge-288/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..4031508d7e --- /dev/null +++ b/challenge-288/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,48 @@ +#!raku + +# +# Perl Weekly Challenge 288 +# Task 2 +# +# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-288> +# + +sub mark-visited( @positions, $r, $c ) { + @positions.push: [ $r, $c ] if ( ! @positions.grep( { $_[ 0 ] == $r && $_[ 1 ] == $c } ) ); + +} + + +sub MAIN() { + my $matrix = [ + ['x', 'x', 'x', 'x', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'o', 'o', 'o', 'o'], + ['x', 'x', 'x', 'o', 'o'], + ]; + + # get all the information about every cell + # keyed by the cell content + my %cells = x => Array.new, o => Array.new; + + for 0 ..^ $matrix.elems -> $row { + for 0 ..^ $matrix[ $row ].elems -> $col { + my $id = $row ~ '-' ~ $col; + my $key = $matrix[ $row ][ $col ]; + mark-visited( %cells{ $key }, $row, $col ) if ( $matrix[ $row ][ $col ] eq $key ); + # adiacent nodes + my ( $nr, $nc ) = $row, $col; + $nr = $row + 1; + mark-visited( %cells{ $key }, $nr, $nc ) if ( $nr >= 0 && $nr < $matrix.elems && $matrix[ $nr ][ $nc ] eq $key ); + $nr = $row - 1; + mark-visited( %cells{ $key }, $nr, $nc ) if ( $nr >= 0 && $nr < $matrix.elems && $matrix[ $nr ][ $nc ] eq $key ); + $nr = $row; + $nc++; + mark-visited( %cells{ $key }, $nr, $nc ) if ( $nc >= 0 && $nc < $matrix[ 0 ].elems && $matrix[ $nr ][ $nc ] eq $key ); + $nc = $col - 1; + mark-visited( %cells{ $key }, $nr, $nc ) if ( $nc >= 0 && $nc < $matrix[ 0 ].elems && $matrix[ $nr ][ $nc ] eq $key ); + } + } + + "$_ = { %cells{ $_ }.flat.elems }".say for %cells.keys; +} |
