aboutsummaryrefslogtreecommitdiff
path: root/challenge-095/perlboy1967/perl/ch-2.pl
blob: 49ece51f5c06eff59a9ea46e25bd8a2812d28c7f (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl

# Perl Weekly Challenge - 095
# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/
#
# Task 1 - Palindrome Number
#
# Author: Niels 'PerlBoy' van Dijke

use v5.16;
use strict;
use warnings;

use Data::Printer;

# Unbuffered STDOUT
$|++;


my $stack = Stack->new;
$stack->push(2);
$stack->push(-1);
$stack->push(0);
$stack->pop;       # removes 0
printf "top: %d\n", $stack->top; # prints -1
p $stack;

$stack->push(0);
printf "min: %d\n", $stack->min; # prints -1
printf "max: %d\n", $stack->max; # prints 2
p $stack;


package Stack;

use List::Util;

sub new {
 my ($class) = @_;

  bless [], $class;
}

sub push {
  my ($this, $value) = @_;

  CORE::push(@$this, $value);
}

sub pop {
  my ($this) = @_;

  CORE::pop(@$this);
}

sub top {
  my ($this) = @_;

  return $this->[-1];
}

sub min {
  my ($this) = @_;

  return List::Util::min(@$this);
}

sub max {
  my ($this) = @_;

  return List::Util::max(@$this);
}