Skip to content

Solutions for challenge #68 #1918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions challenge-068/roger-bell-west/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 30 additions & 0 deletions challenge-068/roger-bell-west/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -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;
}