diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-29 19:18:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-29 19:18:34 +0000 |
| commit | 1aabcc4655c15ece13958fcc180f00ec865466e7 (patch) | |
| tree | 0fece153ea588387261ee1ce508e0ce7b62a2c28 | |
| parent | 4094f8b83c97a98d6e1be61cc3d019e9c565749b (diff) | |
| parent | 5e9eb6d6edff19d83c8189d2378c7626130849b3 (diff) | |
| download | perlweeklychallenge-club-1aabcc4655c15ece13958fcc180f00ec865466e7.tar.gz perlweeklychallenge-club-1aabcc4655c15ece13958fcc180f00ec865466e7.tar.bz2 perlweeklychallenge-club-1aabcc4655c15ece13958fcc180f00ec865466e7.zip | |
Merge pull request #2873 from jaldhar/challenge-088
Challenge 88 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-088/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-088/jaldhar-h-vyas/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-088/jaldhar-h-vyas/perl/ch-2.pl | 60 | ||||
| -rwxr-xr-x | challenge-088/jaldhar-h-vyas/raku/ch-1.sh | 3 | ||||
| -rwxr-xr-x | challenge-088/jaldhar-h-vyas/raku/ch-2.p6 | 47 | ||||
| -rw-r--r-- | challenge-088/jaldhar-h-vyas/spiral.txt | 4 | ||||
| -rw-r--r-- | challenge-088/jaldhar-h-vyas/spiral2.txt | 7 |
7 files changed, 136 insertions, 0 deletions
diff --git a/challenge-088/jaldhar-h-vyas/blog.txt b/challenge-088/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..2996ba2358 --- /dev/null +++ b/challenge-088/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2020/11/perl_weekly_challenge_week_88.html diff --git a/challenge-088/jaldhar-h-vyas/perl/ch-1.pl b/challenge-088/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..6ddfee9979 --- /dev/null +++ b/challenge-088/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +my @N = @ARGV; + +my $sum = 1; +for my $e (@N) { + $sum *= $e; +} + +my @M = map { $sum / $N[$_] } 0 .. scalar @N - 1; + +say join q{, }, @M; diff --git a/challenge-088/jaldhar-h-vyas/perl/ch-2.pl b/challenge-088/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..f6ce52b243 --- /dev/null +++ b/challenge-088/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; + +sub usage { + print<<"-USAGE-"; +Usage: + $PROGRAM_NAME <file> + + <file> a file describing a matrix of numbers where every line is a row in the matrix. +-USAGE- + exit 0; +} + +my $file = shift // usage(); + +my @matrix; +open my $fn, '<', $file or die "$OS_ERROR\n"; +while (my $line = <$fn>) { + chomp $line; + push @matrix, [ $line =~ /(\d+)/g ]; +} +close $fn; + +my @spiral; +my $top = 0; +my $right = scalar @{$matrix[0]} - 1; +my $bottom = scalar @matrix - 1; +my $left = 0; + +while ($top < scalar @matrix / 2) { + if ($top == $bottom) { + push @spiral, @{$matrix[$top]}[$left]; + } else { + + for my $i ($left .. $right) { + push @spiral, @{$matrix[$top]}[$i]; + } + + for my $i ($top + 1 .. $bottom - 1) { + push @spiral, @{$matrix[$i]}[$right]; + } + + for my $i (reverse ($left .. $right)) { + push @spiral, @{$matrix[$bottom]}[$i]; + } + + for my $i (reverse ($top + 1 .. $bottom - 1)) { + push @spiral, @{$matrix[$i]}[$left]; + } + } + + $top++; + $right--; + $bottom--; + $left++; +} + +say join q{, }, @spiral;
\ No newline at end of file diff --git a/challenge-088/jaldhar-h-vyas/raku/ch-1.sh b/challenge-088/jaldhar-h-vyas/raku/ch-1.sh new file mode 100755 index 0000000000..aa597282d7 --- /dev/null +++ b/challenge-088/jaldhar-h-vyas/raku/ch-1.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +perl6 -e 'my $sum = [*] @*ARGS; (0 ..^ @*ARGS.elems).map({ $sum / @*ARGS[$_]; }).join(q{, }).say;' $@
\ No newline at end of file diff --git a/challenge-088/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-088/jaldhar-h-vyas/raku/ch-2.p6 new file mode 100755 index 0000000000..567336c543 --- /dev/null +++ b/challenge-088/jaldhar-h-vyas/raku/ch-2.p6 @@ -0,0 +1,47 @@ +#!/usr/bin/perl6 + +sub MAIN( + Str $file #= a file describing a matrix of numbers where every line + #= is a row in the matrix. +) { + my @matrix; + for $file.IO.lines -> $line { + @matrix.push($line.match(/ (\d+) /, :g)); + } + + my @spiral; + my $top = 0; + my $right = @matrix[0].elems - 1; + my $bottom = @matrix.elems - 1; + my $left = 0; + + while $top < @matrix.elems / 2 { + if $top == $bottom { + push @spiral, @matrix[$top][$left]; + } else { + + for $left .. $right -> $i { + push @spiral, @matrix[$top][$i]; + } + + for $top ^..^ $bottom -> $i { + push @spiral, @matrix[$i][$right]; + } + + for reverse $left .. $right -> $i { + push @spiral, @matrix[$bottom][$i]; + } + + for reverse $top ^..^ $bottom -> $i { + push @spiral, @matrix[$i][$left]; + } + } + + $top++; + $right--; + $bottom--; + $left++; + } + + @spiral.join(q{, }).say; +}
\ No newline at end of file diff --git a/challenge-088/jaldhar-h-vyas/spiral.txt b/challenge-088/jaldhar-h-vyas/spiral.txt new file mode 100644 index 0000000000..ebee37d5d1 --- /dev/null +++ b/challenge-088/jaldhar-h-vyas/spiral.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/jaldhar-h-vyas/spiral2.txt b/challenge-088/jaldhar-h-vyas/spiral2.txt new file mode 100644 index 0000000000..35f82f3319 --- /dev/null +++ b/challenge-088/jaldhar-h-vyas/spiral2.txt @@ -0,0 +1,7 @@ +[ 1, 2, 3, 4, 5, 6, 7 ] +[ 8, 9, 10, 11, 12, 13, 14 ] +[ 15, 16, 17, 18, 19, 20, 21 ] +[ 22, 23, 24, 25, 26, 27, 28 ] +[ 29, 30, 31, 32, 33, 34, 35 ] +[ 36, 37, 38, 39, 40, 41, 42 ] +[ 43, 44, 45, 46, 47, 48, 49 ]
\ No newline at end of file |
