usrbinenv python3 a polybius square is a table that allows someone to translate letters into numbers https www braingle combrainteaserscodespolybius php return the pair of numbers that represents the given letter in the polybius square np arrayequalpolybiuscipher lettertonumbers a 1 1 true np arrayequalpolybiuscipher lettertonumbers u 4 5 true return the letter corresponding to the position index1 index2 in the polybius square polybiuscipher numberstoletter4 5 u true polybiuscipher numberstoletter1 1 a true return the encoded version of message according to the polybius cipher polybiuscipher encodetest message 44154344 32154343112215 true polybiuscipher encodetest message 44154344 32154343112215 true return the decoded version of message according to the polybius cipher polybiuscipher decode44154344 32154343112215 test message true polybiuscipher decode4415434432154343112215 testmessage true 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 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 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 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 return the decoded version of message according to the polybius cipher polybiuscipher decode 44154344 32154343112215 test message true polybiuscipher decode 4415434432154343112215 testmessage true
import numpy as np

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"],
]


class PolybiusCipher:
    def __init__(self) -> None:
        self.SQUARE = np.array(SQUARE)

    def letter_to_numbers(self, letter: str) -> np.ndarray:
        
        index1, index2 = np.where(letter == self.SQUARE)
        indexes = np.concatenate([index1 + 1, index2 + 1])
        return indexes

    def numbers_to_letter(self, index1: int, index2: int) -> str:
        
        return self.SQUARE[index1 - 1, index2 - 1]

    def encode(self, message: str) -> str:
        
        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:
        
        message = message.replace(" ", "  ")
        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