diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-01 20:18:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-01 20:18:15 +0100 |
| commit | 1802a14995644415a4a1c1155c3b86956cc31a82 (patch) | |
| tree | 4bf9e227fe03d63ca11ac2e3ce9948f5e751fdb8 | |
| parent | c84c77f0c7b03e19c66d0e3d16a8c4f93378ffdd (diff) | |
| parent | d7830afb3a0871658eaf295f3671095d2130472e (diff) | |
| download | perlweeklychallenge-club-1802a14995644415a4a1c1155c3b86956cc31a82.tar.gz perlweeklychallenge-club-1802a14995644415a4a1c1155c3b86956cc31a82.tar.bz2 perlweeklychallenge-club-1802a14995644415a4a1c1155c3b86956cc31a82.zip | |
Merge pull request #12614 from zapwai/branch-for-337
Week 337
| -rw-r--r-- | challenge-337/zapwai/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-337/zapwai/perl/ch-2.pl | 59 |
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-337/zapwai/perl/ch-1.pl b/challenge-337/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..862206b66c --- /dev/null +++ b/challenge-337/zapwai/perl/ch-1.pl @@ -0,0 +1,27 @@ +use v5.38; + +sub cum($lvl, @n) { + my $cnt = 0; + for my $n (@n) { + $cnt++ if ($lvl > $n); + } + return $cnt; +} + +sub proc(@n) { + say "Input : @n"; + my @n2; + push @n2, cum($_, @n) for (@n); + say "Output: @n2"; +} + +my @n = (6,5,4,8); +proc(@n); +@n = (7,7,7,7); +proc(@n); +@n = (5,4,3,2,1); +proc(@n); +@n = (-1, 0,3,-2,1); +proc(@n); +@n = (0,1,1,2,0); +proc(@n); diff --git a/challenge-337/zapwai/perl/ch-2.pl b/challenge-337/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..984abb16eb --- /dev/null +++ b/challenge-337/zapwai/perl/ch-2.pl @@ -0,0 +1,59 @@ +use v5.38; + +#increment a location in the matrix +sub boom($ref, $x, $y, $row, $col) { + for my $i (0 .. $row - 1) { + $$ref[$i][$y]++; + } + + for my $j (0 .. $col - 1) { + $$ref[$x][$j]++; + } +} + +sub proc($row, $col, @locations) { + print "Input: \$row = $row \$col = $col \@locations = ( "; + print "[".join(",", @$_)."] " foreach (@locations); + say ")"; + + #Initialize matrix + my @a; + for my $i (0 .. $row - 1) { + for my $j (0 .. $col - 1) { + $a[$i][$j] = 0; + } + } + + #Advance matrix at each location + for my $loc (@locations) { + my ($x, $y) = @$loc; + boom(\@a, $x, $y, $row, $col); + } + + #Display matrix! + say "Final: "; + my $o = 0; + for my $i (0 .. $row - 1) { + print " "; + for my $j (0 .. $col - 1) { + my $a = $a[$i][$j]; + print $a; + $o++ if ($a%2==1); + } + say ""; + } + + say "Output: $o"; +} + +my $row = 2; my $col = 3; my @locations = ([0,1],[1,1]); +proc($row, $col, @locations); +$row = 2; $col = 2; @locations = ([1,1],[0,0]); +proc($row, $col, @locations); +$row = 3; $col = 3; @locations = ([0,0],[1,2],[2,1]); +proc($row, $col, @locations); +$row = 1; $col = 5; @locations = ([0,2],[0,4]); +proc($row, $col, @locations); +$row = 4; $col = 2; @locations = ([1,0],[3,1],[2,0],[0,1]); +proc($row, $col, @locations); + |
