class Stack:
def __init__(self):
self.items=[]
def isEmpty(self):
return self.items==[]
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
s=Stack()
p=Stack()
r=Stack()
f=input("Enter file name:")
file=open(f,"r").read()
sc=0
rc=0
pc=0
for symbol in file:
if symbol=="[":
s.push(symbol)
elif symbol=="]":
if s.size()>0:
s.pop()
sc=+1
print("All [] matched.")
else:
print("Not all [] matched")
elif symbol=="(":
r.push(symbol)
elif symbol==")":
if r.size()>0:
r.pop()
rc=+1
print("All () matched.")
else:
print("Not all () matched")
elif symbol=="{":
p.push(symbol)
elif symbol=="}":
if p.size()>0:
p.pop()
pc=+1
print("All {} matched.")
else:
print("Not all {} matched")
print(sc)
print(rc)
print(pc)
Heyy I have this code and its returning this:
Enter file name:test1.py
Not all {} matched
All () matched.
All [] matched.
All [] matched.
All () matched.
All {} matched.
1
1
1
I want
Enter the file name: test1.py
() pairs: 2
{} pairs: 1
[] pairs: 2
All () matched.
Not all {} matched.
All [] matched.
What am I doing wrong? Its not even counting properly.