Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
7-liner: short and fast solution in Clear category for Remove Brackets by Stensen
def remove_brackets(line):
m, match, count = [], [], {i: line.count(i) for i in '[{()}]'}
for i, b in enumerate(line):
if b == '(' and count[')'] >= m.count(b) + 1: m.extend([b, i])
elif b in '[{' and count[chr(ord(b)+2)] >= m.count(b) + 1: m.extend([b, i])
elif b in ")}]" and m and abs(ord(b) - ord(m[-2])) <= 2: match.extend([(i, b), (m.pop(), m.pop())])
return ''.join([i for _, i in sorted(match)])
Oct. 5, 2020