MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / decode

Function decode

ciphers/baconian_cipher.py:62–83  ·  view source on GitHub ↗

Decodes from Baconian cipher >>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB") 'hello world' >>> decode("AABBBAABAAABABAABABAABBAB") 'hello' >>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB!") Traceback (most recent call last): ...

(coded: str)

Source from the content-addressed store, hash-verified

60
61
62def decode(coded: str) -> str:
63 """
64 Decodes from Baconian cipher
65
66 >>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB")
67 'hello world'
68 >>> decode("AABBBAABAAABABAABABAABBAB")
69 'hello'
70 >>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB!")
71 Traceback (most recent call last):
72 ...
73 Exception: decode() accepts only 'A', 'B' and spaces
74 """
75 if set(coded) - {"A", "B", " "} != set():
76 raise Exception("decode() accepts only 'A', 'B' and spaces")
77 decoded = ""
78 for word in coded.split():
79 while len(word) != 0:
80 decoded += decode_dict[word[:5]]
81 word = word[5:]
82 decoded += " "
83 return decoded.strip()
84
85
86if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected