From 463adb0cbdada147a962aa85951f95bd8a1d7847 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 11 Jan 2021 13:13:48 +0100 Subject: Perl solution for week 95/part 2. --- challenge-095/abigail/README.md | 25 ++++++++++++++++ challenge-095/abigail/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++++++ challenge-095/abigail/t/ctest.ini | 3 +- challenge-095/abigail/t/input-2-1 | 7 +++++ challenge-095/abigail/t/input-2-2 | 23 +++++++++++++++ challenge-095/abigail/t/output-2-1.exp | 2 ++ challenge-095/abigail/t/output-2-2.exp | 6 ++++ 7 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 challenge-095/abigail/perl/ch-2.pl create mode 100644 challenge-095/abigail/t/input-2-1 create mode 100644 challenge-095/abigail/t/input-2-2 create mode 100644 challenge-095/abigail/t/output-2-1.exp create mode 100644 challenge-095/abigail/t/output-2-2.exp diff --git a/challenge-095/abigail/README.md b/challenge-095/abigail/README.md index 6ceb4f2573..aead395b0a 100644 --- a/challenge-095/abigail/README.md +++ b/challenge-095/abigail/README.md @@ -21,3 +21,28 @@ Output: 0 ### Solutions * [Perl](perl/ch-1.pl) + + +## [Task 2](https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/#TASK2) + +Write a script to demonstrate `Stack` operations like below: + +* `push($n)` - add $n to the stack +* `pop()` - remove the top element +* `top()` - get the top element +* `min()` - return the minimum element + +### Example +~~~~ +my $stack = Stack->new; +$stack->push(2); +$stack->push(-1); +$stack->push(0); +$stack->pop; # removes 0 +print $stack->top; # prints -1 +$stack->push(0); +print $stack->min; # prints -1 +~~~~ + +### Solutions +* [Perl](perl/ch-2.pl) diff --git a/challenge-095/abigail/perl/ch-2.pl b/challenge-095/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..0d6fe3c042 --- /dev/null +++ b/challenge-095/abigail/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# 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 (<>) { + 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"; + } +} + +__END__ diff --git a/challenge-095/abigail/t/ctest.ini b/challenge-095/abigail/t/ctest.ini index a517632fbf..4e06c03c53 100644 --- a/challenge-095/abigail/t/ctest.ini +++ b/challenge-095/abigail/t/ctest.ini @@ -3,4 +3,5 @@ 1-2 = Test decimal dot 1-3 = Very short inputs 1-4 = Unicode and script runs -2-1 = Example 1 +2-1 = Given example +2-2 = Test empty stack diff --git a/challenge-095/abigail/t/input-2-1 b/challenge-095/abigail/t/input-2-1 new file mode 100644 index 0000000000..2fd42265cf --- /dev/null +++ b/challenge-095/abigail/t/input-2-1 @@ -0,0 +1,7 @@ +push 2 +push -1 +push 0 +pop +top +push 0 +min diff --git a/challenge-095/abigail/t/input-2-2 b/challenge-095/abigail/t/input-2-2 new file mode 100644 index 0000000000..ccdf552d6b --- /dev/null +++ b/challenge-095/abigail/t/input-2-2 @@ -0,0 +1,23 @@ +top +min +push 1 +push 2 +push 3 +push 4 +min +pop +min +top +push -3 +push -2 +push -1 +min +pop +pop +pop +pop +pop +pop +pop +pop +pop diff --git a/challenge-095/abigail/t/output-2-1.exp b/challenge-095/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..343ee5c2f6 --- /dev/null +++ b/challenge-095/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +-1 +-1 diff --git a/challenge-095/abigail/t/output-2-2.exp b/challenge-095/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..67f67b4693 --- /dev/null +++ b/challenge-095/abigail/t/output-2-2.exp @@ -0,0 +1,6 @@ +Stack is empty +Stack is empty +1 +1 +3 +-3 -- cgit