aboutsummaryrefslogtreecommitdiff
path: root/challenge-095/abigail/perl/ch-2.pl
blob: 463ad43d7e1fcbc23ac47e2b7f55ad0efe68735e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/opt/perl/bin/perl

use 5.032;

use strict;
use warnings;
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.
#
# min, however, is *NOT*. If you want a min operation, a stack is a
# horrible choice of datastructure, and you'd fail algorithms 101
# is that were your choice. Use a heap, or some kind of balanced tree.
#

my $ERROR = "Stack is empty";

my @stack;
while (<>) {
    if (/^push\s+(.*)/) {push @stack => $1}
    if (/^pop/)         {pop  @stack}
    if (/^top/)         {say  $stack [-1]  // $ERROR}
    if (/^min/)         {say  min (@stack) // $ERROR}
}

__END__