Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Third solution in Creative category for Brackets by astynax84
# migrated from python 2.7
from functools import reduce
# Fython :)
# Check expression for correct brackets order
checkio = lambda s: reduce(lambda (f, stk), x:
((False, None) if not f else (
(f, ({"[":"]", "{":"}", "(":")"}[x], stk))
if x in "[({" else (
(f, stk) if x not in "]})" else (
(False, None)
if stk is None or x != stk[0] else
(True, stk[1])
)
)
)), s, (True, None)) == (True, None)
if __name__ == '__main__':
assert checkio('({(asda)sd[s]d})') == True, 'First'
assert checkio('({[a]((s)})') == False, 'Second'
print('All ok')
March 4, 2014