'Palindrome Seperating in python

I tried to separate a string based on palindrome. But my code splitting extra palindromes in my output what mistake I made?

example: Input 1:

radarnoonlevel

Output 1:

radar noon level

Input 2:

malayalamdadmom

Output 2:

malayalam dad mom

My code:

s="radarnoonlevel"
ans=[]
for i in range(0,len(s)):
  for j in range(i+1,len(s)):
    if s[i]==s[j] and s[i:j+1]==s[i:j+1][::-1]:
      ans+=[s[i:j+1]]
print(*ans)

My output:

radar ada noon oo level eve

I tried to separate a string based on palindrome. But my code splitting strings with extra palindromes in my output what mistake I made?



Solution 1:[1]

Kindly fix the indentation in the function after line 1 (def func(s)). I am not able to properly format it here. I hope the algorithm here helps you.

def break_palin(s):
i = 1
j = 0
mylist = []
while i < len(s):
    if s[i] == s[j]:
        mylist.append(s[j:i+1])
        j = i + 1
        i = i + 2
    else:
        i += 1
return ' '.join(mylist)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vivek kumar