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))
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" )
Comments
Leave a comment