• Slow code: how to fix?

Question related to mission Mathematically Lucky Tickets


So, I solved this mission locally on my PC, but the problem is that the code is slow. A full check to verify a "True" solution takes my PC about 60 seconds (and my PC is faster than checkio).

What my code is currently doing:

  1. find all possible ways to split the number (e.g. '123456' into ['123','456'] and ['12', '3', '456'] etc)
  2. for each split, construct the expression string by adding the parts together one by one with an operator in between every time, as well as either putting brackets or not putting brackets
  3. evaluate the constructed expression string using eval()

The problem is that checking every possible expression this way simply takes too much time. So what am I missing? Is there a smart way to somehow not have to check every possible expression? Or are my coding skills too bad and should I be able to write faster code?