Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Remove Brackets by lechrudik
def remove_brackets(string):
res, one, para = [], [], []
open_c, close_c = '([{', ')]}'
for c, i in enumerate(string):
if i in close_c:
if c in para:
res.append(i)
if i in open_c:
count = 0
for k in range(c + 1, len(string)):
if i == string[k]:
count += 1
if string[k] == close_c[open_c.index(i)]:
if not count:
res.append(i)
para.append(k)
one.append([c, k])
break
else:
count -= 1
delete = []
for x, i in enumerate(one):
for y, c in enumerate(one):
if x == y:
break
mas_i = [_ for _ in range(i[0], i[1] + 1)]
mas_c = [_ for _ in range(c[0], c[1] + 1)]
big, smole = sorted((mas_i, mas_c), key=len, reverse=True)
if set(mas_i).isdisjoint(set(mas_c)) or set(smole) <= set(big):
pass
else:
delete.extend(c)
res = [i for c, i in enumerate(res) if c not in delete]
return ''.join(res)
if __name__ == '__main__':
print("Example:")
print(remove_brackets("[(])"))
# exit()
# These "asserts" are used for self-checking and not for an auto-testing
assert remove_brackets('(()()') == '()()'
assert remove_brackets('[][[[') == '[]'
assert remove_brackets('[[(}]]') == '[[]]'
assert remove_brackets('[[{}()]]') == '[[{}()]]'
assert remove_brackets('[[[[[[') == ''
assert remove_brackets('[[[[}') == ''
assert remove_brackets('') == ''
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 14, 2021
Comments: