aboutsummaryrefslogtreecommitdiff
path: root/challenge-076/mohammad-anwar
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-31 05:28:27 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-31 05:28:27 +0100
commit87c6f1e47af81cce9da9dde6e73ca9de5bd77367 (patch)
treede9eeee6aadc5c7cad2fe885243a8ddf8196fa60 /challenge-076/mohammad-anwar
parentbd62d3ce8a741517052c68bd21ef8a8db8f9a8f6 (diff)
downloadperlweeklychallenge-club-87c6f1e47af81cce9da9dde6e73ca9de5bd77367.tar.gz
perlweeklychallenge-club-87c6f1e47af81cce9da9dde6e73ca9de5bd77367.tar.bz2
perlweeklychallenge-club-87c6f1e47af81cce9da9dde6e73ca9de5bd77367.zip
- Added template for Challenge 076.
Diffstat (limited to 'challenge-076/mohammad-anwar')
-rw-r--r--challenge-076/mohammad-anwar/README1
-rwxr-xr-xchallenge-076/mohammad-anwar/perl/ch-1.t67
-rwxr-xr-xchallenge-076/mohammad-anwar/perl/ch-2.t109
-rwxr-xr-xchallenge-076/mohammad-anwar/raku/ch-1.t57
-rwxr-xr-xchallenge-076/mohammad-anwar/raku/ch-2.t106
5 files changed, 340 insertions, 0 deletions
diff --git a/challenge-076/mohammad-anwar/README b/challenge-076/mohammad-anwar/README
new file mode 100644
index 0000000000..d389e70c3b
--- /dev/null
+++ b/challenge-076/mohammad-anwar/README
@@ -0,0 +1 @@
+Solutions by Mohammad S Anwar.
diff --git a/challenge-076/mohammad-anwar/perl/ch-1.t b/challenge-076/mohammad-anwar/perl/ch-1.t
new file mode 100755
index 0000000000..c2d32443b5
--- /dev/null
+++ b/challenge-076/mohammad-anwar/perl/ch-1.t
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+#
+# Perl Weekly Challenge - 075
+#
+# Task #1: Coins Sum
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-075
+#
+
+use strict;
+use warnings;
+use Test::More;
+
+is coins_sum(prepare("1, 2"), 5), 3;
+is coins_sum(prepare("1, 2, 3"), 5), 5;
+is coins_sum(prepare("1, 2, 4"), 6), 6;
+is coins_sum(prepare("25, 10, 5"), 30), 5;
+is coins_sum(prepare("9, 6, 5, 1"), 11), 6;
+
+done_testing;
+
+#
+#
+# METHODS
+
+sub coins_sum {
+ my ($coins, $sum) = @_;
+
+ my $size = $#$coins;
+ return 0 if ($size == -1 || $sum <= 0);
+
+ my $matrix;
+
+ # Sum of 0 can be achieved in one possible way.
+ $matrix->[$_][0] = 1 for (0 .. $size+1);
+
+ foreach my $i (0 .. $size) {
+
+ foreach my $j (1 .. $sum) {
+
+ my $include_current = 0;
+ my $exclude_current = 0;
+
+ if ($coins->[$i] <= $j) {
+ $include_current = $matrix->[$i][$j - $coins->[$i]];
+ }
+
+ if ($i > 0) {
+ $exclude_current = $matrix->[$i - 1][$j];
+ }
+
+ $matrix->[$i][$j] = $include_current + $exclude_current;
+ }
+ }
+
+ return $matrix->[$size][$sum];
+}
+
+sub prepare {
+ my ($coins) = @_;
+
+ if (defined $coins) {
+ $coins =~ s/\s//g;
+ return [ split /\,/, $coins ];
+ }
+}
diff --git a/challenge-076/mohammad-anwar/perl/ch-2.t b/challenge-076/mohammad-anwar/perl/ch-2.t
new file mode 100755
index 0000000000..51ba0dec90
--- /dev/null
+++ b/challenge-076/mohammad-anwar/perl/ch-2.t
@@ -0,0 +1,109 @@
+#!/usr/bin/perl
+
+#
+# Perl Weekly Challenge - 075
+#
+# Task #2: Largest Rectangle Histogram
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-075
+#
+
+use strict;
+use warnings;
+use Test::More;
+use List::Util qw(min max);
+
+is(largest_rectangle_histogram(prepare("2, 1, 4, 5, 3, 7")), 12, "example 1");
+is(largest_rectangle_histogram(prepare("3, 2, 3, 5, 7, 5")), 15, "example 2");
+
+done_testing;
+
+#
+#
+# METHODS
+
+sub largest_rectangle_histogram {
+ my ($list) = @_;
+
+ my $i = 0;
+ my $max = 0;
+ foreach my $n (@$list) {
+
+ my ($left, $right) = (0, 0);
+ $left = go_left($i, $list) if ($i > 0);
+ $right = go_right($i, $list) if ($i <= $#$list);
+
+ my @heights = (@$list)[$i - $left .. $i + $right];
+ my $size = min(@heights) * @heights;
+ $max = $size if ($size > $max);
+
+ $i++;
+ }
+
+ return $max;
+}
+
+sub go_left {
+ my ($i, $list) = @_;
+
+ my $c = $list->[$i];
+ my $j = 0;
+ while ($i > 0) {
+ $i--;
+ last if ($list->[$i] < $c);
+ $j++;
+ }
+
+ return $j;
+}
+
+sub go_right {
+ my ($i, $list) = @_;
+
+ my $c = $list->[$i];
+ my $j = 0;
+ while ($i < $#$list) {
+ $i++;
+ last if ($list->[$i] < $c);
+ $j++;
+ }
+
+ return $j;
+}
+
+sub chart {
+ my ($list) = @_;
+
+ my $max = max(@$list);
+ my $chart = [];
+ my $row = 1;
+ foreach (1..$max) {
+ my $data = "";
+ foreach my $i (0..$#$list) {
+ if ($row <= $list->[$i]) {
+ $data .= " #";
+ }
+ else {
+ $data .= " ";
+ }
+ }
+ $row++;
+ push @$chart, sprintf("%d%s", $_, $data);
+ }
+
+ my ($histogram, $line, $size) = ("", "", " ");
+ $histogram = join "\n", (reverse @$chart);
+ $line .= "_ " for (0..$#$list + 1);
+ $size .= join " ", @$list;
+
+ return join "\n", $histogram, $line, $size;
+}
+
+sub prepare {
+ my ($list) = @_;
+
+ if (defined $list) {
+ $list =~ s/\s//g;
+ return [ split /\,/, $list ];
+ }
+}
diff --git a/challenge-076/mohammad-anwar/raku/ch-1.t b/challenge-076/mohammad-anwar/raku/ch-1.t
new file mode 100755
index 0000000000..e1a0e5b656
--- /dev/null
+++ b/challenge-076/mohammad-anwar/raku/ch-1.t
@@ -0,0 +1,57 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 075
+#
+# Task #1: Coins Sum
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-075
+#
+
+use Test;
+
+is coins-sum("1, 2", 5), 3, "Coins=[1, 2] Sum=5";
+is coins-sum("1, 2, 3", 5), 5, "Coins=[1, 2, 3] Sum=5";
+is coins-sum("1, 2, 4", 6), 6, "Coins=[1, 2, 4] Sum=6";
+is coins-sum("25, 10, 5", 30), 5, "Coins=[25, 10, 5] Sum=30";
+is coins-sum("9, 6, 5, 1", 11), 6, "Coins=[9, 6, 5, 1] Sum=6";
+
+done-testing;
+
+sub coins-sum(Str $coins is copy, Int $sum) returns Int {
+
+ die "ERROR: Invalid coins [$coins].\n"
+ unless $coins ~~ /^[\s?\-?\d\,?\s?]+$/;
+
+ $coins ~~ s:g/\s//;
+ my $_coins = [ $coins.split(',').map({ .Int }) ];
+
+ my $size = $_coins.elems;
+ return 0 if ($size == 0 || $sum <= 0);
+
+ my $matrix;
+
+ # Sum of 0 can be achieved in one possible way.
+ $matrix.[$_][0] = 1 for (0 .. $size-1);
+
+ for 0 .. $size-1 -> $i {
+
+ for 1 .. $sum -> $j {
+
+ my Int $include-current = 0;
+ my Int $exclude-current = 0;
+
+ if $_coins.[$i] <= $j {
+ $include-current = $matrix.[$i][$j - $_coins.[$i]];
+ }
+
+ if $i > 0 {
+ $exclude-current = $matrix.[$i - 1][$j];
+ }
+
+ $matrix.[$i][$j] = $include-current + $exclude-current;
+ }
+ }
+
+ return $matrix.[$size-1][$sum];
+}
diff --git a/challenge-076/mohammad-anwar/raku/ch-2.t b/challenge-076/mohammad-anwar/raku/ch-2.t
new file mode 100755
index 0000000000..f6e50809da
--- /dev/null
+++ b/challenge-076/mohammad-anwar/raku/ch-2.t
@@ -0,0 +1,106 @@
+#!/usr/bin/env raku
+
+#
+# Perl Weekly Challenge - 075
+#
+# Task #2: Largest Rectangle Histogram
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-075
+#
+
+use Test;
+
+is largest-rectangle-histogram(prepare("2, 1, 4, 5, 3, 7")), 12, "example 1";
+is largest-rectangle-histogram(prepare("3, 2, 3, 5, 7, 5")), 15, "example 2";
+
+done-testing;
+
+#
+#
+# METHODS
+
+sub largest-rectangle-histogram($list where .all ~~ Int) returns Int {
+
+ my Int $i = 0;
+ my Int $max = 0;
+
+ for 0 .. $list.elems-1 -> $i {
+
+ my (Int $left, Int $right) = (0, 0);
+ $left = go-left($i, $list) if $i > 0;
+ $right = go-right($i, $list) if $i < $list.elems;
+
+ my $heights = $list.[$i - $left .. $i + $right];
+ my $size = $heights.min * $heights.elems;
+
+ $max = $size if $size > $max;
+ }
+
+ return $max;
+}
+
+sub go-left(Int $i is copy, $list where .all ~~ Int) returns Int {
+
+ my $c = $list.[$i];
+ my $j = 0;
+ while $i > 0 {
+ $i = $i - 1;
+ last if $list.[$i] < $c;
+ $j = $j + 1;
+ }
+
+ return $j;
+}
+
+sub go-right(Int $i is copy, $list where .all ~~ Int) returns Int {
+
+ my $c = $list.[$i];
+ my $j = 0;
+ while $i < $list.elems-1 {
+ $i += 1;
+ last if $list.[$i] < $c;
+ $j += 1;
+ }
+
+ return $j;
+}
+
+sub chart($list where .all ~~ Int) {
+
+ my $max = $list.max;
+ my $chart = [];
+ my $row = 1;
+ for 1 .. $max -> $n {
+ my Str $data = "";
+ for 0 .. $list.elems-1 -> $i {
+ if $row <= $list.[$i] {
+ $data ~= " #";
+ }
+ else {
+ $data ~= " ";
+ }
+ }
+
+ $row += 1;
+
+ $chart.push: sprintf("%d%s", $n, $data);
+ }
+
+ my (Str $histogram, Str $line, Str $size) = ("", "", " ");
+ $histogram = $chart.reverse.join("\n");
+ $line ~= "_ " for 0 .. $list.elems;
+ $size ~= $list.join(" ");
+
+ return ($histogram, $line, $size).join("\n");
+}
+
+sub prepare(Str $list is copy) {
+
+ return unless $list.defined;
+
+ die "ERROR: Invalid list [$list].\n"
+ unless $list ~~ /^[\s?\-?\d\,?\s?]+$/;
+
+ $list ~~ s:g/\s//;
+ return [ $list.split(',').map({ .Int }) ];
+}