-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemi-project-3.py
executable file
·64 lines (50 loc) · 1.33 KB
/
semi-project-3.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
{
"Author": "Fenil Gandhi",
"Version": "Python 3.6.2",
"Description":
'''Write a program to determine whether or not a character string has an unmatched parenthesis.
Use a stack. What is the time complexity of your program? Can we replace the stack with a
queue? ''',
}
import time
import random
def timeit(func):
def decorated_function(*args):
a = time.time()
func(*args)
b = time.time()
return (b - a)
return decorated_function
class Stack():
def __init__(self):
self.memory = []
def push(self):
self.memory += [0]
def pop(self):
return self.memory.pop()
def length(self):
return len(self.memory)
def generatestr(length):
chars = "[]+-<>"
return "".join([chars[random.randint(0,6)] for _ in range(length)])
@timeit
def solve(string):
print ("\nSolving " , string)
# Solve using Stack
stack = Stack()
try:
for c in string:
print(c, stack.memory)
if c == "[":
stack.push()
elif c == "]":
stack.pop()
if stack.length() > 0:
raise IndexError
except:
print("Imbalanced Equation")
else:
print("It is perfectly balanced")
if __name__ == '__main__':
s = generatestr(50)
solve(s)