What word comes at the question mark to complete this word sequence?
Space, Cinema, Ester, Abaci, Cabaret, Seamen, ?
The complete sequence is a palindrome: you get the same sequence of letters if you read the letters of the words from left to right or from right to left. So the missing word is Icecaps:
Space, Cinema, Ester, Abaci, Cabaret, Seamen, Icecaps
In this kind of puzzle you have to append one word at the end of a given sequence such that the complete sequence is a palindrome. You can find the missing word by first writing all letters of the given words one after the other. In doing so, convert all letters into lowercase, to make no distinction between uppercase and lowercase letters. We will call this the forward sequence:
spacecinemaesterabacicabaretseamen
Then reverse the order of the letters from the forward sequence. We will call this the backward sequence:
nemaesterabacicabaretseamenicecaps
Next, find the longest suffix of the forward sequence that is also a prefix of the backward sequence. Here's that longest common suffix/prefix marked in green:
spacecinemaesterabacicabaretseamen------- -------nemaesterabacicabaretseamenicecaps
If you remove the longest common suffix/prefix as a prefix from the backward sequence, what remains is the missing word. We marked that missing word in blue in the above example.
A prefix of a word $$w$$ is a sequence of one or more letters at the start of $$w$$. For example: fore is a prefix of foresight.
A suffix of a word $$w$$ is a sequence of one or more letters at the end of $$w$$. For example: sight is a suffix of foresight.
Write the following four functions that each take a word sequence $$W$$ (str) whose words only contain letters (uppercase and lowercase) and are separated by a comma (,) and a space. One word is missing at the end of the word sequence to make it a palindrome.
A function forward_sequence that returns the forward sequence (str) of $$W$$.
A function backward_sequence that returns the backward sequence (str) of $$W$$.
A function common_sequence that returns the longest common suffix/prefix (str) of $$W$$, i.e. the longest suffix of the forward sequence of $$W$$ that is also a prefix of the backward sequence of $$W$$.
A function missing_word that returns the missing word (str) that can be appended to $$W$$ to make it a palindrome. The missing word must be written as an uppercase letter followed by lowercase letters.
>>> forward_sequence('Space, Cinema, Ester, Abaci, Cabaret, Seamen')
'spacecinemaesterabacicabaretseamen'
>>> backward_sequence('Space, Cinema, Ester, Abaci, Cabaret, Seamen')
'nemaesterabacicabaretseamenicecaps'
>>> common_sequence('Space, Cinema, Ester, Abaci, Cabaret, Seamen')
'nemaesterabacicabaretseamen'
>>> missing_word('Space, Cinema, Ester, Abaci, Cabaret, Seamen')
'Icecaps'