Can anyone please help me in understanding the following python code

Chris Angelico rosuav at gmail.com
Thu May 30 08:55:38 EDT 2013


On Thu, May 30, 2013 at 10:39 PM,  <bhk755 at gmail.com> wrote:
> Chris, Can you please let me know what makes the control of the program code go to  2c after the output "Merging".


It goes like this:

1. [eight element list]
2a. [eight element list]
2b.   1. [four element list]
2b.   2a. [four element list]
2b.   2b.    1. [two element list]
2b.   2b.    2a. [two element list]
2b.   2b.    2b. [two element list]
2b.   2b.    2b.    1. [one element list]
2b.   2b.    2b.    2. [one element list]
2b.   2b.    2b.    3. [one element list]
2b.   2b.    2c. [two element list]
2b.   2b.    3. [two element list]
... etc etc etc ...

Notice how control actually flows from 1, to 2a, to 2b, to 2c, but in
between, it goes back to 1? That's recursion. There are four separate
function calls here, which you can see going down the page. Each time
it gets to step 2b, a whole new "thread" starts, and the previous one
doesn't do anything till the inner one reaches step 3 and finishes.

That's why the indented output can help; eyeball them going down and
you'll see that they don't do anything until the inner one finishes.

ChrisA



More information about the Python-list mailing list