python - Palindrome with Recursion -
i'm trying make more advanced palindrome according docstring. can't work. going right way? have far:
def pal_length(s: str, n: int) -> bool: '''return true iff s has palindrome of length n. >>> pal_length('abclevel', 5) true >>> pal_length('level', 2) false ''' if not s: return true else: index = 0 while index < len(s): if s[index] == s[index+n]: return pal_length(s[index+1:index+n-1],n-1) index += 1 return false
i'm trying not use import modules etc. straight recursion.
any appreciated. thanks.
i think indexing bit off. shouldn't be
index = 0 while index < len(s) - n + 1: if s[index] == s[index+n-1]: return pal_length(s[index+1:index+n-1], n-2) index += 1 return false
Comments
Post a Comment