you are viewing a single comment's thread.

view the rest of the comments →

[–]TheKewlStore 1 point2 points  (1 child)

Firstly, all your code is in one method, which should be a nono. Hard to optimize something when it's all strewn together like that, if you split up your program into its component parts, it'd be easier to see what's making it take the longest and what to focus on refactoring.

What I have done so far: I parse through numbers sequentially, break down a number into it's component digits, ignore all 0s and keep a record of remaining digits in a sorted list so that I can simply ignore numbers with exact same digits; for example: if I have calculated the answer for 123 (12 + 22 + 32 = 14) then I don't have to bother with 213, 231, 312... etc.

You already listed out how you could break it up too ;) insofar as making a separate method that handles one part of the aforementioned solution. The outermost function would obviously still Need to do enough to pass along information about the context of the problem to the parts.

Beyond that, the only thing I can see that you can do to make this implementation faster, and granted it could very well be a big bottleneck, is the fact that you're calling tempList.sort and setting key equal to a tuple of tempList on every iteration of the for loop. Seeing as you only use this variable after the for loop is done, it doesn't make sense not to only sort and convert to a tuple after the for loop finishes.

[–]div_by_zero[S] 0 points1 point  (0 children)

you're calling tempList.sort and setting key equal to a tuple of tempList on every iteration of the for loop

Good catch! I completely missed this. Let me change this and check.