Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46455

Re: Can anyone please help me in understanding the following python code

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; 'output': 0.05; '---------': 0.07; 'smallest': 0.07; 'subject:code': 0.07; 'subject:help': 0.08; '[0,': 0.09; '[0]': 0.09; '[],': 0.09; 'executed': 0.09; 'item,': 0.09; 'rewrite': 0.09; 'similar,': 0.09; 'way:': 0.09; 'runs': 0.10; 'cc:addr :python-list': 0.11; 'def': 0.12; 'question.': 0.14; 'changes': 0.15; '[2,': 0.16; 'code?': 0.16; 'empty,': 0.16; 'help?': 0.16; 'personally,': 0.16; 'silly': 0.16; 'skip:{ 30': 0.16; 'sorting': 0.16; 'subject:skip:u 10': 0.16; 'subject:python': 0.16; 'applies': 0.16; 'skip:# 20': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'split': 0.19; 'starts': 0.20; 'cc:addr:python.org': 0.22; 'replace': 0.24; 'skip:{ 20': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'skip:" 20': 0.27; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; '[1]': 0.29; 'received:209.85.217': 0.29; '[2]': 0.30; 'statement': 0.30; 'subject:please': 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; 'gives': 0.31; 'code': 0.31; 'getting': 0.31; 'easier': 0.31; 'mid': 0.31; 'once,': 0.31; 'piece': 0.31; 'question:': 0.31; 'lists': 0.32; 'skip:m 30': 0.32; 'run': 0.32; 'running': 0.33; 'subject:the': 0.34; 'received:209.85': 0.35; 'except': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'returning': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'received:209': 0.37; 'sometimes': 0.38; 'lists.': 0.38; 'mine': 0.38; 'list,': 0.38; 'little': 0.38; 'does': 0.39; 'flow': 0.39; 'though,': 0.39; 'sure': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'middle': 0.60; 'skip:n 30': 0.60; 'subject:Can': 0.60; 'new': 0.61; 'back': 0.62; 'our': 0.64; 'places': 0.64; 'different': 0.65; 'taking': 0.65; 'to:addr:gmail.com': 0.65; 'within': 0.65; 'here': 0.66; 'angelico)': 0.84; 'answer:': 0.84; 'how.': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=gV6a6RHislGnfHSU7l1OUm3tDVNpKKvG1VOVbhr9H30=; b=uE7VxAzefUEnHos6s3mdxhsp2xxT2x7ZIjE2WH9Y47v4U6a3CHa846DQ/lENxieijQ 5RSg2YqkFjszpUh0egawFEoNFbn8LSsvOh2I9g7MOAkCdznvRav3uej8SWLqWhNH5sYI 6oBW/RmqjVaap/ehl2tv/eNBJOxQjyxz/5a/stey2lDIc6QFAvN3N+ROEbRT9FWAq9IR sz6gUEftnxojlc11t37h4TUt2DxFC8n8c6If5ZO4GPkyAWPlvWAfrsVDvh2qvqudZld9 RUItVjjtN6wZhA6its1X6Nn511rd16NhsuhvQGo9P7efP9PNlaqaerLdzgKbpS9aium6 aNdw==
X-Received by 10.112.185.104 with SMTP id fb8mr3566997lbc.9.1369911843176; Thu, 30 May 2013 04:04:03 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com>
References <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com>
From Joshua Landau <joshua.landau.ws@gmail.com>
Date Thu, 30 May 2013 12:03:22 +0100
Subject Re: Can anyone please help me in understanding the following python code
To bhk755@gmail.com
Content-Type text/plain; charset=UTF-8
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2403.1369911851.3114.python-list@python.org> (permalink)
Lines 166
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369911851 news.xs4all.nl 15991 [2001:888:2000:d::a6]:33077
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:46455

Show key headers only | View raw


On 30 May 2013 10:48,  <bhk755@gmail.com> wrote:
> Question:
> ---------
> Function mergeSort is called only once, but it is getting recursively executed after the printing the last statement "print("Merging ",alist)". But don't recursion taking place except at these places "mergeSort(lefthalf), mergeSort(righthalf)"
>
> Sometimes the function execution directly starts from i=0,j=0,k=0 . Not sure how.

Here's some different code; it does the same thing but in a less weird way:

