What profession is hidden in the following sequence of words?
FALCON, SEPULCHRE, SATCHEL, BOHEMIAN, PANDEMIC, HEMISPHERE, RESISTOR
The profession we were looking for is ALCHEMIST. We have compiled the sequence of words by first determining the consecutive $$n$$-grams of the word ALCHEMIST for a given $$n \in \mathbb{N}$$ (with $$n > 1$$). For example, for $$n = 3$$ we get the following $$n$$-grams:
ALC, LCH, CHE, HEM, EMI, MIS, IST
We then replace each $$n$$-gram by a word in which it appears.
FALCON, SEPULCHRE, SATCHEL, BOHEMIAN, PANDEMIC, HEMISPHERE, RESISTOR
In doing so, we also ensure that the prefix consisting of the first $$n - 1$$ letters of each $$n$$-gram appears only once in the word. For example, the $$n$$-gram CHE will never be replaced by the word ESCHERICHIA in which the prefix CH is followed by both the letter E and the letter I.
If we would know that ALC is the $$n$$-gram from the first word, we also know that the suffix consisting of the last $$n - 1$$ letters of the $$n$$-gram (LC) appears only once in the second word. Therefore, the letter following that suffix in the second word (H) is the next letter of the profession.
FALCON,
SEPULCHRE,
SATCHEL,
BOHEMIAN,
PANDEMIC,
HEMISPHERE,
RESIST
OR
Now that we know that LCH is the $$n$$-gram from the second word, we apply the same strategy to derive the next letter of the profession from the third word. We know that the suffix consisting of the last $$n - 1$$ letters of the $$n$$-gram (CH) appears only once in the third word. The letter following that suffix in the third word (E) is the next letter of the profession.
Similarly, we can use the remaining words to determine the next letters of the profession.
A word is represented as a string (str) that only consists of letters. A sequence of words is represented as a sequence (list or tuple) containing words.
Find the profession that we have hidden in a given sequence of words in the way described in the introduction. This is done in the following way:
Write a function next_letter that takes two words $$v$$ and $$w$$. If $$v$$ appears just once in $$w$$ and if $$v$$ is no suffix of $$w$$, the letter (str) that follows $$v$$ in $$w$$ must be returned. Otherwise the empty string (str) must be returned.
Write a function extend that takes a word $$p$$ and a sequence of words $$W^{-}$$. The word $$p$$ is the first $$n$$-gram ($$n > 1$$) from a profession that we have hidden in the sequence of words $$W$$. The sequence of words $$W^{-}$$ is the sequence of words $$W$$ without the first word. If the next letters of the profession can be derived from the consecutive words in $$W^{-}$$, the hidden profession (str) must be returned. Otherwise the empty string (str) must be returned.
Write a function profession that takes a sequence of words $$W$$ in which we have hidden a profession. The function also has an optional parameter length (int; default value: 2) that may take the length $$n$$ of the $$n$$-grams that have been used to compile the sequence of words. Each $$n$$-gram of the first word in $$W$$ could be the first $$n$$-gram of the hidden profession. If one of these $$n$$-grams can be used to derive the remaining letters from the consecutive words in $$W$$, the obtained word (str) must be returned as the hidden profession. Otherwise the empty string (str) must be returned.
None of these functions may make a distinction between uppercase and lowercase letters when processing the words passed to them. In addition, the strings (str) they return must always be in uppercase.
>>> next_letter('e', 'HERDSMAN')
'R'
>>> next_letter('LC', 'sepulchre')
'H'
>>> next_letter('onf', 'Teleconference')
'E'
>>> next_letter('EURO', 'DOLLAR')
''
>>> next_letter('LF', 'ALFALFA')
''
>>> next_letter('USE', 'TREEHOUSE')
''
>>> extend('Pe', ['HERDSMAN', 'WONDERFUL', 'FURNACE', 'HELIUM', 'PALINDROME', 'PAPERBACK'])
'PERFUMER'
>>> extend('ALC', ['sepulchre', 'satchel', 'Bohemian', 'pandemic', 'hemisphere', 'resistor'])
'ALCHEMIST'
>>> extend('nonc', ['Teleconference', 'Disinfect', 'Defector', 'Election', 'Section', 'Vibration', 'Pioneer', 'Loner'])
''
>>> profession(['OPERATOR', 'HERDSMAN', 'WONDERFUL', 'FURNACE', 'HELIUM', 'PALINDROME', 'PAPERBACK'])
'PERFUMER'
>>> profession(['falcon', 'sepulchre', 'satchel', 'Bohemian', 'pandemic', 'hemisphere', 'resistor'], length=3)
'ALCHEMIST'
>>> profession(['Nonconformist', 'Teleconference', 'Disinfect', 'Defector', 'Election', 'Section', 'Vibration', 'Pioneer', 'Loner'], 4)
'CONFECTIONER'
The book Fantastic Costumes of Trades & Professions (1965) of parisian engraver Nicolas de Larmessin presents workers clothed in the objects of their calling.