def encodeRouteCipher(message: str, rows: int) -> str: """Encode a message using the Route Cipher.""" cur_char = "z" while len(message) % rows != 0: message += cur_char cur_char = chr(ord(cur_char) - 1) str_cols = [message[i : i + rows] for i in range(0, len(message), rows)] str_rows = ["".join(col) for col in zip(*str_cols)] encoded_message = f"{rows}" for idx, row in enumerate(str_rows): if idx % 2 == 0: encoded_message += row else: encoded_message += row[::-1] return encoded_message def decodeRouteCipher(encodedMessage: str) -> str: """Decode a message encoded using the Route Cipher.""" rows = int(encodedMessage[0]) rows_len = len(encodedMessage[1:]) // rows encoded_message = encodedMessage[1:] str_rows = [ encoded_message[i : i + rows_len] for i in range(0, len(encoded_message), rows_len) ] str_rows = [row[::-1] if idx % 2 == 1 else row for idx, row in enumerate(str_rows)] str_cols = ["".join(row) for row in zip(*str_rows)] decoded_string = "".join(str_cols) while decoded_string[-1].islower(): decoded_string = decoded_string[:-1] return decoded_string print("Testing encodeRouteCipher()...", end="") assert encodeRouteCipher("ASECRETMESSAGE", 4) == "4AREGESESETSzyAMC" assert encodeRouteCipher("ASECRETMESSAGE", 3) == "3ACTSGESMRSEEEAz" assert encodeRouteCipher("ASECRETMESSAGE", 5) == "5AESATSEMGEECRSz" assert encodeRouteCipher("ANOTHERSECRET", 4) == "4AHETzCENORRyxEST" print("Testing decodeRouteCipher()...", end="") assert decodeRouteCipher("4AREGESESETSzyAMC") == "ASECRETMESSAGE" assert decodeRouteCipher("3ACTSGESMRSEEEAz") == "ASECRETMESSAGE" assert decodeRouteCipher("5AESATSEMGEECRSz") == "ASECRETMESSAGE" assert decodeRouteCipher("4AHETzCENORRyxEST") == "ANOTHERSECRET" message = "SECRETSTUFFGOESHERE" encodedMessage = encodeRouteCipher(message, 6) plaintext = decodeRouteCipher(encodedMessage) assert plaintext == message print("Passed!")