##########################
def mergeSort(alist, depth=1):
    # If the piece we have to sort is [i] or [], then we have no sorting to do
    if len(alist) <= 1:
        # Returning what we were given
        # Make a new list from it, though, because we are nice
        print("{padding}return {list}\n".format(padding=" "*depth*8,
list=alist))
        return alist[:]

    # Split along the middle
    mid = len(alist)//2

    # We have two halves
    lefthalf = alist[:mid]
    righthalf = alist[mid:]

    # Which we sort, so we have two sorted halves
    print("{padding}lefthalf  = mergesort({list})\n".format(padding="
"*depth*8, list=lefthalf))
    lefthalf  = mergeSort(lefthalf,  depth+1)

    print("{padding}righthalf = mergesort({list})\n".format(padding="
"*depth*8, list=righthalf))
    righthalf = mergeSort(righthalf, depth+1)


    # We want to return a sorted "alist" from our two lists
    # We'll add the items to here
    new_list = []

    # We'll go through adding the smallest to new_list
    while lefthalf and righthalf:

        # Lefthalf has the smaller item, so we "pop" it off into new_list
        if lefthalf[0] < righthalf[0]:
            new_list.append(lefthalf.pop(0))

        # Righthalf has the smaller item, so we "pop" it off into new_list
        else:
            new_list.append(righthalf.pop(0))

    # One of our lists isn't empty, so just add them on
    new_list += lefthalf + righthalf

    print("{padding}return {list}\n".format(padding=" "*depth*8, list=new_list))
    return new_list

print("Start mergesort({list}):\n".format(list=[2, 4, 0, 1]))

sorted_list = mergeSort([2, 4, 0, 1])

print("Our final result: {list}".format(list=sorted_list))
##########################

And it gives

##########################
Start mergesort([2, 4, 0, 1]):

        lefthalf  = mergesort([2, 4])

                lefthalf  = mergesort([2])

                        return [2]

                righthalf = mergesort([4])

                        return [4]

                return [2, 4]

        righthalf = mergesort([0, 1])

                lefthalf  = mergesort([0])

                        return [0]

                righthalf = mergesort([1])

                        return [1]

                return [0, 1]

        return [0, 1, 2, 4]

Our final result: [0, 1, 2, 4]
##########################

Hopefully this code is a little easier to understand - the original
changes the list while it is running the algorithm and mine makes new
lists. The algorithm is very similar, and what you learn applies to
both.

You can see in the output (thanks Chris Angelico) that the main
function is always running. It runs *two* other instances: lefthalf
and righthalf.

So the "mergesort([2, 4, 0, 1])" runs "lefthalf  = mergesort([2, 4])"

"mergesort([2, 4])" runs "lefthalf  = mergesort([2])"

"mergesort([2])" gives back [2] to "mergesort([2, 4])"

"mergesort([2, 4])" goes "OH! I can continue now" and runs "righthalf
= mergesort([4])"

"mergesort([4])" gives back [4] to "mergesort([2, 4])"

"mergesort([2, 4])" goes "OH! I can continue now" and gives back [2,
4] to "mergesort([2, 4, 0, 1])"

"mergesort([2, 4, 0, 1])" goes "OH! I can continue now" and runs
"righthalf = mergesort([0, 1])"

"mergesort([0, 1])" runs "lefthalf  = mergesort([0])"

"mergesort([0])" gives back [0] to "mergesort([0, 1])"

"mergesort([0, 1])" goes "OH! I can continue now" and runs "righthalf
= mergesort([1])"

"mergesort([1])" gives back [1] to "mergesort([0, 1])"

"mergesort([0, 1])" goes "OH! I can continue now" and gives back [0,
1] to "mergesort([2, 4, 0, 1])"

"mergesort([2, 4, 0, 1])" goes "OH! I can continue now" and gives back
[0, 1, 2, 4]

DONE.

Does that help you see the flow?



Exactly the same flow happens for your code. The difference is that
instead of returning a list, mergesort *sorts* a list, and then that
is used to replace items in the original list. Personally, their way
is a little silly (and mine is inefficient, so use neither).


Question: Why did you rewrite the code?
Answer: Revising is boring.


Question: Sometimes the function execution directly starts from
i=0,j=0,k=0 . Not sure how.
Answer: That's not a question. Anyhow:

i, j and k are LOCAL to a function. "mergesort([2, 4, 0, 1])" has a
different i, j and k than "mergesort([0, 1])", They never use each
other's i, j and k. Hence for each recursion they run "i=0, j=0, k=0"
and they are set to 0 within the function.


Does this help?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 02:48 -0700
  Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-30 20:01 +1000
  Re: Can anyone please help me in understanding the following python code Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-05-30 10:17 +0000
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 03:19 -0700
    Re: Can anyone please help me in understanding the following python	code Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-05-30 10:43 +0000
    Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-30 20:47 +1000
    Re: Can anyone please help me in understanding the following python code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-05-30 12:11 +0100
  Re: Can anyone please help me in understanding the following python code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-05-30 12:03 +0100
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 05:39 -0700
    Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 05:42 -0700
      Re: Can anyone please help me in understanding the following python code Dave Angel <davea@davea.name> - 2013-05-30 09:29 -0400
    Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 05:42 -0700
    Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-30 22:55 +1000
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 06:01 -0700
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 21:53 -0700
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 21:54 -0700
    Re: Can anyone please help me in understanding the following python code Cameron Simpson <cs@zip.com.au> - 2013-05-31 15:43 +1000
    Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-31 17:13 +1000
  Re: Can anyone please help me in understanding the following python code rusi <rustompmody@gmail.com> - 2013-06-01 10:43 -0700

csiph-web