EP06 - Python Stack - Balanced Parentheses Checker
HTML-код
- Опубликовано: 24 ноя 2021
- #Python #DataStructures #Stacks
Welcome to the Python Stacks tutorial.
Following is the repository of the code used in this episode
github.com/ashwin-pajankar/Py... Наука
Super amazing explanation
stack is not defined error
It will not work for nested parenthesis
Guys, I use the python strings approach. Very simple and effective.
Pls share the code
Hi @@swethag040 I'll try to find it at my computer at home and share it here.
Hey brother @@swethag040 , I am preparing a video to show my solution. I am newbie to this RUclips production thing so that is taking time to have the nerves to do it, but I promisse the solution is interestingly simpler. As soon as it is ready I'll let you know it here under this comment.
Sir but if won't work let's say ({) } it is actually balanced still it shows False
it's because we are popping { but char will )
Above expression is not balanced..
Type of bracket opened at last should be closed first
wow
Can I do it using dict??
Yes you can.
openingparenthesis = '[{('
closingparenthesis = ')}]'
openingList = []
exp = '(){'
pairs = {')':'(', '}':'{',']':'['}
def checkParenthesis(exp):
for i in exp:
if i in openingparenthesis:
openingList.append(i)
elif i in closingparenthesis:
if openingList==[]:
return False
top_char = openingList.pop()
if pairs[i] != top_char:
return False
if openingList== []:
return True
else:
return False
print(checkParenthesis(exp))