← Back to challenges

Polybius Square (Basic)

PythonHardcryptographyarraysstrings

Instructions

The Polybius Square cipher is a simple substitution cipher that makes use of a 5x5 square grid. The letters A-Z are written into the grid, with "I" and "J" typically sharing a slot (as there are 26 letters and only 25 slots).

12345
1ABCDE
2FGHI/JK
3LMNOP
4QRSTU
5VWXYZ

To encipher a message, each letter is merely replaced by its row and column numbers in the grid.

Create a function that takes a plaintext or ciphertext message, and returns the corresponding ciphertext or plaintext.

Examples

polybius("Hi") ➞ "2324"

polybius("2324  4423154215") ➞ "hi there"

polybius("543445 14343344 522433 21422415331443 52244423 4311311114") ➞ "you dont win friends with salad"

Notes

As "I" and "J" share a slot, both are enciphered into 24, but deciphered only into "I" (see third and fourth test).

python3
Loading editor…
⌘ ↡ to run
Walks through the solution with reasoning and edge cases.