diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-21 21:19:47 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-21 21:19:47 +0000 |
| commit | 6b4be6f131458e6dcb7caecd152a731cbbacb027 (patch) | |
| tree | d31a0c4583fed1c124da4219720171e648a537ec /challenge-100 | |
| parent | d3ad1a565b6ccc7928f992bdaab7f5da1d4a7692 (diff) | |
| parent | 3158ddf165890786ed7174124071b6d8c8f45ef7 (diff) | |
| download | perlweeklychallenge-club-6b4be6f131458e6dcb7caecd152a731cbbacb027.tar.gz perlweeklychallenge-club-6b4be6f131458e6dcb7caecd152a731cbbacb027.tar.bz2 perlweeklychallenge-club-6b4be6f131458e6dcb7caecd152a731cbbacb027.zip | |
Merge pull request #3595 from boblied/master
PWC 99 solutions
Diffstat (limited to 'challenge-100')
| -rw-r--r-- | challenge-100/bob-lied/README | 4 | ||||
| -rwxr-xr-x | challenge-100/bob-lied/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-100/bob-lied/perl/ch-2.pl | 93 |
3 files changed, 164 insertions, 2 deletions
diff --git a/challenge-100/bob-lied/README b/challenge-100/bob-lied/README index be9398e9a3..bcd73da742 100644 --- a/challenge-100/bob-lied/README +++ b/challenge-100/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 83 by Bob Lied. +Solutions to weekly challenge 100 by Bob Lied. -https://perlweeklychallenge.org/blog/perl-weekly-challenge-083/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-100/ diff --git a/challenge-100/bob-lied/perl/ch-1.pl b/challenge-100/bob-lied/perl/ch-1.pl new file mode 100755 index 0000000000..1016be7c42 --- /dev/null +++ b/challenge-100/bob-lied/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge 100, TASK #1 › Fun Time +# +# 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.020; +use experimental qw/signatures/; + +use Getopt::Long; + +my $DoTest = 0; +GetOptions('test' => \$DoTest); + +runTest() if $DoTest; + +for my $tstr ( @ARGV ) +{ + say funTime($tstr); +} + +sub funTime($t) +{ + my ($hr,$min) = $t =~ m/(\d{1,2}):(\d\d)/; + return "--:--" if ( $hr < 0 || $hr > 24 || $min < 0 || $min > 59 ); + my $suf; + + if ( $t =~ m/am/i ) { $suf = "" ; } + elsif ( $t =~ m/pm/i ) { $suf = "" ; $hr += 12 if $hr < 12; } + elsif ( $hr == 24 ) { $suf = "am"; $hr = 0; } + elsif ( $hr == 12 ) { $suf = "pm"; } + elsif ( $hr < 12 ) { $suf = "am"; } + else { $suf = "pm"; $hr -= 12; } + + return sprintf "%02d:%02d%s", $hr, $min, $suf; +} + +sub runTest +{ + use Test::More; + for my $case ( + ['05:15', '05:15am'], + ['19:15', '07:15pm'], + ['00:00', '00:00am'], + ['11:15', '11:15am'], + ['23:45', '11:45pm'], + ['12:00', '12:00pm'], + ) + { + is( funTime($case->[0]), $case->[1] ); + is( funTime($case->[1]), $case->[0] ); + } + is( funTime("24:00"), "00:00am"); + + done_testing; +} diff --git a/challenge-100/bob-lied/perl/ch-2.pl b/challenge-100/bob-lied/perl/ch-2.pl new file mode 100755 index 0000000000..720df96b95 --- /dev/null +++ b/challenge-100/bob-lied/perl/ch-2.pl @@ -0,0 +1,93 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge 100, Task #2 › Triangle Sum +# +# 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 +#============================================================================= + +use strict; +use warnings; +use 5.020; +use experimental qw/ signatures /; + +use Getopt::Long; +my $DoTest; +my $Verbose; +GetOptions('test' => \$DoTest, 'verbose' => \$Verbose); + +use List::Util qw(min); + +runTest() if $DoTest; + +my $minSum; +sub walk($tree, $lastRow, $row, $col, $sum) +{ + say "WALK: lastRow=$lastRow, row=$row, col=$col, sum=$sum" if $Verbose; + if ( $row == $lastRow ) + { + $minSum = min( ($minSum //= $sum), $sum); + say "LEAF: row=$row, col=$col, sum=$sum: minSum=$minSum" if $Verbose; + return $sum; + } + + walk($tree, $lastRow, $row+1, $col, $sum + $tree->[$row+1][$col] ); + walk($tree, $lastRow, $row+1, $col+1, $sum + $tree->[$row+1][$col+1]); + + return $sum; +} +sub triangleSum($tri) +{ + $minSum = undef; + my $lastRow = @$tri - 1; + + if ( $lastRow > 0 ) + { + walk($tri, $lastRow, 1, 0, $tri->[0][0] + $tri->[1][0]) ; + walk($tri, $lastRow, 1, 1, $tri->[0][0] + $tri->[1][1]) ; + } + else + { + $minSum = $tri->[0][0]; + } + + return $minSum; +} + +sub runTest +{ + use Test::More; + + for my $case ( + [ [ [9] ] , 9 ], + [ [ [2], [3,5] ] , 5 ], + [ [ [1], [2,4], [6,4,9] ] , 7 ], + [ [ [1], [2,4], [6,4,9], [5,1,7,2] ] , 8 ], + [ [ [3], [3,1], [5,2,3], [4,3,1,3] ] , 7 ], + ) + { + is ( triangleSum($case->[0]), $case->[1] ); + } + + done_testing; +} + |
