Created
February 1, 2021 08:55
-
-
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".
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
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