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


Groups > comp.lang.python > #7050

Re: How to stop iteration

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <shashank.sunny.singh@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string,': 0.05; 'chunk': 0.07; 'singh': 0.07; 'python': 0.08; "'this": 0.09; 'received :mail-bw0-f46.google.com': 0.09; 'statement.': 0.09; '>>>': 0.12; 'def': 0.12; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '[gcc': 0.16; 'paradigm.': 0.16; 'received:209.85.214.46': 0.16; 'subject:stop': 0.16; 'intermediate': 0.16; 'cc:addr:python- list': 0.17; 'aug': 0.19; 'simpler': 0.19; 'header:In-Reply-To:1': 0.21; 'modify': 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'trying': 0.23; 'once.': 0.23; 'code': 0.24; 'values': 0.25; '(and': 0.25; 'function': 0.25; 'string': 0.26; 'message- id:@mail.gmail.com': 0.28; '(the': 0.28; 'changing': 0.28; 'received:209.85.214': 0.28; 'checking': 0.29; 'process,': 0.29; 'url:ac': 0.29; 'subject:How': 0.30; 'cc:addr:python.org': 0.30; 'remaining': 0.32; 'list': 0.33; 'actually': 0.33; 'url:in': 0.33; 'too': 0.33; 'using': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'case': 0.37; 'anything': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'received:209': 0.39; 'current': 0.40; 'results': 0.60; 'more': 0.60; 'your': 0.60; 'stop': 0.62; 'taking': 0.64; 'become': 0.72; 'gone': 0.78
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=JZVZ4ayTnMLiUj/YwG6sAQ4p6//y7zexZbvzF0LPTMg=; b=nvE+H8AqlfYHyjTi90a+6VTG76rThDa7n3PAa16xaYH8aBIv/D+nDRugvHfMitdQYp ZpPZY3gDQXCuCgypPj8lR6KJWM9JzOijUx1Qwv9C2L1VyLgnHT8NSXLMcHrU15johKw9 seu7rZ7vlLgfplqRfBKQHUwAzc0zayH9OsPgo=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=VaP23SK7n505iUhwce3EgYjdptyd+LPZURXUw6wLhiJ12mDQvLb2mNucbQPDUmHvFE +5GLqPqS4IRxUGIgI/oZ3TilcTO6rxBDjLVOncFl2FrBkqX8uG/wikki9kgZ7vVm5gKh szVUmei5UUZJZy5pMz0UdJxR6LD/FF5ISc4I0=
MIME-Version 1.0
In-Reply-To <BANLkTimk+YFcazmL0A7SyB+c2Tew3UQOuA@mail.gmail.com>
References <BANLkTimk+YFcazmL0A7SyB+c2Tew3UQOuA@mail.gmail.com>
From Shashank Singh <shashank.sunny.singh@gmail.com>
Date Sun, 5 Jun 2011 21:01:55 +0530
Subject Re: How to stop iteration
To Ganapathy Subramanium <sganapathy.subramanium@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.2470.1307287936.9059.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 82.94.164.166
X-Trace 1307287936 news.xs4all.nl 49042 [::ffff:82.94.164.166]:56667
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7050

Show key headers only | View raw


You can modify your code to stop trying to split the 'remaining part'
when the 'remaining part' is too small

def strsplit(stri, spa):
    if len(stri) <= spa:
        final_result.append(stri)
        return
    s = stri[:spa]
    final_result.append(s)

    stri = stri[spa:]
    strsplit(stri,spa)

Also, note that since in your algorithm, as you move through the
string you are appending the intermediate results to a 'global result
list' you don't need to return anything at any step on the process, so
I have removed the return statement. You can get the result by
checking the 'global result list' when you have gone through the whole
string once.

Python 2.6.1 (r261:67515, Aug  2 2010, 20:10:18)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> string = 'This is a sample python programming'
>>> space = 2
>>> final_result = []
>>> def strsplit(stri, spa):
...     if len(stri) <= spa:
...         final_result.append(stri)
...         return
...     s = stri[:spa]
...     final_result.append(s)
...
...     stri = stri[spa:]
...     strsplit(stri,spa)
...
>>> strsplit(string,space)
>>> final_result
['Th', 'is', ' i', 's ', 'a ', 'sa', 'mp', 'le', ' p', 'yt', 'ho', 'n
', 'pr', 'og', 'ra', 'mm', 'in', 'g']
>>>

Please also note that a simpler way to write this using what is known
as the 'dynamic programming' paradigm. When you take out the first
chunk from your string, if you notice, you are left with the same
problem, it's just that the string in this case have become shorter
(the remaining part of the string, after taking out the current
chunk). You can now call the same function again on the remaining list

Python 2.6.1 (r261:67515, Aug  2 2010, 20:10:18)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> string = 'This is a sample python programming'
>>> space = 2
>>> def strsplit(stri, spa):
...     if len(stri) <= spa:
...         return [stri]
...     return [stri[:spa]] + strsplit(stri[spa:], spa)
...
>>> strsplit(string,space)
['Th', 'is', ' i', 's ', 'a ', 'sa', 'mp', 'le', ' p', 'yt', 'ho', 'n
', 'pr', 'og', 'ra', 'mm', 'in', 'g']
>>>

In this case you are actually using the return values (and not
changing a global variable).

HTH

--
Regards
Shashank Singh
http://www.cse.iitb.ac.in/~shashanksingh

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


Thread

Re: How to stop iteration Shashank Singh <shashank.sunny.singh@gmail.com> - 2011-06-05 21:01 +0530

csiph-web