aboutsummaryrefslogtreecommitdiff
path: root/challenge-064
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-064')
-rw-r--r--challenge-064/mohammad-anwar/perl/ch-1.pl39
-rw-r--r--challenge-064/mohammad-anwar/perl/ch-1a.pl47
-rw-r--r--challenge-064/mohammad-anwar/perl/ch-2.pl20
-rw-r--r--challenge-064/mohammad-anwar/perl/ch-2a.pl20
4 files changed, 126 insertions, 0 deletions
diff --git a/challenge-064/mohammad-anwar/perl/ch-1.pl b/challenge-064/mohammad-anwar/perl/ch-1.pl
new file mode 100644
index 0000000000..a9bcec01e6
--- /dev/null
+++ b/challenge-064/mohammad-anwar/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $M = [
+ [ 1, 2, 3 ],
+ [ 4, 5, 6 ],
+ [ 7, 8, 9 ],
+];
+
+print sprintf("%s\n", join " → ", @$_) for find_path($M, 0, 0);
+
+sub find_path {
+ my ($matrix, $row, $col, $path) = @_;
+ $path = [] unless defined $path;
+
+ my $rows = $#$matrix;
+ my $cols = $#{$matrix->[0]};
+
+ # check boundary?
+ return if ($row > $rows || $col > $cols);
+
+ my $final_path = [ @$path ];
+ push @$final_path, $matrix->[$row][$col];
+
+ # reached bottom right corner?
+ return $final_path if ($row == $rows && $col == $cols);
+
+ my @current_path = ();
+
+ # go right if possible.
+ push @current_path, find_path($matrix, $row, $col + 1, $final_path);
+
+ # go down if possible.
+ push @current_path, find_path($matrix, $row + 1, $col, $final_path);
+
+ return @current_path;
+}
diff --git a/challenge-064/mohammad-anwar/perl/ch-1a.pl b/challenge-064/mohammad-anwar/perl/ch-1a.pl
new file mode 100644
index 0000000000..cbfc232d11
--- /dev/null
+++ b/challenge-064/mohammad-anwar/perl/ch-1a.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+
+is_deeply([find_path([[ 1, 2, 3 ],
+ [ 4, 5, 6 ],
+ [ 7, 8, 9 ]],
+ 0, 0)],
+ [[ 1, 2, 3, 6, 9 ],
+ [ 1, 2, 5, 6, 9 ],
+ [ 1, 2, 5, 8, 9 ],
+ [ 1, 4, 5, 6, 9 ],
+ [ 1, 4, 5, 8, 9 ],
+ [ 1, 4, 7, 8, 9 ]]);
+
+done_testing;
+
+sub find_path {
+ my ($matrix, $row, $col, $path) = @_;
+ $path = [] unless defined $path;
+
+ my $rows = $#$matrix;
+ my $cols = $#{$matrix->[0]};
+
+ # check boundary?
+ return if ($row > $rows || $col > $cols);
+
+ my $final_path = [ @$path ];
+ push @$final_path, $matrix->[$row][$col];
+
+ # reached bottom right corner?
+ return $final_path if ($row == $rows && $col == $cols);
+
+ my @current_path = ();
+
+ # go right if possible.
+ push @current_path, find_path($matrix, $row, $col + 1, $final_path);
+
+ # go down if possible.
+ push @current_path, find_path($matrix, $row + 1, $col, $final_path);
+
+ return @current_path;
+}
diff --git a/challenge-064/mohammad-anwar/perl/ch-2.pl b/challenge-064/mohammad-anwar/perl/ch-2.pl
new file mode 100644
index 0000000000..167575aebe
--- /dev/null
+++ b/challenge-064/mohammad-anwar/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $string = "perlweeklychallenge";
+my $words = [ "weekly", "challenge", "perl" ];
+my $match = word_break($string, $words);
+
+(@$match)
+?
+(print sprintf("Matched: %s\n", join (", ", @$match)))
+:
+(print "None matched.\n");
+
+sub word_break {
+ my ($string, $words) = @_;
+
+ return [ grep { $string =~ /$_.*?/i } @$words ];
+}
diff --git a/challenge-064/mohammad-anwar/perl/ch-2a.pl b/challenge-064/mohammad-anwar/perl/ch-2a.pl
new file mode 100644
index 0000000000..a010c4a431
--- /dev/null
+++ b/challenge-064/mohammad-anwar/perl/ch-2a.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+
+is_deeply(word_break("perlweeklychallenge", [ "weekly", "challenge", "perl" ]),
+ [ "weekly", "challenge", "perl" ]);
+is_deeply(word_break("perlandraku", [ "python", "ruby", "haskell" ]),
+ [ ]);
+
+done_testing;
+
+sub word_break {
+ my ($string, $words) = @_;
+
+ return [ grep { $string =~ /$_.*?/i } @$words ];
+}