From f8c4073a75a8f1af7fbeb2e2186bb6f618620ac7 Mon Sep 17 00:00:00 2001 From: Chaitanya Desai <71583721+itsmekrrish@users.noreply.github.com> Date: Mon, 5 Oct 2020 19:54:42 +0530 Subject: [PATCH] Create BalancedBraces.py --- BalancedBraces.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 BalancedBraces.py diff --git a/BalancedBraces.py b/BalancedBraces.py new file mode 100644 index 0000000..15beecf --- /dev/null +++ b/BalancedBraces.py @@ -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")