Which word comes at the question mark?
BUY, PEA, DOC, RYE, FIG
ATOM, LAWS, WHEY, HOME
UNITS, TORSO, SPARK
NOBODY, CYCLIC, RIDING
GLIDERS, MARISOL, ?
The word sequences are characterized by the fact that at each position the letters jump forward a fixed distance in a cyclic alphabet (where the letter Z is followed by the letter A). For example, in the top sequence we see that the first letter jumps forward 14 positions from each word to its successor, the second letter 10 positions and the third letter 2 positions.
From GLIDERS to MARISOL, the corresponding letters jump forward 6, 15, 9, 5, 14, 23 and 19 positions respectively. If we jump forward the same number of positions from MARISOL, we find the next word in the last sequence: SPANGLE. We could try to jump once more from that word across the same number of positions, but then we would find the non-existent word YEJSUIX.
A word is a string (str) consisting of one or more uppercase letters. Your task:
Write a function distance that takes two uppercase letters (str). The function must return how many (int) positions you must jump forward in a cyclic alphabet to go from the first letter to the second letter. We call this the distance from the first to the second letter.
Write a function distances that takes two words (str) of equal length. The function must return a list (list) with the distances (int) from each letter in the first word to the letter at the corresponding position in the second word.
Write a function jump that takes two arguments: i) a word $$w$$ (str) consisting of $$n \in \mathbb{N}_0$$ letters $$l_1l_2l_3\ldots l_n$$ and ii) a sequence $$d = d_1d_2d_3\ldots d_n$$ (list or tuple) of $$n$$ numbers $$d_i \in \mathbb{N}$$ (int; $$i = 1, 2, 3, \ldots, n$$). The function must return the word obtained by jumping forward from each letter $$l_i$$ across $$d_i$$ positions in a cyclic alphabet, for each letter $$l_i$$ of word $$w$$ ($$i = 1, 2, 3, \cdots, n$$). We call this the word we asynchronously jump towards from word $$w$$ across distances $$d$$.
Write a function sequence that takes three arguments: i) a word $$w$$ (str) consisting of $$n \in \mathbb{N}_0$$ letters $$l_1l_2l_3\ldots l_n$$, ii) a sequence $$d = d_1d_2d_3\ldots d_n$$ (list or tuple) of $$n$$ numbers $$d_i \in \mathbb{N}$$ (int; $$i = 1, 2, 3, \ldots, n$$) and iii) the location (str) of a text file containing all existing words (one word per line). The function must return a list (list) of existing words (str). If $$w$$ is not an existing word, the empty list must be returned. Otherwise the list begins with word $$w$$. As long as it is possible to asynchronously jump from the last word in the list across distances $$d$$ towards a new existing word, this new word must be added at the end of the list.
In the following interactive session we assume the text file words.txt1 to be located in the current directory.
>>> distance('U', 'W')
2
>>> distance('Z', 'J')
10
>>> distance('T', 'T')
0
>>> distances('BUY', 'PEA')
[14, 10, 2]
>>> distances('ATOM', 'LAWS')
[11, 7, 8, 6]
>>> distances('UNITS', 'TORSO')
[25, 1, 9, 25, 22]
>>> distances('NOBODY', 'CYCLIC')
[15, 10, 1, 23, 5, 4]
>>> distances('GLIDERS', 'MARISOL')
[6, 15, 9, 5, 14, 23, 19]
>>> jump('BUY', [14, 10, 2])
'PEA'
>>> jump('ATOM', (11, 7, 8, 6))
'LAWS'
>>> jump('UNITS', [25, 1, 9, 25, 22])
'TORSO'
>>> jump('NOBODY', (15, 10, 1, 23, 5, 4))
'CYCLIC'
>>> jump('GLIDERS', [6, 15, 9, 5, 14, 23, 19])
'MARISOL'
>>> sequence('BUY', [14, 10, 2], 'words.txt2')
['BUY', 'PEA', 'DOC', 'RYE', 'FIG']
>>> sequence('ATOM', (11, 7, 8, 6), 'words.txt3')
['ATOM', 'LAWS', 'WHEY', 'HOME']
>>> sequence('UNITS', [25, 1, 9, 25, 22], 'words.txt4')
['UNITS', 'TORSO', 'SPARK']
>>> sequence('NOBODY', (15, 10, 1, 23, 5, 4), 'words.txt5')
['NOBODY', 'CYCLIC', 'RIDING']
>>> sequence('GLIDERS', [6, 15, 9, 5, 14, 23, 19], 'words.txt6')
['GLIDERS', 'MARISOL', 'SPANGLE']