Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Created April 28, 2016 10:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuaxo/c371b2d410191a575b763b74719856c8 to your computer and use it in GitHub Desktop.
Save stuaxo/c371b2d410191a575b763b74719856c8 to your computer and use it in GitHub Desktop.
Unique prefixes
# 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