diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-02-20 10:32:26 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-02-20 10:32:26 +0000 |
| commit | 3bd8f85328c182a2a1c35dbe6a3eacc66b941094 (patch) | |
| tree | 73eed0dee6be5721145d662408db57ab3b7b826d /challenge-152/mohammad-anwar | |
| parent | c1d348be077bc146e2f814cf3f6cc90fd0540154 (diff) | |
| download | perlweeklychallenge-club-3bd8f85328c182a2a1c35dbe6a3eacc66b941094.tar.gz perlweeklychallenge-club-3bd8f85328c182a2a1c35dbe6a3eacc66b941094.tar.bz2 perlweeklychallenge-club-3bd8f85328c182a2a1c35dbe6a3eacc66b941094.zip | |
- Added Perl solution to "Triangle Sum Path" of week 152.
Diffstat (limited to 'challenge-152/mohammad-anwar')
| -rw-r--r-- | challenge-152/mohammad-anwar/perl/ch-1.pl | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-152/mohammad-anwar/perl/ch-1.pl b/challenge-152/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..de9501254d --- /dev/null +++ b/challenge-152/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +=head1 + +Week 152: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-152 + +Task #1: Triangle Sum Path + + You are given a triangle array. + + Write a script to find the minimum sum path from top to bottom. + +=cut + +use strict; +use warnings; +use Test::More; +use List::Util qw(min); + +is(triangle_sum_path_1([ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ]), 8, 'Example 1 (option 1)'); +is(triangle_sum_path_1([ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9] ]), 9, 'Example 2 (option 1)'); + +is(triangle_sum_path_2([ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ]), 8, 'Example 1 (option 2)'); +is(triangle_sum_path_2([ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9] ]), 9, 'Example 2 (option 2)'); + +done_testing; + +# +# +# METHOD + +sub triangle_sum_path_1 { + my ($triangle_array) = @_; + + my $sum_path = 0; + foreach my $row (@$triangle_array) { + $sum_path += min @$row; + } + + return $sum_path; +} + +sub triangle_sum_path_2 { + my ($triangle_array) = @_; + + my $sum_path = 0; + $sum_path += min @$_ for @$triangle_array;; + + return $sum_path; +} |
