华为机试HJ21简单密码
题目:
现在有一种密码变换算法。九键手机键盘上的数字与字母的对应: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,把密码中出现的小写字母都变成九键键盘对应的数字,如:a 变成 2,x 变成 9.而密码中出现的大写字母则变成小写之后往后移一位,如:X ,先变成小写,再往后移一位,变成了 y ,例外:Z 往后移是 a 。数字和其它的符号都不做变换。数据范围: 输入的字符串长度满足 1≤n≤100
想法:
根据上述规则找到对应的变换并输出
input_str = input()
l1 = ['a','b','c']
l2 = ['d','e','f']
l3 = ['g','h','i']
l4 = ['j','k','l']
l5 = ['m','n','o']
l6 = ['p','q','r','s']
l7 = ['t','u','v']
l8 = ['w','x','y','z']
result = ""
for i in input_str:
if i in l1:
result += "2"
elif i in l2:
result += "3"
elif i in l3:
result += "4"
elif i in l4:
result += "5"
elif i in l5:
result += "6"
elif i in l6:
result += "7"
elif i in l7:
result += "8"
elif i in l8:
result += "9"
elif i < "Z" and i >= "A":
result += chr(ord(i) + 32 + 1)
elif i == "Z":
result += "a"
else:
result += i
print(result)