diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-07-07 13:40:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-07 13:40:23 +0100 |
| commit | 06f903272758a80fbfc02a988ab43019836372cd (patch) | |
| tree | 2e51eda77c9616111c3962533710b2d66e42e55c | |
| parent | 42c93bf044cbb16c9e1e904aadc54efbfc2ad067 (diff) | |
| parent | 5aaafa531c9c837996f4d8613d59f19c03305f21 (diff) | |
| download | perlweeklychallenge-club-06f903272758a80fbfc02a988ab43019836372cd.tar.gz perlweeklychallenge-club-06f903272758a80fbfc02a988ab43019836372cd.tar.bz2 perlweeklychallenge-club-06f903272758a80fbfc02a988ab43019836372cd.zip | |
Merge pull request #1918 from Firedrake/rogerbw-challenge-068
Solutions for challenge #68
| -rw-r--r-- | challenge-068/roger-bell-west/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-068/roger-bell-west/perl/ch-2.pl | 30 |
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-068/roger-bell-west/perl/ch-1.pl b/challenge-068/roger-bell-west/perl/ch-1.pl new file mode 100644 index 0000000000..3c6e4166c1 --- /dev/null +++ b/challenge-068/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,42 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is_deeply(zm([[1,0,1],[1,1,1],[1,1,1]]), + [[0,0,0],[1,0,1],[1,0,1]], + 'example 1', + ); +is_deeply(zm([[1,0,1],[1,1,1],[1,0,1]]), + [[0,0,0],[1,0,1],[0,0,0]], + 'example 2', + ); + +sub zm { + my $in=shift; + my $a=scalar @{$in}-1; + my $b=scalar @{$in->[0]}-1; + my %seta; + my %setb; + foreach my $ai (0..$a) { + foreach my $bi (0..$b) { + if ($in->[$ai][$bi]==0) { + $seta{$ai}=1; + $setb{$bi}=1; + } + } + } + foreach my $aa (keys %seta) { + foreach my $bi (0..$b) { + $in->[$aa][$bi]=0; + } + } + foreach my $bb (keys %setb) { + foreach my $ai (0..$a) { + $in->[$ai][$bb]=0; + } + } + return $in; +} diff --git a/challenge-068/roger-bell-west/perl/ch-2.pl b/challenge-068/roger-bell-west/perl/ch-2.pl new file mode 100644 index 0000000000..dbf7eec8c6 --- /dev/null +++ b/challenge-068/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,30 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is_deeply(rl([1,2,3,4]), + [1,4,2,3], + 'example 1', + ); +is_deeply(rl([1,2,3,4,5]), + [1,5,2,4,3], + 'example 2', + ); + +sub rl { + my $list=shift; + my $n=scalar @{$list}; + my $nx=$n-1; + my @i; + foreach my $ni (0..int($nx/2)) { + push @i,$ni,$nx-$ni; + } + if ($i[-1] == $i[-2]) { + pop @i; + } + @{$list}=@{$list}[@i]; + return $list; +} |
