Although recursion is nice here, the current invert routine in solve uses a while loop which has its own elegance without the overhead of python function calls.