题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
按照题意嗯模拟就好,其中、、的转换问题可以考虑在正常转换过程中使用来普适化解决。
def EncryptPw(originalEncryptPw): newEncryptPw = "" for i in range(len(originalEncryptPw)): if originalEncryptPw[i].islower(): newEncryptPw += chr(ord("A") + ((ord(originalEncryptPw[i].upper()) - ord("A") + 1) % 26)) elif originalEncryptPw[i].isupper(): newEncryptPw += chr(ord("a") + ((ord(originalEncryptPw[i].lower()) - ord("a") + 1) % 26)) elif originalEncryptPw[i].isdigit(): newEncryptPw += chr(ord("0") + ((ord(originalEncryptPw[i]) - ord("0") + 1) % 10)) else: newEncryptPw += originalEncryptPw[i] return newEncryptPw def DecryptPw(originalDecryptPw): newDecryptPw = "" for i in range(len(originalDecryptPw)): if originalDecryptPw[i].islower(): newDecryptPw += chr( ord("A") + (((ord(originalDecryptPw[i].upper()) - ord("A")) - 1 + 26) % 26)) elif originalDecryptPw[i].isupper(): newDecryptPw += chr( ord("a") + (((ord(originalDecryptPw[i].lower()) - ord("a")) - 1 + 26) % 26)) elif originalDecryptPw[i].isdigit(): newDecryptPw += chr(ord("0") + ((ord(originalDecryptPw[i]) - ord("0") - 1 + 10) % 10)) else: newDecryptPw += originalDecryptPw[i] return newDecryptPw if __name__ == "__main__": originalEncryptPw = str(input()) originalDecryptPw = str(input()) print(EncryptPw(originalEncryptPw)) print(DecryptPw(originalDecryptPw))