Posted on:

25 Jan 2023

0

Here is my approach.

This isn't the best and most aesthetic way to do it but it works for string with 6 brackets so I would like to share my solution. I would love to receive some tips to improve my code.

def brackets_order_check(brackets):

stack1 = []

pair1 = '()'

pair2 = '{}'

pair3 = '[]'

pairs_brackets = [pair1, pair2, pair3]

for i in range(len(brackets)):

if brackets[i] == '[' or brackets[i] == '(' or brackets[i] == '{':
            stack1.append(brackets[i])
        else:
            Bpair = f'{stack1[-1]}{brackets[i]}'
            for p in pairs_brackets:
                if p == Bpair:
                    stack1.pop()
                else:
                    print(Bpair)

if not stack1:

return True

else:

return False

0 answers ( 0 marked as helpful)

Submit an answer