Apologies in advance as this question has been repeated ad nauseum.
I've banged my head for 3+ hours on what should be simple, but just cannot figure this out. If anyone can tell me what's going wrong I'll grant you naming rights to my first born.
outputs = []
def permutation_helper(available_list, slate):
if len(available_list) == 0:
outputs.append(slate)
return
for num_idx in range(len(available_list)):
slate.append(available_list[num_idx])
if num_idx == len(available_list) - 1:
permutation_helper(available_list[0:num_idx], slate)
else:
permutation_helper(available_list[0:num_idx] + available_list[num_idx+1:], slate)
slate = slate[0:len(slate)-1]
slate = []
nums = [1,2,3]
permutation_helper(nums, slate)
print(outputs)
Instead of the normal set of permutations, the result is
[[1, 2, 3], [1, 2, 3, 2], [1, 2, 2, 1, 3], [1, 2, 2, 1, 3, 1], [1, 2, 2, 1, 3, 1, 2], [1, 2, 2, 1, 3, 1, 2, 1]]
I added some print statements when the slate is changed, and it seems like it is being changed two times from [1,2,3] to [1,2] instead of just once.
Code with debugging statements
outputs = []
def permutation_helper(available_list, slate):
print("Starting with available list: " + str(available_list) + "and slate: " + str(slate))
if len(available_list) == 0:
print("Adding to output")
outputs.append(slate)
return
for num_idx in range(len(available_list)):
print("Starting iteration")
print("Appending " + str(available_list[num_idx]) + " to slate of: " + str(slate))
slate.append(available_list[num_idx])
if num_idx == len(available_list) - 1:
print("Calling last")
permutation_helper(available_list[0:num_idx], slate)
else:
print("Calling default")
permutation_helper(available_list[0:num_idx] + available_list[num_idx+1:], slate)
print("Slate was: " + str(slate) + " and will be: "+ str(slate[0:len(slate)-1]))
slate = slate[0:len(slate)-1]
print("Ending iteration with available list of " + str(available_list))
print("Ending call for available list: " + str(available_list))
slate = []
nums = [1,2,3]
permutation_helper(nums, slate)
Output
Starting with available list: [1, 2, 3]and slate: []
Starting iteration
Appending 1 to slate of: []
Calling default
Starting with available list: [2, 3]and slate: [1]
Starting iteration
Appending 2 to slate of: [1]
Calling default
Starting with available list: [3]and slate: [1, 2]
Starting iteration
Appending 3 to slate of: [1, 2]
Calling last
Starting with available list: []and slate: [1, 2, 3]
Adding to output
Slate was: [1, 2, 3] and will be: [1, 2]
Ending iteration with available list of [3]
Ending call for available list: [3]
Slate was: [1, 2, 3] and will be: [1, 2]
Ending iteration with available list of [2, 3]
Starting iteration
Appending 3 to slate of: [1, 2]
Calling last
Starting with available list: [2]and slate: [1, 2, 3]
Starting iteration
Appending 2 to slate of: [1, 2, 3]
Calling last
Starting with available list: []and slate: [1, 2, 3, 2]
Adding to output
Slate was: [1, 2, 3, 2] and will be: [1, 2, 3]
Ending iteration with available list of [2]
Ending call for available list: [2]
Slate was: [1, 2, 3, 2] and will be: [1, 2, 3]
Ending iteration with available list of [2, 3]
Ending call for available list: [2, 3]
Slate was: [1, 2, 3] and will be: [1, 2]
Ending iteration with available list of [1, 2, 3]
Starting iteration
Appending 2 to slate of: [1, 2]
Calling default
Starting with available list: [1, 3]and slate: [1, 2, 2]
Starting iteration
Appending 1 to slate of: [1, 2, 2]
Calling default
Starting with available list: [3]and slate: [1, 2, 2, 1]
Starting iteration
Appending 3 to slate of: [1, 2, 2, 1]
Calling last
Starting with available list: []and slate: [1, 2, 2, 1, 3]
Adding to output
Slate was: [1, 2, 2, 1, 3] and will be: [1, 2, 2, 1]
Ending iteration with available list of [3]
Ending call for available list: [3]
Slate was: [1, 2, 2, 1, 3] and will be: [1, 2, 2, 1]
Ending iteration with available list of [1, 3]
Starting iteration
Appending 3 to slate of: [1, 2, 2, 1]
Calling last
Starting with available list: [1]and slate: [1, 2, 2, 1, 3]
Starting iteration
Appending 1 to slate of: [1, 2, 2, 1, 3]
Calling last
Starting with available list: []and slate: [1, 2, 2, 1, 3, 1]
Adding to output
Slate was: [1, 2, 2, 1, 3, 1] and will be: [1, 2, 2, 1, 3]
Ending iteration with available list of [1]
Ending call for available list: [1]
Slate was: [1, 2, 2, 1, 3, 1] and will be: [1, 2, 2, 1, 3]
Ending iteration with available list of [1, 3]
Ending call for available list: [1, 3]
Slate was: [1, 2, 2, 1, 3] and will be: [1, 2, 2, 1]
Ending iteration with available list of [1, 2, 3]
Starting iteration
Appending 3 to slate of: [1, 2, 2, 1]
Calling last
Starting with available list: [1, 2]and slate: [1, 2, 2, 1, 3]
Starting iteration
Appending 1 to slate of: [1, 2, 2, 1, 3]
Calling default
Starting with available list: [2]and slate: [1, 2, 2, 1, 3, 1]
Starting iteration
Appending 2 to slate of: [1, 2, 2, 1, 3, 1]
Calling last
Starting with available list: []and slate: [1, 2, 2, 1, 3, 1, 2]
Adding to output
Slate was: [1, 2, 2, 1, 3, 1, 2] and will be: [1, 2, 2, 1, 3, 1]
Ending iteration with available list of [2]
Ending call for available list: [2]
Slate was: [1, 2, 2, 1, 3, 1, 2] and will be: [1, 2, 2, 1, 3, 1]
Ending iteration with available list of [1, 2]
Starting iteration
Appending 2 to slate of: [1, 2, 2, 1, 3, 1]
Calling last
Starting with available list: [1]and slate: [1, 2, 2, 1, 3, 1, 2]
Starting iteration
Appending 1 to slate of: [1, 2, 2, 1, 3, 1, 2]
Calling last
Starting with available list: []and slate: [1, 2, 2, 1, 3, 1, 2, 1]
Adding to output
Slate was: [1, 2, 2, 1, 3, 1, 2, 1] and will be: [1, 2, 2, 1, 3, 1, 2]
Ending iteration with available list of [1]
Ending call for available list: [1]
Slate was: [1, 2, 2, 1, 3, 1, 2, 1] and will be: [1, 2, 2, 1, 3, 1, 2]
Ending iteration with available list of [1, 2]
Ending call for available list: [1, 2]
Slate was: [1, 2, 2, 1, 3, 1, 2] and will be: [1, 2, 2, 1, 3, 1]
Ending iteration with available list of [1, 2, 3]
Ending call for available list: [1, 2, 3]
[[1, 2, 3], [1, 2, 3, 2], [1, 2, 2, 1, 3], [1, 2, 2, 1, 3, 1], [1, 2, 2, 1, 3, 1, 2], [1, 2, 2, 1, 3, 1, 2, 1]]