FAQ
Hi All,

I'm a new bie to programming and need some assistance in this code.

I have a function which will split the given string into 3 characters each
and I want to achieve this by recursion.

I have written the following code, but I don't know how to stop the
recursion when the length of the remaining string is less than or equal to 3
characters. Any inputs on this please?

string = 'This is a sample python programming'
space = 2
final_result = []
def strsplit(stri, spa):
s = stri[:spa]
final_result.append(s)
stri = stri[spa:]
strsplit(stri,spa)
return final_result
c = strsplit(string,space)
print 'The final result is: ', c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110605/2c2447b2/attachment.html>

Search Discussions

  • Nitin Pawar at Jun 5, 2011 at 3:24 pm
    check length of input string with if stri.len > 3
    On Sun, Jun 5, 2011 at 8:35 PM, Ganapathy Subramanium wrote:

    Hi All,

    I'm a new bie to programming and need some assistance in this code.

    I have a function which will split the given string into 3 characters each
    and I want to achieve this by recursion.

    I have written the following code, but I don't know how to stop the
    recursion when the length of the remaining string is less than or equal to 3
    characters. Any inputs on this please?

    string = 'This is a sample python programming'
    space = 2
    final_result = []
    def strsplit(stri, spa):
    s = stri[:spa]
    final_result.append(s)
    stri = stri[spa:]
    strsplit(stri,spa)
    return final_result
    c = strsplit(string,space)
    print 'The final result is: ', c


    --
    http://mail.python.org/mailman/listinfo/python-list

    --
    Nitin Pawar
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110605/344641c2/attachment.html>
  • Shashank Singh at Jun 5, 2011 at 3:31 pm
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 5, '11 at 3:05p
activeJun 5, '11 at 3:31p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase