Condensing your word1
and word2
into a string called alphabets
alphabets = 'abcdefghijklmnopqrstuvwxyz'
The following function will do what you want (It is not very pretty)
def find_match(s):
split_s=s.lower().split(' ')
matches = []
for word in split_s:
found = 0
sum = 0
for i in xrange(0,len(word)//2):
sum += 1
if alphabets.index(word[i])+alphabets.index(word[len(word)-i-1]) == 25:
found += 1
if found == sum:
matches.append(word)
return matches
Output
>>> find_match('bully him')
[]
>>> find_match('the boy wants ')
['boy']
>>> find_match('the boy wants aazz')
['boy', 'aazz']
>>> find_match('the boy wants abayz')
['boy', 'abayz']
>>> find_match('the boy wants abasz')
['boy']
Split your input string to extract words. Then for each word compare the first and the last letter (and so on) to their actual positions in the alphabets (The sum of their indices in alphabet
should be 25
,i.e. max index in alphabets
). If every letter of the word is matched, add the word to a list of matched words