PARANTHESIS CHECKER IMPLEMENTATION USING PYTHON - STACK APPLICATON👩💻👨💻
In this post, we are going to see one of the stack application's implementations which is a parenthesis checker and also the stack has several other applications like
🎯 Reverse a string,
🎯 Undo mechanism in editors,
🎯 Recursion,
🎯 Expression conversion, and so on.
Here, I am using the 'queue' module to implement the stack in python. You can implement the stack without using the 'queue' module and you can check out herex
Code😃 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import queue | |
def bracketChecker(x): | |
stack = queue.LifoQueue() | |
def isOpen(data): | |
if data == '{': | |
return True | |
if data == '(': | |
return True | |
if data == '[': | |
return True | |
return False | |
def isMatching(data1, data2): | |
if data1 == '{' and data2 == '}': | |
return True | |
if data1 == '(' and data2 == ')': | |
return True | |
if data1 == '[' and data2 == ']': | |
return True | |
return False | |
for i in x: | |
if isOpen(i): | |
stack.put(i) | |
else: | |
if stack.empty(): | |
return False | |
j = stack.get() | |
if isMatching(j, i): | |
continue | |
else: | |
return False | |
if stack.empty(): | |
return True | |
else: | |
return False | |
x = '{([])}' | |
if bracketChecker(x): | |
print('Given string of paranthesis is balanced') | |
else: | |
print('Given string of paranthesis is not balanced') |
I hope it will help you😊
Comments