Good afternoon friends! The task is to write code that checks whether brackets are correctly placed in the expression, i.e.
("(5+5)/[4+4]*{2*2}" - True, "(3+[2*3)]" - False .
I wrote the code, to be honest, of course it's a shame to show it, but alas, no one except you will make me smarter. Please - tell me how you can make of this, the normal code? The code itself works.
def checkio(expression): list_backets = [] for i in expression: if i == "[" or i == "]" or i == "(" or i == ")" or i == "{" or i == "}": list_backets.append(i) //создаю список нужных нам элементов if len(list_backets) == 0: //если нет элементов вовсе return True elif list_backets.count("[") == list_backets.count("]") and list_backets.count("(") == list_backets.count(")") and list_backets.count("{") == list_backets.count("}"): //если количество "(" равно количеству ")" и так далее... j = 0 try: while j != range(len(list_backets)): //попытка найти в списке соседние скобки if list_backets[j] == "(" and list_backets[j+1] == ")": list_backets.pop(j) list_backets.pop(j) j = 0 elif list_backets[j] == "{" and list_backets[j+1] == "}": list_backets.pop(j) list_backets.pop(j) j = 0 elif list_backets[j] == "[" and list_backets[j+1] == "]": list_backets.pop(j) list_backets.pop(j) j = 0 else: j += 1 except: if (len(list_backets)) == 0: return True else: return False else: return False It looks dumb and clumsy, if you have the opportunity and time, tell us how to make it more "beautiful" with explanations. Thank!