Python Forum
Please help explaining this recursive function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help explaining this recursive function
#1
Hi, I am learning Python and now I am having trouble with a recursive generator. I could not seem to trace how it works regardless of many print statements.

def heap_perm(n, A):      
    print('outside loop 01: n=%d'%n)
    if n == 1: 
        yield A     
        print('outside loop 02: n=%d'%n)
    else:
        print('outside loop 03: n=%d'%n)
        for i in range(n-1):            
            print('inside loop 01: n=%d, i=%d'%(n,i))
            for hp in heap_perm(n-1, A):
                print('inside loop 02: n=%d, i=%d'%(n,i))
                yield hp
                print('inside loop 03: n=%d, i=%d'%(n,i))
            print('inside loop 04: n=%d, i=%d'%(n,i))
            j = 0 if (n % 2) == 1 else i 
            print('inside loop 05: n=%d, i=%d'%(n,i))
            A[j],A[n-1] = A[n-1],A[j]
            print('inside loop 06: n=%d, i=%d'%(n,i))
        print('inside loop 07: n=%d, i=%d'%(n,i))
        for hp in heap_perm(n-1, A): 
            print('inside loop 08: n=%d, i=%d'%(n,i))
            yield hp
            print('inside09: n=%d, i=%d'%(n,i))
When I tried to see the process,

for n = 3 and A = [1,2,3],

I seem to understand how i works for the first round. The result looks like this.

outside loop 01: n=3
outside loop 03: n=3
inside loop 01: n=3, i=0
outside loop 01: n=2
outside loop 03: n=2
inside loop 01: n=2, i=0
outside loop 01: n=1
inside loop 02: n=2, i=0
inside loop 02: n=3, i=0

k= [1, 2, 3]


1. How does n increase back to 3?

When I looked into the second round, I am more confused.

inside loop 03: n=3, i=0
inside loop 03: n=2, i=0

outside loop 02: n=1
inside loop 04: n=2, i=0
inside loop 05: n=2, i=0
inside loop 06: n=2, i=0
inside loop 07: n=2, i=0
outside loop 01: n=1
inside loop 08: n=2, i=0
inside loop 02: n=3, i=0
k= [2, 1, 3]


2. I have no idea why n is reduced again.
3. When it jumped to inside loop 04, why does n increase to 2?

If anyone could please explain me, or provide a better way rather than using print statements, that will be great.
Thanks very much.
Reply
#2
This wikipedia page may help, I think.
Reply
#3
This algorithm is to generate all arrangments of the List Elements.
Since its, a recursive generator function it is difficult to visualise it via print statements.
I'd suggest you either use THIS to visualise or use an IDE debugger and set breakpoints to get a better idea.
Reply
#4
(Nov-13-2018, 02:24 PM)Gribouillis Wrote: This wikipedia page may help, I think.

Thanks a lot.

(Nov-13-2018, 02:35 PM)hbknjr Wrote: This algorithm is to generate all arrangments of the List Elements.
Since its, a recursive generator function it is difficult to visualise it via print statements.
I'd suggest you either use THIS to visualise or use an IDE debugger and set breakpoints to get a better idea.

This website is of great help.

Thanks very much !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 571 May-23-2023, 02:37 AM
Last Post: deanhystad
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,221 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,334 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 2,999 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,668 Dec-03-2020, 05:10 PM
Last Post: muzikman
  list call problem in generator function using iteration and recursive calls postta 1 1,890 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Recursive function returns None, when True is expected akar 0 3,383 Sep-07-2020, 07:58 PM
Last Post: akar
  factorial using recursive function spalisetty06 12 4,043 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  Recursive Function sridhar 7 2,820 Jul-14-2020, 07:53 PM
Last Post: deanhystad
  Nested Recursive Function not Returning Etotheitau 2 2,252 May-09-2020, 06:09 PM
Last Post: Etotheitau

Forum Jump:

User Panel Messages

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