From afab67673cd0533780c8b7dc38835caba7ee3491 Mon Sep 17 00:00:00 2001 From: Doomtrain14 Date: Fri, 19 Feb 2021 09:47:42 +0800 Subject: Added solution for ch100 --- challenge-100/yet-ebreo/perl/ch-1.pl | 22 ++++++++++++++++++++ challenge-100/yet-ebreo/perl/ch-2.pl | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 challenge-100/yet-ebreo/perl/ch-1.pl create mode 100644 challenge-100/yet-ebreo/perl/ch-2.pl 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..515b224531 --- /dev/null +++ b/challenge-100/yet-ebreo/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /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)),$&.($`<12?'am':'pm')x!$' } + +say &f('01:00 pm'); +say &f('12:01 am'); +say &f('13:00'); +say &f('00:00'); 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 -- cgit From 72209dc06a802d269a2adbd0265066cd75d8a9d5 Mon Sep 17 00:00:00 2001 From: Doomtrain14 Date: Fri, 19 Feb 2021 10:57:21 +0800 Subject: Added few tests --- challenge-100/yet-ebreo/perl/ch-1.pl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/challenge-100/yet-ebreo/perl/ch-1.pl b/challenge-100/yet-ebreo/perl/ch-1.pl index 515b224531..569125e669 100644 --- a/challenge-100/yet-ebreo/perl/ch-1.pl +++ b/challenge-100/yet-ebreo/perl/ch-1.pl @@ -14,9 +14,12 @@ use feature qw(say); # Output: 07:15 pm or 07:15pm #one-liner? sure thing! :D -sub f { pop=~/:.. */&&sprintf"%02d%s",$`%12+12*($'?'pm'eq$':!($`%12)),$&.($`<12?'am':'pm')x!$' } +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'); -- cgit