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

Function encode

ciphers/baconian_cipher.py:40–59  ·  view source on GitHub ↗

Encodes to Baconian cipher >>> encode("hello") 'AABBBAABAAABABAABABAABBAB' >>> encode("hello world") 'AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB' >>> encode("hello world!") Traceback (most recent call last): ... Exception: encode() accepts only lett

(word: str)

Source from the content-addressed store, hash-verified

38
39
40def encode(word: str) -> str:
41 """
42 Encodes to Baconian cipher
43
44 >>> encode("hello")
45 'AABBBAABAAABABAABABAABBAB'
46 >>> encode("hello world")
47 'AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB'
48 >>> encode("hello world!")
49 Traceback (most recent call last):
50 ...
51 Exception: encode() accepts only letters of the alphabet and spaces
52 """
53 encoded = ""
54 for letter in word.lower():
55 if letter.isalpha() or letter == " ":
56 encoded += encode_dict[letter]
57 else:
58 raise Exception("encode() accepts only letters of the alphabet and spaces")
59 return encoded
60
61
62def decode(coded: str) -> str:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected