Skip to content

Instantly share code, notes, and snippets.

@Bumbleblo
Created February 1, 2021 08:55
Show Gist options
  • Save Bumbleblo/e1ec44e590a0ef7f49249c1ada720b72 to your computer and use it in GitHub Desktop.
Save Bumbleblo/e1ec44e590a0ef7f49249c1ada720b72 to your computer and use it in GitHub Desktop.
Test if a alphabet have a unique encoding using the "test for unique decodability".
codewords = {'0', '01', '10', '1'}
def isprefix(string_1: str, string_2: str) -> bool:
return string_1 == string_2[:len(string_1)]
dangling_codewords = set()
for word1 in codewords:
for word2 in codewords:
if word1 == word2:
continue
if isprefix(word1, word2):
dangling_codewords.add(word2[len(word1):])
print(dangling_codewords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment