python - How to rewrite a recursive function to use a loop instead? -


This stack overflow thread claims that each recursive function can be written as a loop.

It makes a complete sense but I am not sure how the following recursive function should be expressed as a loop because it has a preceding part of the logic and a rational post.

Obviously the solution can not use the Goto statement code here:

  def gen_perms (lst, k, m): if k == m: all_perms Append (list (lst)) Other: xrange (k, m +1): #swap four tmp = lst [k] lst [k] = lst [i] lst [i] = tmp gen_perms (lst, k + 1, M) #swap four tmp = lst [k] lst [k]] = lst [i] lst [i] = tmp   

Using these would be like this: < Pre> all_perms = [] gen_perm ([1, 2], 3], 0, 2)

and this list generates every order of 1,2,3 .

For (j = 0; j & lt; size; j ++) for (k = 0; k & lt; size; k ++) if (i! = J & J! = C & amp; ; I! = K) printf ("% d% d% d \ n", list [i], list [j], list [k]);

Comments