Goal
Sometimes dinner menus can have long winded names. It's much easier for the waiter to write an abbreviation that unambiguously identifies the dish.
For example, given this list:
beef burger
chicken burger
chicken nuggets
The abbreviation c n
matches chicken nuggets
.
The abbreviation bur
matches beef burger
and chicken burger
.
Write a program of function that identifies one or more items that match an abbreviation.
Input
- The abbreviated string to find.
- A list of items to search.
You may change this order and use any suitable data types.
Output
- If the abbreviation does not match any items:
Not found
- If the abbreviation unambiguously matches an item: output the unabbreviated item.
- If the abbreviation ambiguously matches several items: output a comma separated list of words that would make the selection unambiguous, followed by a question mark. The order is not important. Extra spaces are permitted.
Matching rules
Each word in the abbreviation and the items is considered separately. If an abbreviated word matches the start of an item word, the item is a potential match. If none of an item's words start with any if the abbreviated words, that item is not a match.
Abbreviated words can be in any order.
An abbreviated word cannot be used to match multiple item words and visa-versa.
Abbreviations only match the start of words.
Assume no item will contain all of the words of another item. For example, you will never have beef burger
and beef cheese burger
.
Test Cases
Given this list:
beef burger
crispy chicken burger
grilled chicken burger
chicken nuggets
chocolate cone
strawberry cone
vanilla cone
These abbreviation give the specified output:
fish Not found
cones Not found
chicken cone Not found
nilla Not found
v vanilla cone
be beef burger
c n chicken nuggets
b b beef burger
c b c crispy chicken burger
c b crispy,grilled?
bu beef,crispy,grilled?
or beef,crispy chicken,grilled chicken?
ch crispy,grilled,nuggets,cone?
or crispy chicken,grilled chicken,nuggets,cone?
Scoring
This is code golf. The shortest answer in bytes in 12 days wins acceptance. (I would have made it one week, but I won't be around for that.)
v
matchesvanilla cone
. \$\endgroup\$c b
would matchcrispy chicken burger
instead of being ambiguous. When I say "word" I mean a string of characters delimited by a space, sog c b
is three words. \$\endgroup\$crispy,grilled,nuggets,chocolate
should be valid for thech
test case? With the second interpretation,chicken,grilled
should be valid for thec b
test case. What am I missing? \$\endgroup\$b b
beef burger
battered bacon
what would be acceptable output? \$\endgroup\$