Autocomplete the input Pre-Note
Because of popular demand, and to avoid disqualifying certain languages, the dictionary can now be provided as input parameter.
Intro
Given a dictionary of words, you should write an autocomplete program or function.
Autocompletion
&quot;Autocomplete, or word completion, is a feature in which an application predicts the rest of a word a user is typing.&quot; (Wikipedia)
Autocompletion is oftenly used to reduce the time needed for typing sentences. After typing a single character, the program will start guessing what the user is trying to say. In this challenge we will simulate the behaviour of autocompleting words.
If the program encounters a word it doesn't know, and no matching autocompletion words can be found, it will assume that the user is trying to type a word the program doesn't know. Good autocompletion software will own and edit a custom dictionary of words, but for the sakes of complexity that's not part of this challenge.
Input
Your program/function will take at least a single parameter of input. This parameter can either be a string or an array of characters, and will consist of lowercase characters [a-z].
Optionally, your program/function will take a second parameter containing a dictionary of words. More info on this in the paragraph Dictionary.
Output
You should print or return an array of autocompleted words. For each character in the input, an autocompletion should be added to the output array.
The autocompletions will be taken from the provided dictionary. If a word doesn't exist in the dictionary, it will alphabetically search for the next word matching your characters. If no matching word is found, the input will be returned (see test cases)
Dictionary
If you choose to read the dictionary from a file, the file structure should be your list of words, seperated by newlines.
If you choose to submit the dictionary of words as a parameter of a function call, it should be structured as an ordered list of strings; ['cat', 'cake', ...].
The input is always fully lowercase and sorted alphabetically.
Test case
a.txt content:
alpha
calf
cave
coax
code
codegimmick
codegolf
cool
gamma

Test cases:
&quot;codegolf&quot;                  // [&quot;calf&quot;, &quot;coax&quot;, &quot;code&quot;, &quot;code&quot;, &quot;codegimmick&quot;, &quot;codegolf&quot;, &quot;codegolf&quot;, &quot;codegolf&quot;]
&quot;alpaca&quot;                    // [&quot;alpha&quot;, &quot;alpha&quot;, &quot;alpha&quot;, &quot;alpa&quot;, &quot;alpac&quot;, &quot;alpaca&quot;]
[&quot;t&quot;, &quot;h&quot;, &quot;i&quot;, &quot;s&quot;]        // [&quot;t&quot;, &quot;th&quot;, &quot;thi&quot;, &quot;this&quot;]
(&quot;catses&quot;, [&quot;cake&quot;,&quot;cats&quot;]) // [&quot;cake&quot;, &quot;cake&quot;, &quot;cats&quot;, &quot;cats&quot;, &quot;catse&quot;, &quot;catses&quot;]

This is code-golf, shortest code in bytes wins
Pyth, 22 13 bytes

mhf!xTd+Qd._z




m         _z  map over prefixes of second input...
 hf    +Qd    find the first element in (dictionary+word) which...
   !xTd       ... starts with k


Sample run:

llama@llama:~$ pyth -c 'mhf!xTd+Qd._z' 
['alpha', 'calf', 'cave', 'coax', 'code', 'codegimmick', 'codegolf', 'cool', 'gamma']
codegolf
['calf', 'coax', 'code', 'code', 'codegimmick', 'codegolf', 'codegolf', 'codegolf']


Thanks to @FryAmTheEggman for 4 bytes!