-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_as_array.py
More file actions
44 lines (36 loc) · 946 Bytes
/
stack_as_array.py
File metadata and controls
44 lines (36 loc) · 946 Bytes
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
# http://quiz.geeksforgeeks.org/stack-set-1/
import os
import sys
import operator
import csv
import itertools
import math
import collections
from itertools import groupby
from sys import argv
from operator import itemgetter, attrgetter, methodcaller
from sys import maxsize
def createStack():
stack = []
return stack
def isEmpty(stack):
return len(stack) == 0
def push(stack, item):
stack.append(item)
print ("Item Pushed:"+item)
def pop(stack):
if(isEmpty(stack)):
return str(-maxsize-1)
return stack.pop()
def peek(stack):
if(isEmpty(stack)):
return str(-maxsize-1)
return stack[len(stack)-1]
def main():
stack = createStack()
push(stack, str(10))
push(stack, str(20))
push(stack, str(30))
print(pop(stack) + " popped from stack")
print("Top item is " + peek(stack))
main()