diff options
| -rw-r--r-- | challenge-068/paulo-custodio/Makefile | 3 | ||||
| -rw-r--r-- | challenge-068/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-068/paulo-custodio/perl/ch-1.pl | 77 | ||||
| -rw-r--r-- | challenge-068/paulo-custodio/perl/ch-2.pl | 39 |
4 files changed, 120 insertions, 0 deletions
diff --git a/challenge-068/paulo-custodio/Makefile b/challenge-068/paulo-custodio/Makefile new file mode 100644 index 0000000000..ba9a2bf399 --- /dev/null +++ b/challenge-068/paulo-custodio/Makefile @@ -0,0 +1,3 @@ +all: + perl perl/ch-1.pl + perl perl/ch-2.pl diff --git a/challenge-068/paulo-custodio/README b/challenge-068/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-068/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-068/paulo-custodio/perl/ch-1.pl b/challenge-068/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..e7279cec76 --- /dev/null +++ b/challenge-068/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl + +# Challenge 068 +# +# TASK #1 › Zero Matrix +# Submitted by: Mohammad S Anwar +# You are given a matrix of size M x N having only 0s and 1s. +# +# Write a script to set the entire row and column to 0 if an element is 0. +# +# Example 1 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 1, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [1, 0, 1] +# Example 2 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 0, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [0, 0, 0] + +use Modern::Perl; +use Clone 'clone'; +use Test::More; + +is_deeply [zero_matrix([1,0,1], + [1,1,1], + [1,1,1])], + [[0,0,0], + [1,0,1], + [1,0,1]]; + +is_deeply [zero_matrix([1,0,1], + [1,1,1], + [1,0,1])], + [[0,0,0], + [1,0,1], + [0,0,0]]; + +done_testing; + + +sub zero_matrix { + my(@m) = @_; + my $orig = clone(\@m); + for my $r (0 .. @m-1) { + for my $c (0 .. @{$m[0]}-1) { + if (!$orig->[$r][$c]) { + @m = zero_row($r, @m); + @m = zero_col($c, @m); + } + } + } + return @m; +} + +sub zero_row { + my($r, @m) = @_; + for my $c (0 .. @{$m[0]}-1) { + $m[$r][$c] = 0; + } + return @m; +} + +sub zero_col { + my($c, @m) = @_; + for my $r (0 .. @m-1) { + $m[$r][$c] = 0; + } + return @m; +} diff --git a/challenge-068/paulo-custodio/perl/ch-2.pl b/challenge-068/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b4730ddce8 --- /dev/null +++ b/challenge-068/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# Challenge 068 +# +# TASK #2 › Reorder List +# Submitted by: Mohammad S Anwar +# You are given a singly linked list $L as below: +# +# L0 ? L1 ? … ? Ln-1 ? Ln +# Write a script to reorder list as below: +# +# L0 ? Ln ? L1 ? Ln-1 ? L2 ? Ln-2 ? +# You are ONLY allowed to do this in-place without altering the nodes’ values. +# +# Example +# Input: 1 ? 2 ? 3 ? 4 +# Output: 1 ? 4 ? 2 ? 3 + +use Modern::Perl; +use Test::More; + +is_deeply reorder_list([1,[2,[3,[4]]]]), [1,[4,[2,[3]]]]; +done_testing; + +sub reorder_list { + my($l) = @_; + # get second element + my $tail = $l->[1]; + # get and remove last element + my $p = $tail; + my $last; + while ($p->[1]) { + $last = $p; + $p = $p->[1]; + } + my $eln = pop(@{$last}); + + return [$l->[0], [$eln->[0], $tail]]; +} |
