aboutsummaryrefslogtreecommitdiff
path: root/challenge-095/paulo-custodio/python/ch-2.py
blob: 11d779e09998b27ffd6c39104d0ac59002be1806 (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
#!/usr/bin/env python

# Challenge 095
#
# TASK #2 › Demo Stack
# Submitted by: Mohammad S Anwar
# 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

class Stack:
    def __init__(self):
        self.data = []
    def __repr__(self):
        return str(self.data)
    def push(self, n):
        self.data.append(n)
    def pop(self):
        self.data.pop()
    def top(self):
        return self.data[-1]
    def min(self):
        return min(self.data)

stack = Stack()
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