aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-07-12 15:56:16 +0100
committerGitHub <noreply@github.com>2020-07-12 15:56:16 +0100
commit66306f6cbbe3f1f6249a820b4711acf5607eb5d5 (patch)
tree6955d87a5d2821e517c81ac66c105412f895c133
parent7eeb3feeb65a04c5f79638a92f540432ec051517 (diff)
parent2a017b4374854dc8567666d50dbb38f9099da976 (diff)
downloadperlweeklychallenge-club-66306f6cbbe3f1f6249a820b4711acf5607eb5d5.tar.gz
perlweeklychallenge-club-66306f6cbbe3f1f6249a820b4711acf5607eb5d5.tar.bz2
perlweeklychallenge-club-66306f6cbbe3f1f6249a820b4711acf5607eb5d5.zip
Merge pull request #1932 from E7-87-83/master
Cheok Yin's submission for PWC#068 Task 1
-rw-r--r--challenge-068/cheok-yin-fung/perl/ch-1.pl88
1 files changed, 88 insertions, 0 deletions
diff --git a/challenge-068/cheok-yin-fung/perl/ch-1.pl b/challenge-068/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..d17cce301f
--- /dev/null
+++ b/challenge-068/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+# Usage: ch-1.pl M N a_(1,1) a_(1,2) ... a(M,N)
+# Perl Weekly Challenge #068 Task 1 Zero Matrix
+
+my @matrix;
+my ($M, $N);
+$M = 4; $N = 3;
+@matrix = ([0, 1, 0, 1] ,[ 1, 0,1, 1] ,[1, 1, 1, 1]);
+
+if ($ARGV[0] and $ARGV[1]) {
+ $M = $ARGV[0];
+ $N = $ARGV[1];
+ @matrix = ();
+ die "parameter(s) problem" unless scalar @ARGV == ($M*$N+2);
+
+ for my $i (0..$N-1) {
+ my @s = ();
+ for my $j (0..$M-1) {
+ push @s, $ARGV[$M*$i+$j +2 ];
+ }
+ push @matrix, \@s ;
+ }
+}
+
+
+my @newmatrix;
+
+for my $i (0..$N-1) {
+ my @s = ();
+ for my $j (0..$M-1) {
+ push @s, 1;
+ }
+ push @newmatrix, \@s;
+}
+
+
+my @zerorow = ();
+my @zerocol = ();
+
+for my $i (0..$N-1) {
+ for my $j (0..$M-1) {
+ if ($matrix[$i][$j] == 0) {
+ # if (${@matrix[$i]}[$j] == 0) { improve after using warnings
+ push @zerorow, $i;
+ push @zerocol, $j;
+ }
+ }
+}
+
+sub assignzerorow {
+ my $r = $_[0];
+ for my $j (0..$M-1) {
+ $newmatrix[$r][$j] = 0;
+ }
+}
+
+sub assignzerocol {
+ my $c = $_[0];
+ for my $i (0..$N-1) {
+ $newmatrix[$i][$c] = 0;
+ }
+}
+
+print "\n";
+
+for my $i (0..$N-1) {
+ for my $j (0..$M-1) {
+ print $matrix[$i][$j], " ";
+ }
+ print "\n";
+}
+
+print " * * * * * * * \n";
+
+print "\n";
+assignzerorow($_) foreach (@zerorow);
+assignzerocol($_) foreach (@zerocol);
+
+for my $i (0..$N-1) {
+ for my $j (0..$M-1) {
+ print $newmatrix[$i][$j], " ";
+ }
+ print "\n";
+}
+