diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-05-26 14:36:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-26 14:36:05 +0100 |
| commit | 9bc96eae448f61d5bf1eb155fa42b1a97c9ab8dc (patch) | |
| tree | b088fef5c5330d128f5dcc2746dd393f724efe8d | |
| parent | ed328f106502e9fe15fc8a525e42c4a7dea98130 (diff) | |
| parent | 94730aac3d332b4f3451bc454c72a3302e89b241 (diff) | |
| download | perlweeklychallenge-club-9bc96eae448f61d5bf1eb155fa42b1a97c9ab8dc.tar.gz perlweeklychallenge-club-9bc96eae448f61d5bf1eb155fa42b1a97c9ab8dc.tar.bz2 perlweeklychallenge-club-9bc96eae448f61d5bf1eb155fa42b1a97c9ab8dc.zip | |
Merge pull request #1765 from fluca1978/pwc62
Pwc62
| -rw-r--r-- | challenge-062/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-062/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-062/luca-ferrari/raku/ch-1.p6 | 41 | ||||
| -rw-r--r-- | challenge-062/luca-ferrari/raku/ch-2.p6 | 67 |
4 files changed, 110 insertions, 0 deletions
diff --git a/challenge-062/luca-ferrari/blog-1.txt b/challenge-062/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..cd8d470969 --- /dev/null +++ b/challenge-062/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/05/26/PerlWeeklyChallenge62.html#task1 diff --git a/challenge-062/luca-ferrari/blog-2.txt b/challenge-062/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..723a84b3c2 --- /dev/null +++ b/challenge-062/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/05/26/PerlWeeklyChallenge62.html#task2 diff --git a/challenge-062/luca-ferrari/raku/ch-1.p6 b/challenge-062/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..b6468f1988 --- /dev/null +++ b/challenge-062/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,41 @@ +#!env raku + +# Write a script that takes a list of email addresses (one per line) +# and sorts them first by the domain part of the email address, +# and then by the part to the left of the @ (known as the mailbox). +# +# Note that the domain is case-insensitive, while the mailbox part is case sensitive. +# (Some email providers choose to ignore case, but that’s another matter entirely.) +# +# If your script is invoked with arguments, it should treat them as file names +# and read them in order, otherwise your script should read email addresses from standard input. +# +# Bonus +# +# Add a -u option which only includes unique email addresses in the output, just like sort -u. + + + +sub MAIN( Bool :$u? = False, *@emails ) { + if ( ! @emails ) { + @emails = 'name@example.org', + 'rjt@cpan.org', + 'Name@example.org', + 'rjt@CPAN.org', + 'user@alpha.example.org'; + } + + + @emails = @emails.sort( *.split( '@' )[ 0 ] ).sort( *.split( '@' )[ 1 ].lc ); + + say @emails.join( "\n" ) if ! $u; + if $u { + my @unique-emails; + + for @emails -> $email { + @unique-emails.push: $email.lc if ( ! @unique-emails.grep( $email.lc ) ); + } + + say @unique-emails.join( "\n" ); + } +} diff --git a/challenge-062/luca-ferrari/raku/ch-2.p6 b/challenge-062/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..c56a20c4ff --- /dev/null +++ b/challenge-062/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,67 @@ +#!env raku + +# Task 2 +# N-queens + +# Places the queen into the chessboard at the indicated coordinates. +# Also setto False every square the queen can reach. +sub place-queen( @chessboard, $row, $column, $height, $dimension ){ + + for 0 ..^ $dimension { + @chessboard[ $row ][ $_ ][ $height ] = False; + @chessboard[ $row ][ $column ][ $_ ] = False; + @chessboard[ $_ ][ $column ][ $height ] = False; + } + + # diagonal (only on one level) + for 0 ..^ $dimension { + @chessboard[ $row + $_ ][ $column + $_ ][ $height ] = False if ( $row + $_ < $dimension && $column + $_ < $dimension); + @chessboard[ $row - $_ ][ $column - $_ ][ $height ] = False if ( $row - $_ >= 0 && $column - $_ >= 0 ); + @chessboard[ $row - $_ ][ $column + $_ ][ $height ] = False if ( $row - $_ >= 0 && $column + $_ < $dimension ); + @chessboard[ $row + $_ ][ $column - $_ ][ $height ] = False if ( $row + $_ < $dimension && $column - $_ >= 0 ); + } + + @chessboard[ $row ][ $column ][ $height ] = 'QUEEN'; +} + + +# +# Show every level of the chessboard. +# +sub show-chessboard( @chessboard, $dimension ) { + for 0 ..^ $dimension -> $height { + say "Layer $height"; + + for 0 ..^ $dimension -> $row { + for 0 ..^ $dimension -> $column { + given @chessboard[ $row ][ $column ][ $height ] { + when Str { print "\t ", @chessboard[ $row ][ $column ][ $height ]; } + default { print "\t x "; } + } + + } + + print "\n"; + } + } +} + +sub MAIN( Int $dimension = 3 ){ + my @chessboard = [[True xx $dimension] xx $dimension] xx $dimension; + + for 0 ..^ $dimension -> $height { + for 0 ..^ $dimension -> $row { + for 0 ..^ $dimension -> $column { + + # is the cell available? + next if ! @chessboard[ $row ][ $column ][ $height ]; + + # # place the queen + place-queen( @chessboard, $row, $column, $height, $dimension ); + } + } + } + + + show-chessboard( @chessboard, $dimension ); +} |
