![]() |
Need help understanding .pop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Need help understanding .pop (/thread-34298.html) |
Need help understanding .pop - Akulinyak - Jul-16-2021 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) 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: Thanks in advance!!
RE: Need help understanding .pop - Marbelous - Jul-16-2021 You have your generate_permutations() function called (on line 22) inside of itself. This is called recursion and it's very useful is some situations. I'm not really sure what this code is trying to achieve with it though. Generally, code to "generate permutations" would take a list and return all the possible combinations of its elements. https://www.w3schools.com/python/gloss_python_function_recursion.asp https://www.pythonpool.com/python-permutations/ BTW, there is a permutations function in the itertools module you might want to look at. ![]() RE: Need help understanding .pop - Akulinyak - Jul-16-2021 (Jul-16-2021, 02:12 PM)Marbelous Wrote: You have your generate_permutations() function called (on line 22) inside of itself. This is called recursion and it's very useful is some situations. I'm not really sure what this code is trying to achieve with it though. Generally, code to "generate permutations" would take a list and return all the possible combinations of its elements.Thanks a lot. I guess it's somehow related to a situtation when the funciton calls itself. The task was to generate all possible permutations of 0, 1, 2, 3. And the teacher has shown this code. The function does what supposed to but I don't understand why after generating 0 1 2 3 sequence pop happens twice and begin from 0 1 not from 0 1 2 3 as i exepcted. It somehow works but I don't understand why?? RE: Need help understanding .pop - bowlofred - Aug-14-2021 (Jul-16-2021, 03:49 PM)Akulinyak Wrote: I don't understand why after generating 0 1 2 3 sequence pop happens twice and begin from 0 1 not from 0 1 2 3 as i exepcted. It's because the recursion doesn't run the functions one after the other, it stops in the middle. You're running a function and get to line 22 (no POP yet). You then start another function. *It* might run to line 22 and start another function. Finally, the last one started exits. At that point it returns and the previous function picks up where it left off at line 23. You see the POP messages. It exits and the previous function picks up (with its arguments) at line 23 also. It's not that one function execution is popping twice, it's that you're running multiple functions simultaneously and they're all printing to the screen in an order that you don't expect. |