题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
分开处理加密和解密。 函数中一次判断是否为数字,编码时最后一个字母(解码时第一个字母),大写字母,小写字母。
def enc(s):
s = list(s)
for i in range(len(s)):
if s[i].isdigit():
s[i] = str((int(s[i]) + 1) % 10)
elif s[i] == 'Z':
s[i] = 'a'
elif s[i] == 'z':
s[i] = 'A'
elif s[i].isupper():
s[i] = chr(ord(s[i]) + 1 + 32)
elif s[i].islower():
s[i] = chr(ord(s[i]) + 1 - 32)
return ''.join(s)
def dec(s):
s = list(s)
for i in range(len(s)):
if s[i].isdigit():
s[i] = str((int(s[i]) - 1) % 10)
elif s[i] == 'a':
s[i] = 'Z'
elif s[i] == 'A':
s[i] = 'z'
elif s[i].isupper():
s[i] = chr(ord(s[i]) - 1 + 32)
elif s[i].islower():
s[i] = chr(ord(s[i]) - 1 - 32)
return ''.join(s)
while True:
try:
s1, s2 = input(), input()
d1 = enc(s1)
e2 = dec(s2)
print(d1)
print(e2)
except:
break