def p(e): for o in"+-*/": for i,c in enumerate(e): if(c==o)*(0==sum([(d=="(")-(d==")")for d in e[:i]])):return"("+p(e[:i])+o+p(e[i+1:])+")" return e