Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On the whole the function of your function is: if length of x equals 5 , return True else if last character of string equals first one return True, else return False</p> <p>This kind of condition else condition...else return False is best expressed with or statement which return False only if all conditions are False and returns value of first not-False element. Other choice is any function which does basically same with any sequence. Here test of those alternatives for all branches of original if statement:</p> <pre><code>def function(x): if len(x) == 5: return True else: return x[0] == x[-1] def funcor(x): return (len(x)==5) or (x[0] == x[-1]) def funcany(x): return any((len(x)==5, x[0] == x[-1])) def funcverbal(sequence): ## sequence[0] is the first element of zero based indexed sequence ## endswith is string specific function so sequence must be string ## if it's length is not 5 return len(sequence)==5 or sequence.endswith(sequence[0]) ## function is normal data type in Python, so we can pass it in as variable def test(func): print('Testing %s function' % func) for value in ('12345','annb','ansa','23424242',('1','2','1'), 123, '131'): try: print ("%r -&gt; %r" % (value,func(value))) except: print ("Failed to call function with " + repr(value)) print(10 * '-'+'Finished testing '+str(func) + 10 * '-') for thisfunction in (function, funcor, funcany, funcverbal): test(thisfunction) </code></pre> <p>(function is hightlighted as reserved word in blue but it is mistake in highlight routine in this website)</p> <p>In case of the isPalindrome function, the length condition is not arbitrary, but it is necessary to recognize the primitive cases to stop the recursion, in case of 'anna' the palindrome function does:</p> <p>see if the length of 'anna' is less than 2 (1 or 0), no they are not compare 'a' with 'a' , continue as they are same drop out the compared first and last letter and call isPalindrome with 'nn'</p> <p>see if the length of 'nn' is less than 2 (1 or 0), no they are not compare 'n' with 'n', continue as they are same drop out the compared first and last letter and call isPalindrome with ''</p> <p>see if the length of '' is less than 2 (1 or 0), yes. Return True as we found palindrome.</p> <p>Here is alternative shorte function of palindrome testing based on the fact that palindrome reversed is same as palindrome.</p> <pre><code>def isPalindrome(s): return s==s[::-1] </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. 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