From 8425624928e6067d82d4d0f54c003bcc690b03c8 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 15 Feb 2021 17:26:00 -0600 Subject: Simplify the codes slightly --- challenge-100/wlmb/perl/ch-1.pl | 2 +- challenge-100/wlmb/perl/ch-2.pl | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/challenge-100/wlmb/perl/ch-1.pl b/challenge-100/wlmb/perl/ch-1.pl index 6cc0f7ef7b..31284205c7 100755 --- a/challenge-100/wlmb/perl/ch-1.pl +++ b/challenge-100/wlmb/perl/ch-1.pl @@ -13,7 +13,7 @@ sub usage { Converts time between 12 and 24 hour formats Usage; ./ch-1.pl time1 time2 ... - Each argument must have the format hh:mm:ss ampm + Each argument must have the format "hh:mm:ss ampm" where the minutes and seconds are optional and ampm is either am or pm or null. If ampm is given, the hour should be not greater than 12 nor diff --git a/challenge-100/wlmb/perl/ch-2.pl b/challenge-100/wlmb/perl/ch-2.pl index 5716c3e031..eb9e60713e 100755 --- a/challenge-100/wlmb/perl/ch-2.pl +++ b/challenge-100/wlmb/perl/ch-2.pl @@ -25,13 +25,12 @@ my @next_row=(0)x@{$rows[-1]}; my $cost; my @choices; foreach my $current_row(reverse @rows){ # move upwards - my @current_row=pairwise {$a+$b} @$current_row, @next_row; # + my @current_row=pairwise {$a+$b} @$current_row, @next_row; #get totalcost for each element $cost=$current_row[0],last if @current_row==1; # done? # Find best choices for each index of the row above - @next_row=map {min ($current_row[$_], $current_row[$_+1])} (0..@current_row-2); - # and register their indices - my @chosen_indices=map {$next_row[$_]==$current_row[$_]?$_:$_+1} (0..@current_row-2); - # Build a triangle of chosen indices + my @chosen_indices=map {$current_row[$_]<=$current_row[$_+1]?$_:$_+1}(0..@current_row-2); + @next_row=@current_row[@chosen_indices]; + # Build a triangle of chosen indices for later display unshift @choices, [@chosen_indices]; } #print input triangle and optimal cost -- cgit