diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-25 22:04:59 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-25 22:04:59 +0000 |
| commit | a6b9539ff46f91005e248d20b91308e284f020d4 (patch) | |
| tree | ecbee67c16b4431d9ee083b9c7d3a5d3a20b9800 | |
| parent | 16008c950a7c2fac4cc659003f43cecea043ffa4 (diff) | |
| parent | 3d7add076cd7a1e0a1bd83e5cb6c6f6921bf899d (diff) | |
| download | perlweeklychallenge-club-a6b9539ff46f91005e248d20b91308e284f020d4.tar.gz perlweeklychallenge-club-a6b9539ff46f91005e248d20b91308e284f020d4.tar.bz2 perlweeklychallenge-club-a6b9539ff46f91005e248d20b91308e284f020d4.zip | |
Merge pull request #2848 from arnesom/branch-for-challenge-088
Arne Sommer 088
| -rw-r--r-- | challenge-088/arne-sommer/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-088/arne-sommer/example1.txt | 3 | ||||
| -rw-r--r-- | challenge-088/arne-sommer/example2.txt | 4 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/perl/array-of-product-perl | 15 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/perl/ch-1.pl | 15 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/perl/ch-2.pl | 32 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/perl/spiral-matrix-perl | 32 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/raku/array-of-product | 11 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/raku/ch-1.p6 | 11 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/raku/ch-2.p6 | 45 | ||||
| -rwxr-xr-x | challenge-088/arne-sommer/raku/spiral-matrix | 45 |
11 files changed, 214 insertions, 0 deletions
diff --git a/challenge-088/arne-sommer/blog.txt b/challenge-088/arne-sommer/blog.txt new file mode 100644 index 0000000000..28486e97a6 --- /dev/null +++ b/challenge-088/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/arrayed-spiral.html diff --git a/challenge-088/arne-sommer/example1.txt b/challenge-088/arne-sommer/example1.txt new file mode 100644 index 0000000000..32009c9873 --- /dev/null +++ b/challenge-088/arne-sommer/example1.txt @@ -0,0 +1,3 @@ +[ 1, 2, 3 ] +[ 4, 5, 6 ] +[ 7, 8, 9 ] diff --git a/challenge-088/arne-sommer/example2.txt b/challenge-088/arne-sommer/example2.txt new file mode 100644 index 0000000000..ebee37d5d1 --- /dev/null +++ b/challenge-088/arne-sommer/example2.txt @@ -0,0 +1,4 @@ +[ 1, 2, 3, 4 ] +[ 5, 6, 7, 8 ] +[ 9, 10, 11, 12 ] +[ 13, 14, 15, 16 ] diff --git a/challenge-088/arne-sommer/perl/array-of-product-perl b/challenge-088/arne-sommer/perl/array-of-product-perl new file mode 100755 index 0000000000..d9e7f0befc --- /dev/null +++ b/challenge-088/arne-sommer/perl/array-of-product-perl @@ -0,0 +1,15 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use List::Util qw/reduce all/; + +my @N = @ARGV; + +die '@N must contain positive integers only' unless all { $_ =~ qr/^[1-9]\d*$/ } @N; + +my $product = reduce { $a * $b } @N; + +my @M = map { $product / $_ } @N; + +say '(', join(', ', @M), ')'; diff --git a/challenge-088/arne-sommer/perl/ch-1.pl b/challenge-088/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..d9e7f0befc --- /dev/null +++ b/challenge-088/arne-sommer/perl/ch-1.pl @@ -0,0 +1,15 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use List::Util qw/reduce all/; + +my @N = @ARGV; + +die '@N must contain positive integers only' unless all { $_ =~ qr/^[1-9]\d*$/ } @N; + +my $product = reduce { $a * $b } @N; + +my @M = map { $product / $_ } @N; + +say '(', join(', ', @M), ')'; diff --git a/challenge-088/arne-sommer/perl/ch-2.pl b/challenge-088/arne-sommer/perl/ch-2.pl new file mode 100755 index 0000000000..c070a3abdd --- /dev/null +++ b/challenge-088/arne-sommer/perl/ch-2.pl @@ -0,0 +1,32 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use Math::Matrix; +use File::Slurp; + +die "Specify a matrix file" unless $ARGV[0]; + +my @rows; + +for my $line (read_file($ARGV[0], chomp => 1)) +{ + $line =~ /\[ +(.*) \]/; + my @values = split(",? +", $1); + push(@rows, \@values); +} + +my $m = Math::Matrix->new(@rows); + +my @spiral; + +while ($m->nrow()) +{ + my $row = $m->getrow(0); + + push(@spiral, @{@{$row}[0]}); + $m = $m->delrow(0); + $m = $m->rot90(); +} + +say '[ ', join(', ', @spiral), ' ]'; diff --git a/challenge-088/arne-sommer/perl/spiral-matrix-perl b/challenge-088/arne-sommer/perl/spiral-matrix-perl new file mode 100755 index 0000000000..c070a3abdd --- /dev/null +++ b/challenge-088/arne-sommer/perl/spiral-matrix-perl @@ -0,0 +1,32 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use Math::Matrix; +use File::Slurp; + +die "Specify a matrix file" unless $ARGV[0]; + +my @rows; + +for my $line (read_file($ARGV[0], chomp => 1)) +{ + $line =~ /\[ +(.*) \]/; + my @values = split(",? +", $1); + push(@rows, \@values); +} + +my $m = Math::Matrix->new(@rows); + +my @spiral; + +while ($m->nrow()) +{ + my $row = $m->getrow(0); + + push(@spiral, @{@{$row}[0]}); + $m = $m->delrow(0); + $m = $m->rot90(); +} + +say '[ ', join(', ', @spiral), ' ]'; diff --git a/challenge-088/arne-sommer/raku/array-of-product b/challenge-088/arne-sommer/raku/array-of-product new file mode 100755 index 0000000000..440db9612b --- /dev/null +++ b/challenge-088/arne-sommer/raku/array-of-product @@ -0,0 +1,11 @@ +#! /usr/bin/env raku + +subset PositiveInt of Int where * > 0; + +unit sub MAIN (*@N where @N.elems && all(@N) ~~ PositiveInt); + +my $product = [*] @N; + +my @M = @N.map( { $product / $_ }); + +say '(', @M.join(', '), ')'; diff --git a/challenge-088/arne-sommer/raku/ch-1.p6 b/challenge-088/arne-sommer/raku/ch-1.p6 new file mode 100755 index 0000000000..440db9612b --- /dev/null +++ b/challenge-088/arne-sommer/raku/ch-1.p6 @@ -0,0 +1,11 @@ +#! /usr/bin/env raku + +subset PositiveInt of Int where * > 0; + +unit sub MAIN (*@N where @N.elems && all(@N) ~~ PositiveInt); + +my $product = [*] @N; + +my @M = @N.map( { $product / $_ }); + +say '(', @M.join(', '), ')'; diff --git a/challenge-088/arne-sommer/raku/ch-2.p6 b/challenge-088/arne-sommer/raku/ch-2.p6 new file mode 100755 index 0000000000..7a0c454d11 --- /dev/null +++ b/challenge-088/arne-sommer/raku/ch-2.p6 @@ -0,0 +1,45 @@ +#! /usr/bin/env raku + +# subset PositiveInt of Int where * > 0; + +unit sub MAIN ($matrix where $matrix.IO.f && $matrix.IO.r, :v(:$verbose)); + +my @matrix = $matrix.IO.lines>>.words>>.map( * ~~ (/<[1..9]>\d*/) )>>.grep( *.defined )>>.Array; + +my $rows = @matrix.elems; +my $cols = @matrix[0].elems; + +die "All rows must have the same length" unless [==] @(@matrix)>>.elems; + +my $row = 0; +my $col = 0; + +my $direction = "E"; + +my @spiral; + +say ": { @matrix[$row][$col] } Direction: $direction" if $verbose; + +loop +{ + @spiral.push: @matrix[$row][$col]; + + @matrix[$row][$col] = Any; + + if $direction eq "E" && @matrix[$row][$col+1] { $col++; $direction = "E"; } + elsif $direction eq "E" && @matrix[$row+1][$col] { $row++; $direction = "S"; } + elsif $direction eq "S" && @matrix[$row+1][$col] { $row++; $direction = "S"; } + elsif $direction eq "S" && @matrix[$row][$col-1] { $col--; $direction = "W"; } + elsif $direction eq "W" && @matrix[$row][$col-1] { $col--; $direction = "W"; } + elsif $direction eq "W" && @matrix[$row-1][$col] { $row--; $direction = "N"; } + elsif $direction eq "N" && @matrix[$row-1][$col] { $row--; $direction = "N"; } + elsif $direction eq "N" && @matrix[$row][$col+1] { $col++; $direction = "E"; } + else + { + last; + } + say ": { @matrix[$row][$col] } Direction: $direction" if $verbose; +} + +say "[ { @spiral.join(', ') } ]"; + diff --git a/challenge-088/arne-sommer/raku/spiral-matrix b/challenge-088/arne-sommer/raku/spiral-matrix new file mode 100755 index 0000000000..7a0c454d11 --- /dev/null +++ b/challenge-088/arne-sommer/raku/spiral-matrix @@ -0,0 +1,45 @@ +#! /usr/bin/env raku + +# subset PositiveInt of Int where * > 0; + +unit sub MAIN ($matrix where $matrix.IO.f && $matrix.IO.r, :v(:$verbose)); + +my @matrix = $matrix.IO.lines>>.words>>.map( * ~~ (/<[1..9]>\d*/) )>>.grep( *.defined )>>.Array; + +my $rows = @matrix.elems; +my $cols = @matrix[0].elems; + +die "All rows must have the same length" unless [==] @(@matrix)>>.elems; + +my $row = 0; +my $col = 0; + +my $direction = "E"; + +my @spiral; + +say ": { @matrix[$row][$col] } Direction: $direction" if $verbose; + +loop +{ + @spiral.push: @matrix[$row][$col]; + + @matrix[$row][$col] = Any; + + if $direction eq "E" && @matrix[$row][$col+1] { $col++; $direction = "E"; } + elsif $direction eq "E" && @matrix[$row+1][$col] { $row++; $direction = "S"; } + elsif $direction eq "S" && @matrix[$row+1][$col] { $row++; $direction = "S"; } + elsif $direction eq "S" && @matrix[$row][$col-1] { $col--; $direction = "W"; } + elsif $direction eq "W" && @matrix[$row][$col-1] { $col--; $direction = "W"; } + elsif $direction eq "W" && @matrix[$row-1][$col] { $row--; $direction = "N"; } + elsif $direction eq "N" && @matrix[$row-1][$col] { $row--; $direction = "N"; } + elsif $direction eq "N" && @matrix[$row][$col+1] { $col++; $direction = "E"; } + else + { + last; + } + say ": { @matrix[$row][$col] } Direction: $direction" if $verbose; +} + +say "[ { @spiral.join(', ') } ]"; + |
