Jul-16-2021, 12:25 PM
(This post was last modified: Jul-16-2021, 12:33 PM by Yoriz.
Edit Reason: Added code tags
)
Hi! taking a course on coursera - Mathematical Thinking in Computer Science
There is an example of creating a permutation and I am having porblems understanding why .pop taking out 2 items from the list at once. Can somebody explain it to me as I already spent 5 days trying to understand and seems like it is impossible with my current knowledges.
(I've added print() codes trying to understand what happens)
There is an example of creating a permutation and I am having porblems understanding why .pop taking out 2 items from the list at once. Can somebody explain it to me as I already spent 5 days trying to understand and seems like it is impossible with my current knowledges.
(I've added print() codes trying to understand what happens)
n=4 perm=[] def generate_permuations(perm,n): print("BEGIN of fuction-----") if len(perm)==n: print("4 digits REACHED!!!---",perm) return else: print("4 DIGITS not reached") for k in range(n): print("K first in for",k) if k in perm: print("K In perm!!!!") if k not in perm: print("K not in perm!!!!",k) print("Begin of for until 4 digits----") perm.append(k) print("Appended perm---",perm) print("Before_perm--",perm) print("Before_k--", k) generate_permuations(perm,n) print("begin POP") print("Before_POP_perm--",perm) print("Before_POP_k--", k) perm.pop() print("After_pop_Test-perm---", perm) print("After_pop_Test-k---", k) # print("n--------",k) generate_permuations(perm,n)Part of output with thing that I don't understand - why .pop happens twice and K coming back to 2 after being 3:
Output:begin POP
Before_POP_perm-- [0, 1, 2, 3]
Before_POP_k-- 3
After_pop_Test-perm--- [0, 1, 2]
After_pop_Test-k--- 3
begin POP
Before_POP_perm-- [0, 1, 2]
Before_POP_k-- 2
After_pop_Test-perm--- [0, 1]
After_pop_Test-k--- 2
Thanks in advance!!