-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Add Polybius cipher #5409
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
Add Polybius cipher #5409
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
A Polybius Square is a table that allows someone to translate letters into numbers. | ||
|
||
https://www.braingle.com/brainteasers/codes/polybius.php | ||
""" | ||
|
||
import numpy as np | ||
|
||
|
||
class PolybiusCipher: | ||
def __init__(self) -> None: | ||
SQUARE = [ | ||
["a", "b", "c", "d", "e"], | ||
["f", "g", "h", "i", "k"], | ||
["l", "m", "n", "o", "p"], | ||
["q", "r", "s", "t", "u"], | ||
["v", "w", "x", "y", "z"], | ||
] | ||
self.SQUARE = np.array(SQUARE) | ||
|
||
def letter_to_numbers(self, letter: str) -> np.ndarray: | ||
""" | ||
Return the pair of numbers that represents the given letter in the | ||
polybius square | ||
>>> np.array_equal(PolybiusCipher().letter_to_numbers('a'), [1,1]) | ||
True | ||
|
||
>>> np.array_equal(PolybiusCipher().letter_to_numbers('u'), [4,5]) | ||
True | ||
""" | ||
index1, index2 = np.where(self.SQUARE == letter) | ||
indexes = np.concatenate([index1 + 1, index2 + 1]) | ||
return indexes | ||
|
||
def numbers_to_letter(self, index1: int, index2: int) -> str: | ||
""" | ||
Return the letter corresponding to the position [index1, index2] in | ||
the polybius square | ||
|
||
>>> PolybiusCipher().numbers_to_letter(4, 5) == "u" | ||
True | ||
|
||
>>> PolybiusCipher().numbers_to_letter(1, 1) == "a" | ||
True | ||
""" | ||
letter = self.SQUARE[index1 - 1, index2 - 1] | ||
return letter | ||
|
||
def encode(self, message: str) -> str: | ||
""" | ||
Return the encoded version of message according to the polybius cipher | ||
|
||
>>> PolybiusCipher().encode("test message") == "44154344 32154343112215" | ||
True | ||
|
||
>>> PolybiusCipher().encode("Test Message") == "44154344 32154343112215" | ||
True | ||
""" | ||
message = message.lower() | ||
message = message.replace("j", "i") | ||
|
||
encoded_message = "" | ||
for letter_index in range(len(message)): | ||
if message[letter_index] != " ": | ||
numbers = self.letter_to_numbers(message[letter_index]) | ||
encoded_message = encoded_message + str(numbers[0]) + str(numbers[1]) | ||
elif message[letter_index] == " ": | ||
encoded_message = encoded_message + " " | ||
|
||
return encoded_message | ||
|
||
def decode(self, message: str) -> str: | ||
""" | ||
Return the decoded version of message according to the polybius cipher | ||
|
||
>>> PolybiusCipher().decode("44154344 32154343112215") == "test message" | ||
True | ||
|
||
>>> PolybiusCipher().decode("4415434432154343112215") == "testmessage" | ||
True | ||
""" | ||
message = message.replace(" ", " ") | ||
l3str4nge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
decoded_message = "" | ||
for numbers_index in range(int(len(message) / 2)): | ||
if message[numbers_index * 2] != " ": | ||
index1 = message[numbers_index * 2] | ||
index2 = message[numbers_index * 2 + 1] | ||
|
||
letter = self.numbers_to_letter(int(index1), int(index2)) | ||
decoded_message = decoded_message + letter | ||
elif message[numbers_index * 2] == " ": | ||
decoded_message = decoded_message + " " | ||
|
||
return decoded_message |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why replace? Please add test for this condition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there's only 25 spaces in the polybius square and 26 letters, the cipher replaces all j's in the message with i's
A test was added for this condition