Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is where the magic happens:</p> <pre><code>return (s[0] == s[-1]) and isPal(s[1:-1]) </code></pre> <p>(I have added parentheses, so you are absolutely clear on the operator precedence). It is important to know that the Python AND statement will evaluate the first argument, and only if it is <code>True</code>, will it evaluate the second argument. If the first was <code>False</code>, then it will return <code>False</code> immediately without calling <code>isPal()</code>. This is known as <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation" rel="nofollow noreferrer">short-circuit evaluation</a>. <a href="https://stackoverflow.com/a/18898515/341459">Blender</a> makes a good example of this in his post.</p> <p><code>s[0]</code> is the first letter in the string, <code>s[-1]</code> is the last letter in the sting. First we check to see if the first and last letters are the same. If they are different, then there is no way the string can be a palindrome. If the are the same we can disregard them and move onto the inner part of the string.</p> <p><code>s[1:-1]</code> trims off the first and last letters, and then passes that to the <code>isPal()</code> function, which happens to be the function we are currently in -- this is called recursion. When we 'recurse' into the function again, a new stack is created, new local variables are created, and the input arguments to the function will be different to the ones in the previous call.</p> <p>We continue to check the first and last letters, and recurse all the way to the centre of the word. At this point there are either 1 or 0 characters left, and if we have got that far we know we have found a palindrome.</p> <p>When we return from this final, inner, call to <code>isPal()</code> the return value (<code>True</code> if a palindrome has been found) is passed to the calling function, which then returns it to its calling function, etc., until the final 'outside' <code>isPal()</code> function returns the result to the initial caller. We call this process 'unrolling the stack'.</p> <p>Here is an example:</p> <pre><code>s = 'racecar' s[0] == s[-1] # True - both letters are 'r' s = s[1:-1] # s becomes 'aceca' s[0] == s[-1] # True - both letters are 'a' s = s[1:-1] # s becomes 'cec' s[0] == s[-1] # True - both letters are 'c' s = s[1:-1] # s becomes 'e' len(s) &lt;= 1 # This statement is now True, so the stack will start to unwind </code></pre> <p>Visually, the calls will look like this:</p> <pre><code>'racecar' --------- | V 'aceca' True &lt;--- isPal( ) ---- ^ | True | V 'cec' ---- isPal( ) ---- ^ | True | V 'e' ---- isPal( ) ---- ^ | True | V ---- isPal( ) # We get all the way down here, # and then we start to return </code></pre> <p>In the case where we do not have a palindrome, this will happen:</p> <pre><code>'race2car' -------- | V 'ace2ca' False &lt;--- isPal( ) ---- ^ | False | V 'ce2c' ---- isPal( ) ---- ^ | False | V 'e2' ---- isPal( ) ---- ^ | False | V ---- isPal( ) # Here the outer two letters # do not match, so we start to # return False </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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