-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw02.py
121 lines (99 loc) · 2.44 KB
/
hw02.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
if self.items:
return self.items.pop()
else:
print("pop() error: Stack is empty.")
return None
def peek(self):
if self.items:
return self.items[-1]
else:
print("peek() error: Stack is empty.")
return None
def size(self):
return len(self.items)
"""
1. postfixEval
Input type: a list of strings
Output type: a floating point number
"""
def postfixEval(list):
s = Stack()
total = 0
for n in list:
if n != "+" and n!= "-" and n!= "*" and n!= "/":
s.push(n)
total = n
if n == "+":
a = float(s.pop())
b = float(s.pop())
total = (a + b)
s.push(total)
if n == "-":
c = float(s.pop())
d = float(s.pop())
total = (d - c)
s.push(total)
if n == "*":
e = float(s.pop())
f = float(s.pop())
total = (f * e)
s.push(total)
if n == "/":
g = float(s.pop())
h = float(s.pop())
total = (h / g)
s.push(total)
return float(total)
"""
2. validParentheses
Input type: a string
Output type: a Boolean
"""
def validParentheses(string):
s = Stack()
for ch in string:
if ch == "(" or ch == "[" or ch == "{":
s.push(ch)
if ch == ")":
if not s.isEmpty():
a = s.pop()
if a != "(":
return False
else:
return False
if ch == "]":
if not s.isEmpty():
b = s.pop()
if b != "[":
return False
else:
return False
if ch == "}":
if not s.isEmpty():
c = s.pop()
if c != "{":
return False
else:
return False
return s.isEmpty()
"""
3. reverseString
Input type: a string
Output type: a string
"""
def reverseString(string):
stack1 = Stack()
new_string = ""
for c in string:
stack1.push(c)
for c in string:
new_string += stack1.pop()
return new_string