Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Not</strong> untested ...</p> <p>code:</p> <pre><code>def countSubStringMatchRecursive(target, key, count=0): #### index = find(target, key) # HUH? index = target.find(key) if index &gt;= 0: count += 1 target = target[index+len(key):] count = countSubStringMatchRecursive(target, key, count) return count for test in ['', 'bar', 'foo', 'foofoo', 'foo foo foo fo']: print countSubStringMatchRecursive(test, 'foo'), test.count(key), repr(test) </code></pre> <p>output:</p> <pre><code>0 0 '' 0 0 'bar' 1 1 'foo' 2 2 'foofoo' 3 3 'foo foo foo fo' </code></pre> <p>I'm presuming that this is just amusement or homework ... recursive function must be slower than corresponding Python iterative solution, which will be naturally slower than using <code>target.count(key)</code> ... so I haven't bothered with fixing all the problems your version had ... but do read PEP-008 :-)</p> <p><strong>Comments on string module</strong></p> <p>You commented that you had omitted <code>from string import find</code>. What version of Python are you using? What is the last update date on the book or tutorial that you are using?</p> <p>From the start of the string module (it will be on your computer as <code>&lt;your Python install directory&gt;/Lib/string.py</code>; I'm quoting from the 2.6 version):</p> <p>"""A collection of string operations (most are no longer used).</p> <p>Warning: most of the code you see here isn't normally used nowadays. Beginning with Python 1.6, many of these functions are implemented as methods on the standard string object. They used to be implemented by a built-in module called strop, but strop is now obsolete itself.</p> <p>etc """</p> <p>and here is that file's code for the <code>find</code> function (stripped of comments):</p> <pre><code>def find(s, *args): return s.find(*args) </code></pre> <p>so using <code>string.find(target, key)</code> instead of <code>target.find(key)</code> is a waste. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload