python - Need to find all palindromes of a specific length -


given string, need find index of palindromes of specific length within sequence , print index next palindrome length.

for example, if wanted palindromes 4 characters long:

seq = 'abbacdefggfhijkkjlmn'  

the optimal readout be:

[(0,4), (7,4), (13,4)] 

i have written function this, have glitch in it. returns correct data set, returns on , on again, many times sequence length. example, given sequence above, return data set 20 times:

def find_palindromes(seq,y): l = len(seq) res = [] x in seq:     x=0     while x<= l-y:         if seq[x:x+y] == reverse(seq[x:x+y]):             res.append((x,y))         x=x+1 return res 

any insight glitch appreciated. know may not efficient way of doing things, i'm incredibly new , trying feet wet.

your for-loop starting @ 0 every time, you're doing many times there characters in seq. while loop should enough — need iterate through sequence once. (as 2rs2ts's comment says) remove for x in seq line.

that's simple fix: more drastic 1 switch while-loop for x in range(0, l-y): loop.

also: in code isn't function header should indented, python knows it's inside function.


Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

How to mention the localhost in android -

php - Calling a template part from a post -