Breaking out of a recursive function?

I am wondering how to break out of a recursive loop into a main function. I am trying to do a simple palindrome operation. The function should return True for "redivider", but the return value of True is passed to is_pal (), and the function is not interrupted. With the exception of adding a second is_pal variable to track True / False, what is the right way to break out of this recursive loop? Thanks.

def first(word): return word[0] def last(word): return word[-1] def middle(word): return word[1:-1] def is_pal(str): if len(str) == 1: return True if first(str) == last(str) and len(str) > 1: is_pal(middle(str)) print is_pal("redivider") 
+10
source share
7 answers
 def is_pal(str): if len(str) <= 1: return True if first(str) == last(str): return is_pal(middle(str)) else: return False 

Thus, if they do not match, False returned; if it reaches the end, True is returned. I also eliminated the redundant conditional expression and checked for an edge case of an even-length palindrome.

+8
source

You do not break recursive functions. Trying to do this says that you think about them wrong. Currently, your recursive call ignores the output, which means that recursion is pointless; any returns of is_pal(middle(str)) do not affect the return value of your function.

A recursive algorithm solves the input problem by decomposing the problem into a smaller problem, solving the regression of solving the smaller problem, and then using the smaller solution to build the correct solution to the larger problem. You do not "break" the internal challenges, you return the solution to one level. You do not know (or need to know), you need to know whether you are in an internal call or at a higher level. In any case, your function should do the same: return True if the argument is a palindrome and False if it is not.

The algorithm you are trying to implement is basically this:

  • If the string has a length of 1, this is a palindrome (return True)
  • Otherwise, if the first character matches the last character, then the input is a palindrome, if the middle characters are a palindrome.

So, this means that after you set the first and last characters, the same thing, the answer to “my input is a palindrome” is exactly the same as the answer to “the average character is a palindrome”. You must return a response in order to fulfill your contract. Thus, the recursive call should be return is_pal(middle(str)) , not just is_pal(middle(str)) . If it was a top-level call, then this is the answer; if this is not a top-level call, then an external call will be needed to answer an external problem (in this case, simply returning it).


Btw, your algorithm also has some other problems.

  • You never return False , so the answer can never be False (in this case you accidentally return None , falling away from the end of the function, if the first and last characters do not match, and None will probably be False in most cases, but it still not quite right).
  • If the length of the string is zero , not 1 (what happens if an empty string is sent to or if a palindrome of even length is transmitted once, pairs of equal first and last characters are deleted), then you are not returning the correct answer, and in fact you are trying get the first and last character of an empty string, which will throw an exception.
+7
source

One way to break out of a recursive function in Python is to throw an exception and catch it at the top level. Some people will say that this is the wrong way to think about recursion, but it does its job. In addition, if the task is to identify the "problem" elements in the array / array of arrays / ndarray, etc., the Interrupt method is convenient because it stops the execution of the algorithm after determining the correct global solution.

 def solve_problem(lst): def solve_rec(l): '''has impl. that may throw an exception ''' try: solve_rec(lst) return True except: return False 
+3
source

You need to return the result of your recursive call, and you also need to add return False if other paths fail.

 def is_pal(str): if len(str) == 1: return True if first(str) == last(str) and len(str) > 1: return is_pal(middle(str)) return False 
+1
source

You are missing a refund. Also, do not use str as a variable name. The last thing, the first and last functions could be called a little better.

 def first_letter(word): return word[0] def last_letter(word): return word[-1] def middle(word): return word[1:-1] def is_pal(word): if len(word) == 1: return True if first_letter(word) == last_letter(word) and len(word) > 1: return is_pal(middle(word)) print is_pal("redivider") 
0
source

You need to return False if they do not match and add a return statement. Also, you probably want to check against len ​​(str) == 0 and len (str) == 1:

 def is_pal(str): if len(str) in [0, 1]: return True if first(str) == last(str) and len(str) > 1: return is_pal(middle(str)) else : return False 
0
source

You can exit the program after printing the results using the exit() function.

This may not be a good practice, but it may be what you are looking for.

0
source

Source: https://habr.com/ru/post/1411901/


All Articles