diff options
| author | Abigail <abigail@abigail.be> | 2021-01-12 18:11:20 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-12 18:11:20 +0100 |
| commit | b645c150c27865a7c5430616bb163c85ef4b1ce6 (patch) | |
| tree | b751871e2bb1a1d06a023481adb2b522ea2ace4e | |
| parent | c9cd319243d636baa71d50255bc4f0c71c2c2892 (diff) | |
| download | perlweeklychallenge-club-b645c150c27865a7c5430616bb163c85ef4b1ce6.tar.gz perlweeklychallenge-club-b645c150c27865a7c5430616bb163c85ef4b1ce6.tar.bz2 perlweeklychallenge-club-b645c150c27865a7c5430616bb163c85ef4b1ce6.zip | |
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.
| -rw-r--r-- | challenge-095/abigail/perl/ch-2.pl | 32 |
1 files 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__ |
