Skip to content

Create baconian_cipher.py #5251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Oct 14, 2021
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d087155
Create baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
203f127
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
7b19ebf
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
ca64585
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
8045dd7
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
a5797d4
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
0996953
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
1a2c7a7
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
e39e02b
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
3c40713
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
09dfe50
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
e2a8883
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
6ddfc1e
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
65eda7d
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
0c3d668
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
3676c27
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
621f5e5
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
71514ce
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
e89bd55
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
fda5dc8
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
9d14237
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
18db6a5
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
73c1553
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
a51d545
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
6198907
Update baconian_cipher.py
Rohanrbharadwaj Oct 11, 2021
04be254
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
38eafd9
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
79ff3ef
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
92e8b9e
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
42043be
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
7a973dc
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
369e47b
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
a4cf59c
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
694cb87
Update baconian_cipher.py
Rohanrbharadwaj Oct 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update baconian_cipher.py
  • Loading branch information
Rohanrbharadwaj authored Oct 14, 2021
commit 04be254bda1659c46c854bc34e994f1a2cefaa9a
16 changes: 6 additions & 10 deletions ciphers/baconian_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
" ": " ",
}

decode_dict = {}
for alphabet in encode_dict:
decode_dict[encode_dict[alphabet]] = alphabet
decode_dict = {value : key for key, value in encode_dict.items()}


def encode(word: str) -> str:
Expand All @@ -49,14 +47,14 @@ def encode(word: str) -> str:
>>> encode("hello world!")
Traceback (most recent call last):
...
Exception: encode() accepts only alphabets
Exception: encode() accepts only letters of the alphabet and spaces
"""
encoded = ""
for letter in word.lower():
if letter.isalpha() or letter == " ":
encoded += encode_dict[letter]
else:
raise Exception("encode() accepts only alphabets")
raise Exception("encode() accepts only letters of the alphabet and spaces")
return encoded


Expand All @@ -75,15 +73,13 @@ def decode(coded: str) -> str:
"""
if set(coded) - {"A", "B", " "} != set() and set(coded) - {"A", "B"} != set():
raise Exception("decode accepts only 'A', 'B' and ' '")
words = coded.split()
decoded = ""
for word in words:
decoded = ""
for word in coded.split():
while len(word) != 0:
decoded += decode_dict[word[:5]]
word = word[5:]
decoded += " "
decoded = decoded.strip(" ")
return decoded
return decoded.strip()


if "__name__" == "__main__":
Expand Down