-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
executable file
·90 lines (73 loc) · 1.9 KB
/
Copy pathstack.py
File metadata and controls
executable file
·90 lines (73 loc) · 1.9 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
'''
$Id$
stack.py -- simulate stack data structures using lists
Exercises:
13-8) create a Stack class
13-10) create a class similar to arrays in Perl which have
both queue- and stack-like qualities and features
'''
# create our data structure
stack = []
#
# pushit() -- adds stirng to the stack
#
def pushit():
stack.append(raw_input('Enter new string: '))
#
# popit() -- removes stirng from the stack
#
def popit():
if len(stack) == 0:
print 'Cannot pop from an empty stack!'
else:
print 'Removed [', stack.pop(), ']'
#
# viewstack() -- display stack contents
#
def viewstack():
print str(stack)
#
# showmenu() -- interactive portion of application
# displays menu to prompt user and takes
# action based on user response
#
def showmenu():
# using triple quotes to help us put together
# the multi-line string prompt to display
prompt = """
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice: """
# loop until user quits
done = 0
while not done:
# loop until user choses valid option
chosen = 0
while not chosen:
# if user hits RETURN/Enter, ^C, or ^D (EOF),
# pretend they typed 'q' to quit normally
try:
choice = raw_input(prompt)[0]
except (IndexError, EOFError, KeyboardInterrupt):
choice = 'q'
print '\nYou picked: [%s]' % choice
# validate option chosen
if choice not in 'uovq':
print 'invalid option, try again'
else:
chosen = 1
# take appropriate action
if choice == 'q':
done = 1
if choice == 'u':
pushit()
if choice == 'o':
popit()
if choice == 'v':
viewstack()
# run showmenu() as the application
if __name__ == '__main__':
showmenu()