diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-16 20:15:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-16 20:15:42 +0000 |
| commit | 484bbbe3662d7448cf93d0e2e29a8666c8d9b51c (patch) | |
| tree | 383ab66d7aa1f5930d9c7d49851f5ad1b320855d | |
| parent | 94b811cb4dadf38d8f743598a742638afd03deb4 (diff) | |
| parent | 81d0e649a1806722ae856cbb612716a50824b8f6 (diff) | |
| download | perlweeklychallenge-club-484bbbe3662d7448cf93d0e2e29a8666c8d9b51c.tar.gz perlweeklychallenge-club-484bbbe3662d7448cf93d0e2e29a8666c8d9b51c.tar.bz2 perlweeklychallenge-club-484bbbe3662d7448cf93d0e2e29a8666c8d9b51c.zip | |
Merge pull request #3547 from pauloscustodio/paulo-custodio
Add Perl solution to challenge 100
| -rw-r--r-- | challenge-100/paulo-custodio/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/perl/ch-2.pl | 83 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/t/test-1.yaml | 40 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/test.pl | 7 |
5 files changed, 175 insertions, 0 deletions
diff --git a/challenge-100/paulo-custodio/perl/ch-1.pl b/challenge-100/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..88e5a10357 --- /dev/null +++ b/challenge-100/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +# TASK #1 > Fun Time +# Submitted by: Mohammad S Anwar +# 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 + +use strict; +use warnings; +use 5.030; +use Time::Piece; + +@ARGV==1 or die "Usage: ch-1.pl time\n"; +my $time = shift =~ s/\s+//gr; +if ($time =~ /AM|PM/i) { + say Time::Piece->strptime($time, "%I:%M%P")->strftime("%H:%M"); +} +else { + say Time::Piece->strptime($time, "%H:%M")->strftime("%I:%M%P"); +} diff --git a/challenge-100/paulo-custodio/perl/ch-2.pl b/challenge-100/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..03ca506736 --- /dev/null +++ b/challenge-100/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl + +# TASK #2 > Triangle Sum +# Submitted by: Mohammad S Anwar +# 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 +# Example 2: +# Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] +# Output: 7 +# +# Explanation: The given triangle +# +# 3 +# 3 1 +# 5 2 3 +# 4 3 1 3 +# +# The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 +# +# [3] +# 3 [1] +# 5 [2] 3 +# 4 3 [1] 3 + +use strict; +use warnings; +use 5.030; + +@ARGV or die "Usage: ch-2.pl row1 row2 ...\n"; +my @triangle = parse(@ARGV); +say min_sum(@triangle); + +sub parse { + my(@rows) = @_; + my @triangle; + my $i = 1; + while (@rows) { + my @row = split(/\D+/, shift(@rows)); + @row==$i or die "malformed triangle row $i\n"; + push @triangle, \@row; + $i++; + } + return @triangle; +} + +sub min_sum { + my(@triangle) = @_; + return min_sum_1(0, 0, 0, \@triangle); +} + +sub min_sum_1 { + my($sum, $r, $c, $triangle) = @_; + if ($r == $#$triangle) { # bottom row + return $sum + $triangle->[$r][$c]; + } + else { + $sum += $triangle->[$r][$c]; + my $sum1 = min_sum_1($sum, $r+1, $c, $triangle); + my $sum2 = min_sum_1($sum, $r+1, $c+1, $triangle); + return $sum1<$sum2 ? $sum1 : $sum2; + } +} diff --git a/challenge-100/paulo-custodio/t/test-1.yaml b/challenge-100/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ae209f3172 --- /dev/null +++ b/challenge-100/paulo-custodio/t/test-1.yaml @@ -0,0 +1,40 @@ +- setup: + cleanup: + args: 05:15PM + input: + output: 17:15 +- setup: + cleanup: + args: 17:15 + input: + output: 05:15pm +- setup: + cleanup: + args: 5:00am + input: + output: 05:00 +- setup: + cleanup: + args: 5:00 + input: + output: 05:00am +- setup: + cleanup: + args: 12:00am + input: + output: 00:00 +- setup: + cleanup: + args: 00:00 + input: + output: 12:00am +- setup: + cleanup: + args: 12:00pm + input: + output: 12:00 +- setup: + cleanup: + args: 12:00 + input: + output: 12:00pm diff --git a/challenge-100/paulo-custodio/t/test-2.yaml b/challenge-100/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..7641a99b77 --- /dev/null +++ b/challenge-100/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2,4 6,4,9 5,1,7,2 + input: + output: 8 +- setup: + cleanup: + args: 3 3,1 5,2,3 4,3,1,3 + input: + output: 7 +- setup: + cleanup: + args: 1 5,4 6,9,9 1,9,9,9 1,9,9,9,9 + input: + output: 14 diff --git a/challenge-100/paulo-custodio/test.pl b/challenge-100/paulo-custodio/test.pl new file mode 100644 index 0000000000..01ed2b83cd --- /dev/null +++ b/challenge-100/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; |
