diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-08 08:41:08 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-07-08 08:41:08 +0100 |
| commit | f1b346b4170e685c2c6229e8cb8a5b1d9fe36b2d (patch) | |
| tree | 593a3b2037d1276fbe955565271b5d5925a3d301 /challenge-068/mohammad-anwar | |
| parent | d67a9ab53ac0948a74f7bb6235978aec3861491d (diff) | |
| download | perlweeklychallenge-club-f1b346b4170e685c2c6229e8cb8a5b1d9fe36b2d.tar.gz perlweeklychallenge-club-f1b346b4170e685c2c6229e8cb8a5b1d9fe36b2d.tar.bz2 perlweeklychallenge-club-f1b346b4170e685c2c6229e8cb8a5b1d9fe36b2d.zip | |
- Added Raku solutions for the "Zero Matrix" task.
Diffstat (limited to 'challenge-068/mohammad-anwar')
| -rw-r--r-- | challenge-068/mohammad-anwar/raku/ch-1.raku | 79 | ||||
| -rw-r--r-- | challenge-068/mohammad-anwar/raku/ch-1a.raku | 52 |
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-068/mohammad-anwar/raku/ch-1.raku b/challenge-068/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..a766354a50 --- /dev/null +++ b/challenge-068/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,79 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 068 +# +# Task #1: Zero Matrix +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-068/ +# + +use v6.d; + +sub MAIN(Int :$R? is copy where { $R >= 2 } = 3, + Int :$C? is copy where { $C >= 2 } = 3) { + + my $matrix = get-matrix(--$R, --$C); + my $zero_matrix = make-zero-matrix($matrix); + + display-matrix('Matrix:', $matrix); + display-matrix('Zero Matrix:', $zero_matrix); +} + +# +# +# METHODS + +sub get-matrix(Int $rows where { $rows >= 1 }, + Int $cols where { $cols >= 1 }) { + + # prabability of picking 1 is higher than 0 (80:20). + my $min = 0; + my $max = 9; + my $array = [ 1, 0, 1, 1, 1, 1, 1, 0, 1, 1 ]; + + my $matrix = []; + for 0..$rows -> $r { + for (0..$cols) -> $c { + $matrix.[$r][$c] = $array.pick; + } + } + + return $matrix; +} + +sub make-zero-matrix($matrix) { + + my $rows = $matrix.elems - 1; + my $cols = $matrix.[0].elems - 1; + + my $zero_matrix = []; + for 0..$rows -> $r { + for 0..$cols -> $c { + if $matrix.[$r][$c] == 0 { + # make zero row + (0..$cols).map({ $zero_matrix.[$r][$_] = 0 }); + # make zero col + (0..$rows).map({ $zero_matrix.[$_][$c] = 0 }); + } + } + } + + # fill empty cells, if any. + for (0..$rows) -> $r { + for (0..$cols) -> $c { + $zero_matrix.[$r][$c] = 1 unless + defined $zero_matrix.[$r][$c]; + } + } + + return $zero_matrix; +} + +sub display-matrix($label, $matrix) { + + say $label; + for 0..$matrix.elems - 1 -> $r { + say sprintf("[ %s ]", $matrix.[$r].join(', ')); + } +} diff --git a/challenge-068/mohammad-anwar/raku/ch-1a.raku b/challenge-068/mohammad-anwar/raku/ch-1a.raku new file mode 100644 index 0000000000..b26981df76 --- /dev/null +++ b/challenge-068/mohammad-anwar/raku/ch-1a.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 068 +# +# Task #1: Zero Matrix +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-068/ +# + +use Test; + +is-deeply make-zero-matrix([[1, 0, 1], [1, 1, 1], [1, 1, 1]]), + [[0, 0, 0], [1, 0, 1], [1, 0, 1]], + 'testing example 1'; +is-deeply make-zero-matrix([[1, 0, 1], [1, 1, 1], [1, 0, 1]]), + [[0, 0, 0], [1, 0, 1], [0, 0, 0]], + 'testing example 2'; + +done-testing; + +# +# +# METHODS + +sub make-zero-matrix($matrix) { + + my $rows = $matrix.elems - 1; + my $cols = $matrix.[0].elems - 1; + + my $zero_matrix = []; + for 0..$rows -> $r { + for 0..$cols -> $c { + if $matrix.[$r][$c] == 0 { + # make zero row + (0..$cols).map({ $zero_matrix.[$r][$_] = 0 }); + # make zero col + (0..$rows).map({ $zero_matrix.[$_][$c] = 0 }); + } + } + } + + # fill empty cells, if any. + for (0..$rows) -> $r { + for (0..$cols) -> $c { + $zero_matrix.[$r][$c] = 1 unless + defined $zero_matrix.[$r][$c]; + } + } + + return $zero_matrix; +} |
