Dec-11-2018, 03:39 PM
Hello everyone,
So basically i am trying to solve an homework about dictionaries:
"We will call two words “opposite” if one is equal to
the other read backwards. For example, words “deer” and “reed” are opposite. The function
should accept as a parameter a list of words lst and return a list of all
pairs of opposite words from lst . For example, for a list:
[ "according", "deer", "net", "ten", "reed", "refer",
"raw", "war", "addition", "frequency", "platform" ]
your function should return: [('deer', 'reed'), ('net', 'ten'), ('raw', 'war')]
The result should:
• contain every pair of words only once – notice that there is ('deer', 'reed')
but no ('reed', 'deer')
• not contain palindromes (words which are opposite to themselves).
That's my code, but it doesn't work..
So basically i am trying to solve an homework about dictionaries:
"We will call two words “opposite” if one is equal to
the other read backwards. For example, words “deer” and “reed” are opposite. The function
should accept as a parameter a list of words lst and return a list of all
pairs of opposite words from lst . For example, for a list:
[ "according", "deer", "net", "ten", "reed", "refer",
"raw", "war", "addition", "frequency", "platform" ]
your function should return: [('deer', 'reed'), ('net', 'ten'), ('raw', 'war')]
The result should:
• contain every pair of words only once – notice that there is ('deer', 'reed')
but no ('reed', 'deer')
• not contain palindromes (words which are opposite to themselves).
That's my code, but it doesn't work..
lst= [ "deer", "net", "malayalam", "ten", "reed"] # def find_opposites(lst): new_lst =[] tup = [] for word in lst : rev_word = word[::-1] if rev_word in lst: tup.append(word) tup.append(rev_word) tup = tuple(tup) tup=[] new_lst.append(tup) lst.remove(word) lst.remove(rev_word) else: break print(new_lst)But is not working and i don't understand why..