I am converting some of the recursive calls in our code base to iterations. This has been pretty straightforward, thanks to this blog, and this question. However, there is the following pattern (as a minimal example), which is giving me a hard time. It basically gives n^m
permutations (with repetition) of n
numbers in m
positions (here n=4
, m=3
):
def f (i, arr):
if i < len(arr):
for j in [2,3,5,8]:
arr[i] = j
f(i+1, arr)
else:
print(arr)
f(0, [0,0,0])
which outputs:
[2, 2, 2]
[2, 2, 3]
[2, 2, 5]
...
[8, 8, 3]
[8, 8, 5]
[8, 8, 8]
According to this discussion, this should be possible. It would be great if someone can share some guidance as to how to proceed with this conversion?