From b645c150c27865a7c5430616bb163c85ef4b1ce6 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 12 Jan 2021 18:11:20 +0100 Subject: Don't be silly. Removed the nonsense of using a different stack when calculating min. Now we just use List::Util::min, like we do other languages solutions. --- challenge-095/abigail/perl/ch-2.pl | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/challenge-095/abigail/perl/ch-2.pl b/challenge-095/abigail/perl/ch-2.pl index 0d6fe3c042..463ad43d7e 100644 --- a/challenge-095/abigail/perl/ch-2.pl +++ b/challenge-095/abigail/perl/ch-2.pl @@ -9,6 +9,8 @@ no warnings 'syntax'; use experimental 'signatures'; use experimental 'lexical_subs'; +use List::Util 'min'; + # # First off, this is a very strange exercise. "push", "pop" and "top" # are bog standard stack operations. @@ -22,32 +24,10 @@ my $ERROR = "Stack is empty"; my @stack; while (<>) { - chomp; - if (/^push\s+/p) {push @stack => ${^POSTMATCH}} - elsif (/^pop/) {pop @stack} - elsif (/^top/) {say $stack [-1] // $ERROR} - elsif (/^min/) { - # - # Well, since we're asked to demonstrate stack operations - # with "min", we won't be using List::Util::min, instead, - # we will use a second stack.... - # - my $min; - my @other_stack; - while (@stack) { - my $top = pop @stack; - $min = $top if !defined $min || $top < $min; - push @other_stack => $top; - } - say $min // $ERROR; - # - # Restore the original stack. - # - push @stack => pop @other_stack while @other_stack; - } - else { - die "Undefined command"; - } + if (/^push\s+(.*)/) {push @stack => $1} + if (/^pop/) {pop @stack} + if (/^top/) {say $stack [-1] // $ERROR} + if (/^min/) {say min (@stack) // $ERROR} } __END__ -- cgit