diff options
| -rw-r--r-- | challenge-068/walt-mankowski/perl/Node.pm | 30 | ||||
| -rw-r--r-- | challenge-068/walt-mankowski/perl/ch-1.pl | 83 | ||||
| -rw-r--r-- | challenge-068/walt-mankowski/perl/ch-2.pl | 68 |
3 files changed, 181 insertions, 0 deletions
diff --git a/challenge-068/walt-mankowski/perl/Node.pm b/challenge-068/walt-mankowski/perl/Node.pm new file mode 100644 index 0000000000..7f749b5c15 --- /dev/null +++ b/challenge-068/walt-mankowski/perl/Node.pm @@ -0,0 +1,30 @@ +package Node; + +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +sub new($package, $val) { + my $self = {}; + bless $self, $package; + + $self->{val} = $val; + $self->{next} = undef; + + return $self; +} + +sub set_next($self, $node) { + $self->{next} = $node; +} + +sub next($self) { + return $self->{next}; +} + +sub val($self) { + return $self->{val}; +} + +1; diff --git a/challenge-068/walt-mankowski/perl/ch-1.pl b/challenge-068/walt-mankowski/perl/ch-1.pl new file mode 100644 index 0000000000..9915a5cd52 --- /dev/null +++ b/challenge-068/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# 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] + +my @m1 = ([1,0,1], + [1,1,1], + [1,1,1]); + +my @m2 = ([1,0,1], + [1,1,1], + [1,0,1]); + +print_mat(\@m1); +print_mat(\@m2); + +zero_mat(\@m1); +zero_mat(\@m2); + +print_mat(\@m1); +print_mat(\@m2); + +sub print_mat($ar) { + for my $row ($ar->@*) { + printf "[%s]\n", join(", ", $row->@*); + } + print "\n"; +} + +sub zero_mat($ar) { + my @row_flag; + my @col_flag; + + # make a first pass to find where the 0s are + for my $r (0..$#$ar) { + my $row = $ar->[$r]; + for my $c (0..$#$row) { + if ($ar->[$r][$c] == 0) { + $row_flag[$r] = $col_flag[$c] = 1; + } else { + $row_flag[$r] //= 0; + $col_flag[$c] //= 0; + } + } + } + + # now set the cells to 0 if there was a 0 in its row or column + for my $r (0..$#$ar) { + my $row = $ar->[$r]; + for my $c (0..$#$row) { + if ($row_flag[$r] || $col_flag[$c]) { + $ar->[$r][$c] = 0; + } + } + } +} diff --git a/challenge-068/walt-mankowski/perl/ch-2.pl b/challenge-068/walt-mankowski/perl/ch-2.pl new file mode 100644 index 0000000000..d67e2f2426 --- /dev/null +++ b/challenge-068/walt-mankowski/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); +use lib '.'; +use Node; + +# 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 + +my $list = make_list(1..10); +print_list($list); +reorder_list($list); +print_list($list); + +sub make_list(@a) { + my $list = Node->new(shift @a); + my $cur = $list; + for my $val (@a) { + my $node = Node->new($val); + $cur->set_next($node); + $cur = $node; + } + return $list; +} + +sub print_list($list) { + while (defined($list)) { + print $list->val; + if (defined($list->next)) { + print " => "; + } + $list = $list->next; + } + print "\n"; +} + +sub reorder_list($list) { + # save the nodes in an array + my @a; + while (defined $list) { + push @a, $list; + $list = $list->next; + } + + # now reorder things + my $n = $#a; + for my $i (0..$n / 2 - 1) { + $a[$n-$i]->set_next($a[$i]->next); + $a[$i]->set_next($a[$n-$i]); + $a[$n-$i-1]->set_next(undef); + } +} |
