3

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.

2
  • In my opinion this activity technically cannot be done 100% correctly. what happen If you enter word "deal" but you really want word "lead". Commented Mar 11, 2020 at 7:51
  • you are expecting ['am','eat'] and not ['ma', 'tea'] because am and eat appears first in the sentence? or it doesn't matter the order of the expected output? Commented Mar 11, 2020 at 8:12

4 Answers 4

3

you can use:

words = input_str.split()

s = set()
result = set()
for w in words:
    r = w[::-1]
    if r in s:
        result.add(r)
    else:
        s.add(w)

list(result)

output:

['am', 'eat']

this is O(n) time complexity solution, so you have to get first the words and iterate through them, each time you have a new word you are adding him to a set, if the reverse is already in the set you are adding the reverse to the result

Sign up to request clarification or add additional context in comments.

Comments

1

just change the indent of line "return reverse" :

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])
    if reverse:
        return reverse
    else:    # No such string exists
        return "-1"
print(getWord(word, len(word)))

Comments

0
input_str= 'i am going to eat ma and tae will also go'
words_list = input_str.split()
new_words_list = [word[::-1] for word in words_list]

data = []
for i in words_list:
    if len(i) > 1 and i in new_words_list:
        data.append(i)

Output:

['am', 'eat', 'ma', 'tae']

Comments

0

Your solution is overcomplicated. For example, function isReverseEqual can be written in one line:

def isReverseEqual(s1, s2):
    return s1==s2[::-1]

Let's simplify it. First, treat your sentence as a set, not a list, because set lookups are more efficient:

words = set(input_str.split())

Next, select the words from the set/list if their reversed copy is also in the set/list and they are alphabetically smaller than the reversed copy (to avoid duplicates):

[w for w in words if w[::-1] in words and w < w[::-1]]
#['am', 'eat']

This solution works for lists, too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.