Python Forum
Need help understanding .pop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help understanding .pop
#1
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:

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!!
Yoriz write Jul-16-2021, 12:33 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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_p...ursion.asp
https://www.pythonpool.com/python-permutations/

BTW, there is a permutations function in the itertools module you might want to look at. Wink
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#3
(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.

https://www.w3schools.com/python/gloss_p...ursion.asp
https://www.pythonpool.com/python-permutations/

BTW, there is a permutations function in the itertools module you might want to look at. Wink
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??
Reply
#4
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020