This code I've written reads the words from a certain file and connects them into a chain in accordance with the given principle. Without the last snippet (the one after the last comment line), it would just output the longest chain possible, composed from the input words. Then the code would meet only 5 requirements. I added the last section in case there are some unmatchable words in the source file, and now the 6th requirement is satisfied, too. However, the last one is still not validated, though the input given in the example produces the expected output. I'm not sure what else I haven't taken into account.