Posted on:

15 Nov 2022

1

My Code Solution

I tried to solve it before watching at your solution and it works but i don't think that this is the best way to do it ! here's my code :
li_2 = ['{','(','[','{',']',']',')','}']
check = False
opening_curly_bracket = '{'
opening_parenth = '('
opening_bracket = '['
closing_curly_bracket = '}'
closing_parenth = ')'
closing_bracket = ']'

for i in range(len(li_2)//2):
    last_elt= li_2[-1]
    li_2.pop()
    temp_li = li_2[i:]
    while len(temp_li) != 1:
        temp_li.pop()

if (temp_li[0] == opening_curly_bracket) and (last_elt == closing_curly_bracket) :
        check = True
    elif (temp_li[0] == opening_bracket) and (last_elt == closing_bracket):
        check = True
    elif (temp_li[0] == opening_parenth) and (last_elt == closing_parenth):
        check = True
    else :
        check=False
        break
print(check)

0 answers ( 0 marked as helpful)

Submit an answer