题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
# 拆分出加密前密码 和 加密后密码
original_password = input().strip()
decrypted_password = input().strip()
# 加密
def encrypter(password):
encrypted_result = []
for char in password:
if char.isalpha():
# 检查大小写,然后反转大小写并且加1
if char.islower():
encrypted_result.append(chr(((ord(char) - ord('a') + 1) % 26) + ord('A')))
else:
encrypted_result.append(chr(((ord(char) - ord('A') + 1) % 26) + ord('a')))
elif char.isdigit():
# 数字 + 1
encrypted_result.append(str((int(char) + 1) % 10))
else:
# 保留其他字符
encrypted_result.append(char)
return ''.join(encrypted_result)
# 解密
def decrypter(password):
decrypted_result = []
for char in password:
if char.isalpha():
# 检查大小写,然后反转大小写并且加1
if char.islower():
decrypted_result.append(chr(((ord(char) - ord('a') - 1) % 26) + ord('A')))
else:
decrypted_result.append(chr(((ord(char) - ord('A') - 1) % 26) + ord('a')))
elif char.isdigit():
# 数字 + 1
decrypted_result.append(str((int(char) - 1) % 10))
else:
# 保留其他字符
decrypted_result.append(char)
return ''.join(decrypted_result)
# print最终结果
print(encrypter(original_password))
print(decrypter(decrypted_password))



360集团公司氛围 413人发布