diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-19 06:18:14 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-19 06:18:14 +0000 |
| commit | 4d006eba46a36132edf46e8548f5f251b405d006 (patch) | |
| tree | 03490e2a59df6c7f1fdef12ff3129bf43a100ad1 | |
| parent | 09eeda584c580b11e6835d570fab3384ee7e210b (diff) | |
| parent | e81716a4f76b1904d52e5d0c5fbcf82bf36182d4 (diff) | |
| download | perlweeklychallenge-club-4d006eba46a36132edf46e8548f5f251b405d006.tar.gz perlweeklychallenge-club-4d006eba46a36132edf46e8548f5f251b405d006.tar.bz2 perlweeklychallenge-club-4d006eba46a36132edf46e8548f5f251b405d006.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-100/yet-ebreo/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-100/yet-ebreo/perl/ch-2.pl | 39 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-100/yet-ebreo/perl/ch-1.pl b/challenge-100/yet-ebreo/perl/ch-1.pl new file mode 100644 index 0000000000..569125e669 --- /dev/null +++ b/challenge-100/yet-ebreo/perl/ch-1.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use feature qw(say); +# You are given a time (12 hour / 24 hour). +# Write a script to convert the given time from 12 hour format to 24 hour format and vice versa. +# Ideally we expect a one-liner. +# Example 1: +# Input: 05:15 pm or 05:15pm +# Output: 17:15 +# Example 2: +# Input: 19:15 +# Output: 07:15 pm or 07:15pm + +#one-liner? sure thing! :D +sub f { pop=~/:.. */&&sprintf"%02d%s",$`%12+12*($'?'pm'eq$':$`%12<1),$&.($`<12?'am':'pm')x!$' } + +say &f('01:00 pm'); +say &f('12:01 am'); +say &f('12:00 pm'); +say &f('12:00 am'); +say &f('13:00'); +say &f('00:00'); +say &f('19:15'); diff --git a/challenge-100/yet-ebreo/perl/ch-2.pl b/challenge-100/yet-ebreo/perl/ch-2.pl new file mode 100644 index 0000000000..2640c69c6b --- /dev/null +++ b/challenge-100/yet-ebreo/perl/ch-2.pl @@ -0,0 +1,39 @@ +use strict; +use warnings; +use feature qw(say); + +# You are given triangle array. +# Write a script to find the minimum path sum from top to bottom. +# When you are on index i on the current row then you may move to either index i or index i + 1 on the next row. +# Example 1: +# Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] +# Output: 8 +# Explanation: The given triangle +# 1 +# 2 4 +# 6 4 9 +# 5 1 7 2 +# The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 +# [1] +# [2] 4 +# 6 [4] 9 +# 5 [1] 7 2 + +my @triangle = ( [1], [2,4], [6,4,9], [5,1,7,2] ); +my $min = 1e99; + +sub f { + my ($row, $col, $sum, $max) = @_; + + if ($row>$max) { + ($sum < $min) && ($min = $sum); + } else { + $sum += $triangle[$row][$col]; + + f($row+1, $col, $sum, $max); + f($row+1, $col+1, $sum, $max); + } +} + +f(0, 0, 0, $#triangle); +say $min
\ No newline at end of file |
