3

I'm trying to convert the following nested for loops which constructs and prints a list into a recursive function as it would greatly improve the runtime of it. However, I'm having difficulty with this. How could I go about converting it with so many layers of nested loops?

for v in range(-10, 11, 1):
     for w in range(-10, 11, 1):
            for x in range(-10, 11, 1):
                for y in range(-10, 11, 1):
                    for z in range(-10, 11, 1):
                        print([v, w, x, y, z])
8
  • Minor typo in the first line: for (v in range(-10, 11, 1)) should be for v in range(-10, 11, 1) I presume. Your point is clear nevertheless. Commented Feb 15, 2018 at 0:22
  • 4
    While recursion does have some advantages over iteration, performance is generally not one of them. Especially not in Python, which by default, doesn't optimize tail recursion. Is there any other reason you need this to be in the form of a recursive function? Commented Feb 15, 2018 at 0:22
  • 1
    @mypetlion Honestly, I'm just trying to see if I'd get any performance advantages as well as it being a personal challenge. Commented Feb 15, 2018 at 0:24
  • What would be an example output? Commented Feb 15, 2018 at 0:32
  • 1
    @SebastienD [-4, 0, 7, 1, 7] Commented Feb 15, 2018 at 0:34

3 Answers 3

4

As @mypetlion mentioned above, recursion in Python might not give you the performance boost you are looking for.

What about using itertools.product? I haven't benchmarked it compared to your original but it does seem to be implemented in C.

from itertools import product

for p in product(range(-10, 11), repeat=5):
    print(p)
Sign up to request clarification or add additional context in comments.

1 Comment

I was about to mention the same solution. According to the documentation "This function (...) does not build up intermediate results in memory", so it is a perf win. Moreover, if there was something else to optimize, I'm pretty sure they'd do that.
1

If I understood well, this could be a combination with replacement function done with itertools:

import itertools
import numpy as np

comb =  list(itertools.combinations_with_replacement(range(-10,11,1), 5))

for x in comb:
    print  np.asarray(x)

3 Comments

Still doesn't match the output of the original. Your last two lines: [ 9 10 10 10 10]; [10 10 10 10 10] VS Original last two lines: [10, 10, 10, 10, 9]; [10, 10, 10, 10, 10]. If you just run the original code you will see that the pattern does not match the output of combinations_with_replacement.
Alright I see, the output order matters this is it?
This link shows the last few lines of your output VS the original codes output (the outputs are totally different): bpaste.net/show/bbb6497d707d
0

You can create default list in the function signature to store the elements you have currently accumulated, and once the length of the list equals the desired amount, yield the current list:

def combinations(count, args, current = []): 
    if len(current) == count:
       yield current
    else:
       for i in range(*args):
         yield from combinations(count, args, current + [i])

print(list(combinations(5, [-10, 11])))

2 Comments

You're using both iteration and recursion here, which is not what the question asked for. Your print statement also doesn't produce the desired output.
@mypetlion The OP does not specifically state that all loops were to be eliminated from the recursion. However, I did fix the print statement so that the generator expression will be forced to retrieve all combinations in memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.