in the following code when I call function isPal the length of string guttug is less than or equal to 1 so the else statement then compares the first letter at index 0 and last letter at index -1 after that i dont understand what goes on . http://dpaste.com/783076/
Output of recursive isPal function is Boolean, it can be True or False. Expression after return statement is evaluated to be True or False. So: 'g' == 'g' and isPal('uttu') --> 'g' == 'g' and 'u' == 'u' and isPal('tt') --> 'g' == 'g' and 'u' == 'u' and 't' == 't' and True evaluates as True, so given argument 'guttug' is palindrome
this is a recursive procedure./algorithm. the first part of line 5 compares the first and last letters of the string - it evaluates to either true or false. the second part of line 5 calls isPal() passing it the string with the first and last letters removed. the new function call starts the process over with the new string - this continues till eventually there are 0 or one letters in the string that is passed as an argument lines 2 and 3 are the base case of the recursive procedure - it just returns True - the recursion stops here. recursive procedures have to have a base case or they will keep calling themselves forever. each time the function gets to line five and calls itself its execution is suspended and the new function is executed. this builds up a stack of functions that have been suspended and are waiting for the function that they called to return. when the base case is reached, it returns true to the function that called it which returns to the function that called it .... til finally the first function can return. in this case the function is trying to determine if the string is a palindrome. it builds up a 'string' of 'anded' booleans. I'm pretty sure that Python uses 'lazy execution' - and comparisons will return False immediately if the first term is False - the second term will not even be evaluated (there is no need to). so for this function, the recursion might get short circuited - lets try, print statements are very helpful for seeing whats happening when your code executes: http://dpaste.com/783285/ yep, the recursion gets short circuited - it doesn't proceed to the base case if s[0] == s[-1] evaluates to False
thanks bwCA your reply is awesome. but the question is at line 5 once the first letter lets say 'g' and the last letter 'g' are compared and results to true it goest to the second part of the line 5 which statement deletes the first letter 'g' and last letter 'g' so that latter 'u' and 'u' are compared . because the second statment only calls the function with the string sliced from second letter 'u' to the last letter 'g'
each recursion the slice removes the first and last letter. what are you asking?
at line 5 the first part compares the first and the last letter but it does not remove the first and the last letter . the second part slices the list form the second letter index 1 to the last letter index -1. for example string 'guttug' the first part compares first letter 'g' and last letter 'g' it is true the second part slices string from index one which in our case is 'u' to the last letter which is 'g' but the code seems to use last letter as 'u' .my question is why because when slicing -1 gives last element which is 'g'
the slice includes all items from the first number up to but not including the second number - slice notation can also include a step. The shell or Idle is a great place to try things out, it is one of the nice things of an interpreted language. http://dpaste.com/784330/ http://effbot.org/zone/python-list.htm http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation
thanks alot i have got it
Join our real-time social learning platform and learn together with your friends!