Created
April 28, 2016 10:56
-
-
Save stuaxo/c371b2d410191a575b763b74719856c8 to your computer and use it in GitHub Desktop.
Unique prefixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ungolphed version with some of the suggestions applied from | |
# http://codegolf.stackexchange.com/questions/28278/find-all-unambiguous-prefixes-of-a-set-of-strings/28280#28280?newreg=5341df6133e145258b48ede419de4fac | |
def unique_prefixes(words): | |
result = {} | |
for word in words: | |
prefix = "" | |
for ch in word: | |
prefix += ch | |
if prefix == word or sum(_word.startswith(prefix) for _word in words) == 1: | |
result[word] = prefix | |
break | |
return result | |
words = raw_input().split(",") | |
unique_prefixes(words) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment