From a4c8767e48a2bb11f9840e0bd38917be2fa6481b Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Sun, 12 Jul 2020 22:26:31 +0800 Subject: Rename ch-1.pl to challenge-068/cheok-yin-fung/perl/ch-1.pl --- ch-1.pl | 87 ------------------------------- challenge-068/cheok-yin-fung/perl/ch-1.pl | 87 +++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 87 deletions(-) delete mode 100644 ch-1.pl create mode 100644 challenge-068/cheok-yin-fung/perl/ch-1.pl diff --git a/ch-1.pl b/ch-1.pl deleted file mode 100644 index bfffdfe43f..0000000000 --- a/ch-1.pl +++ /dev/null @@ -1,87 +0,0 @@ -#!/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[$N*$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"; -} - 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..bfffdfe43f --- /dev/null +++ b/challenge-068/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/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[$N*$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"; +} + -- cgit