def encodeSimpleCipher(s: str) -> str: """ Encode a string using this formula. - s[i] = 2*i - if 2*i > len(s), s[i] = (2*i) % len(s) """ encoded = list(s) for i in range(len(s)): j = (2 * i) % len(s) cj = encoded[j] encoded[j] = encoded[i] encoded[i] = cj return "".join(encoded) def decodeSimpleCipher(s: str) -> str: """Decode a string encoded from encodeSimpleCipher""" decoded = list(s) for i in range(len(s) - 1, -1, -1): j = (2 * i) % len(s) cj = decoded[j] decoded[j] = decoded[i] decoded[i] = cj return "".join(decoded) print("Testing encodeSimpleCipher()...", end="") assert encodeSimpleCipher("AB") == "BA" assert encodeSimpleCipher("ABC") == "ABC" assert encodeSimpleCipher("ABCD") == "BCDA" assert encodeSimpleCipher("ABCDEFGH") == "BCFGDEHA" assert encodeSimpleCipher("SECRET MESSAGE") == "MCE SSTSAEREEG" assert ( encodeSimpleCipher("THIS IS A LONGER SECRET MESSAGE JUST FOR YOU!") == "T SAENG SR MGARJ H U!SSTE TFECORIIEYSELUOOS" ) print("Passed!") print("Testing decodeSimpleCipher()...", end="") assert decodeSimpleCipher("BA") == "AB" assert decodeSimpleCipher("ABC") == "ABC" assert decodeSimpleCipher("BCDA") == "ABCD" assert decodeSimpleCipher("BCFGDEHA") == "ABCDEFGH" assert decodeSimpleCipher("MCE SSTSAEREEG") == "SECRET MESSAGE" assert ( decodeSimpleCipher("T SAENG SR MGARJ H U!SSTE TFECORIIEYSELUOOS") == "THIS IS A LONGER SECRET MESSAGE JUST FOR YOU!" ) print("Passed!")