26
1
This question is based on a question I asked in Spanish language. Yes, I asked for an algorithm in Spanish Language. :)
In Spain, current license plates have this pattern:
1234 XYZ
where XYZ are three consonants taken from the full set of Spanish consonants (except the 'Ñ', I think).
Sometimes, when traveling with my wife, we use to play a game. When we see a license plate, we take its three consonants and try to form a word that contains those three consonants, appearing in the same order as in the license plate. Examples (in Spanish):
BCD
BoCaDo (valid)
CaBezaDa (not valid)
FTL
FaTaL (valid)
FLeTar (not valid)
FTR
FleTaR (valid, wins)
caFeTeRa (valid, loses)
The winner is the one who uses the least number of characters, as you can see in the last example.
The challenge
Write the shortest program or function that receives a list of words and a set of three consonants and finds the shortest word in the list that contains the three consonants in the same order. For the purposes of this game, case does not matter.
- The input for the word list (first parameter) will be an array of your language
string
type. The second parameter (the three consonants) will be anotherstring
. If it's better for your language, consider thestring
with the three consonants the last item of the whole list of parameters. The output will be anotherstring
. - The words in the word list won't be invented or infinite words, they will word that appear in any standard dictionary. If you need a limit, suppose no word in the word list will be longer than 50 characters.
- If there are several words with the same lenght that could be the valid answer, you can return any one of them. Just make sure you return just one word, or an empty string if no words match the pattern of three consonants.
- You can repeat consonants in the group, so valid inputs for the three consonants are both
FLR
andGGG
. - The Spanish consonants are exactly the same as English, with the addition of the "Ñ". The vowels are the same with the adition of the stressed vowels: "áéíóúü". There won't be any other kind of marks such as "-" or "'".
- You can suppose the case will always be the same in both the word list and the three consonants.
If you want to test your algorithm with a real collection of Spanish words, you can download a file (15.9 MB) from Dropbox with more than a million words.
Test cases
Input: 'psr', {'hola' 'repasar' 'pasarais' 'de' 'caída' 'pequeñísimo' 'agüeros'}
Output: 'repasar'
Input: 'dsd', {'dedos' 'deseado' 'desde' 'sedado'}
Output: 'desde'
Input: 'hst', {'hastío' 'chest'}
Output: 'chest'
This is code-golf, so may the shortest program that helps me to always beat my wife wins! :)
How long are the words in the word list guaranteed to be? – Neil – 2017-06-17T14:25:13.073
@Neil Just suppose they come from a standard dictionary, there won't be invented, infinite words. I don't know what would be the longest word in English or Spanish. If you need a limit, just consider no word will be longer than 50 characters. – Charlie – 2017-06-17T14:30:22.770
2In actual license plates, letter Q is not allowed either; and W is, although not a proper Spanish letter – Luis Mendo – 2017-06-17T14:31:44.317
@Arnauld yes, you can suppose that words will always be uppercase or lowercase. – Charlie – 2017-06-17T14:33:18.190
You should add some simple test cases in your text, with input and output – Luis Mendo – 2017-06-17T14:34:36.570
Sorry, I meant the length of the words, not the count of them. – Neil – 2017-06-17T14:37:46.033
2May we assume the words in the list and the three letters will be all in one case? – Jonathan Allan – 2017-06-17T14:39:43.690
Are we guaranteed to find at least one matching word for the given consonants? If not, what's the expected behavior? – Arnauld – 2017-06-17T14:47:24.707
@Arnauld no, you cannot suppose that there will always be a match. If not, just return the empty string (question edited, sorry for that). – Charlie – 2017-06-17T14:50:07.517
@JonathanAllan yes, you can assume that. – Charlie – 2017-06-17T14:51:46.170
A test case like
'psr', {'hola' 'repasar' 'pasarais' 'de' 'caída' 'pequeñísimo' 'agüeros'}
may be interesting. My answer failed for that one, because it was only findind the first occurrence of each letter in a word – Luis Mendo – 2017-06-17T15:06:53.7431
@LuisMendo W has been a Spanish letter since 1969.
– walen – 2017-06-19T07:22:52.4001@walen That's why I said "proper" :-) It exists in Spanish, but is feels foreign – Luis Mendo – 2017-06-19T08:47:48.920