题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
使用正则表达式匹配小写字母a-z,大写字母A-Y。使用字典保存9键中包含字母的8键,来转换小写字母;通过ASCII码值转码解码来转换大写字母。代码如下
import re done = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz', } pattern = re.compile(r'[a-z]') pattern2 = re.compile(r'[A-Y]') raw = input() ripe = '' for s in raw: if re.match(pattern, s): for d in done.keys(): if done.get(d).__contains__(s): ripe += d elif re.match(pattern2, s): ripe += chr(ord(s.lower()) + 1) elif s.__eq__('Z'): ripe += 'a' else: ripe += s print(ripe)