In a game of word zippers, you get two words
$$A$$ and $$C$$, and the aim is to place a new word $$B$$ in between those
words. The word $$B$$ must be chosen so that the first two words $$AB$$ and
the last two words $$BC$$ each form a new word again. The length of the word
$$B$$ is also given. E.g. the four letter word type
fits between the words hydro
and writer, which results in
the compound words hydrotype
and typewriter.
Assignment
Write a function
that takes the location of a text file as its argument. This text file
must contain a list of all words that are seen as valid. Each of these
words must be on a separate line. The file wordlist.txt
is used in the examples of this assignment, and can also be used to test
your solution. The function must print a (compound) object as a result,
that contains all valid words from the file. The choice of the valid
data type for this object is made in function of the speed with which
the function wordzippers
(see below) is executed. A less suitable choice will lead to a solution
that can not determine a sequence of word zippers within a set time
limit.
Write a function
wordzippers(zipper, words)
that prints all valid words with the given length that fit between the
given words. As a first parameter, a string with the format word-....-word must
be given to the function. The first given word is in front of the first
hyphen, and the second given word comes after the second hyphen. The
words do not contain any hyphens themselves. The number of full stops
that is between the two hyphens, gives the length of the wanted word
that fits between the two given words. The second parameter that is
given to the function is a (compound) object that contains all words
that are considered valid. When searching the word that can form a word
zipper, no distinction should be made between uppercase and lowercase
letters, neither in the words from the given file, nor in the word that
was given in the first parameter.
As a result, the function must print a string, with the format word-WORDS-word.
The words before and after the hyphens are the given words, and are
written in lowercase letters. The words between the hyphens are the
words that form a valid word zipper. These words must be written in
uppercase letters. If there are multiple words between the hyphens, they
must be listed alphabetically and separated by commas. If no words can
be found, three question marks must be placed in between the hyphens.
Example
>>> words = readWords('wordlist.txt')
>>> wordzippers('gyne-....-wrote', words)
'gyne-TYPE-wrote'
>>> wordzippers('hydro-....-writer', words)
'hydro-TYPE-writer'
>>> wordzippers('java-.....-python', words)
'java-???-python'
>>> wordzippers('agit-....-wood', words)
'agit-PROP-wood'
>>> wordzippers('arch-...-thing', words)
'arch-SEE-thing'
>>> wordzippers('frog-...-puller', words)
'frog-LEG-puller'
>>> wordzippers('arche-....-wrote', words)
'arche-TYPE-wrote'