I'm solving HackerRank "Stacks: Balanced Brackets" in Python.
A bracket is considered to be any one of the following characters:
(
,)
,{
,}
,[
, or]
.Two brackets are considered to be a matched pair if the an opening bracket (i.e.,
(
,[
, or{
) occurs to the left of a closing bracket (i.e.,)
,]
, or}
) of the exact same type. There are three types of matched pairs of brackets:[]
,{}
, and()
.A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example,
{[(])}
is not balanced because the contents in between{
and}
are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket,(
, and the pair of parentheses encloses a single, unbalanced closing square bracket,]
.By this logic, we say a sequence of brackets is considered to be balanced if the following conditions are met:
It contains no unmatched brackets. The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets. Given strings of brackets, determine whether each sequence of brackets is balanced. If a string is balanced, print
YES
on a new line; otherwise, printNO
on a new line.
My code:
def is_matched(expression):
if len(expression) % 2 != 0:
return False
opening = ("(", "[", "{")
closing = (")", "]", "}")
mapping = {opening[0]:closing[0],
opening[1]:closing[1],
opening[2]:closing[2]}
if expression[0] in closing:
return False
if expression[-1] in opening:
return False
closing_queue = []
for letter in expression:
if letter in opening:
closing_queue.append(mapping[letter])
elif letter in closing:
if not closing_queue:
return False
if closing_queue[-1] == letter:
closing_queue.pop()
else:
return False
return True
t = int(input().strip())
for a0 in range(t):
expression = input().strip()
if is_matched(expression) == True:
print("YES")
else:
print("NO")
What could be improved here? What can be done to make my code more idiomatic (pythonic code)?
Is my implementation bad? Why is it?