Palindromes are words, numbers or phrases that read the same from left to right and from right to left. Examples of palindromic words in English are eye, gig, kayak, reviver, level, rotator and redivider. The term semordnilap (palindromes spelled from right to left) is used for words that deliver a different word when spelled from right to left. According to the linguist Dmitri A. Borgmann the term was introduced by Martin Gardner in Oddities and Curiosities of Words and Literature.
Write a function dictionary to which the name of a text file must be passed. This text file contains a series of words, each on a separate line. The function must return a set of all words in the specified file. Any white space at the front and end of each line should be removed, and all the capitals in the words must be converted to their corresponding lowercase letter.
Write a function semordnilap to which two arguments should be passed: a word and a set of words. With the words in the given collection all capitals should be converted to their corresponding lowercase letter. The function must return a Boolean value that indicates whether the given word is a semordnilap or not. A word is only considered a semordnilap if the following conditions are met:
the word is found in the given set of words
the inverted word (word read from right to left) is found in the given set of words
the word is not a palindrome
In the following example session we assume that the file words.txt1 is in the current directory.
>>> words = dictionary('words.txt')
>>> len(words)
321115
>>> semordnilap('dream', words)
True
>>> semordnilap('rule', words)
True
>>> semordnilap('fever', words)
True
>>> semordnilap('rocks', words)
True
>>> semordnilap('lever', words)
False