Answer to Question #202027 in Python for reeeg

Question #202027

Can you please rewrite the code, without using def.

opening_list = ["[", "{", "("]

closing_list = ["]", "}", ")"]

def parenthesis(str):

stack = []

 for j in str:

if j in opening_list:

stack.append(j)

 elif j in closing_list:

 position = closing_list.index(j)

  if ((len(stack) > 0) and

=  (opening_list[position] == stack[len(stack) - 1])):

 stack.pop()

else:

return "Unbalance"

if len(stack) == 0:

return "Balance"

else:

 return "Unbalance"

string = input("Enter the set of parenthesis: \n")  

print(string, "-", parenthesis(string))   


1
Expert's answer
2021-06-02T03:03:22-0400
opening_list = ["[", "{", "("]


closing_list = ["]", "}", ")"]
strings = input("Enter the set of parenthesis: \n")



stack = []


for j in strings:


    if j in opening_list:


        stack.append(j)


    elif j in closing_list:


        position = closing_list.index(j)


        if ((len(stack) > 0) and (opening_list[position] == stack[len(stack) - 1])):


            stack.pop()



if len(stack) == 0:


    print(strings, "- Balance")


else:


    print( strings,"- Unbalance" )  

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS