I would like to get find all the words that have reversed words in a same sentence. How ever, the code I got finds only first occurrence of the word. in my sentence "i am going to eat ma and tae will also go", I should get an output of am( ma is reversed word) and eat( tae is the reversed word). I only get am..how can i modify this code to get all the words that have reversed words in it i.e. both am and eat.
input_str='i am going to eat ma and tae will also go'
word=input_str.split()
def isReverseEqual(s1, s2):
# If both the strings differ in length
if len(s1) != len(s2):
return False
l = len(s1)
for i in range(l):
# In case of any character mismatch
if s1[i] != s2[l-i-1]:
return False
return True
def getWord(str, n):
reverse=[]
# Check every string
for i in range(n-1):
# Pair with every other string
# appearing after the current string
for j in range(i+1, n):
# If first string is equal to the
# reverse of the second string
if (isReverseEqual(str[i], str[j])):
reverse.append(str[i])
return reverse
# No such string exists
return "-1"
print(getWord(word, len(word)))
Output: ['am','eat'] is what expected.
['am','eat']and not['ma', 'tea']becauseamandeatappears first in the sentence? or it doesn't matter the order of the expected output?