Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions BalancedBraces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#it can check if your all braces is balanced or not like {[()]} is balanced
#and [{]} is not balanced
p=input("Input Your paragraph :")
symbollist=["{","}","[","]","(",")"]
opensymbol=["[","{","("]
closesymbol=["]","}",")"]
templist=[]
for word in p:
for char in word:
if char in symbollist:
templist.append(char)
def check():
global templist
tlen=len(templist)
if tlen==0:
return 0
elif(tlen==1):
return
for i in range(len(templist)):
#item
try:
I=templist[i]
except:
break
if I in opensymbol:
continue
#previousitem
pI=templist[i-1]
if I in closesymbol and i==0:
break
if I in closesymbol:
op=closesymbol.index(I)
#oppositeitem
opI=opensymbol[op]
if pI==opI:
templist.pop(i)
templist.pop(i-1)
else:
print("not Balanced")
return
check()
check()
if len(templist)==0:
print("balanced")