题解 | #简单密码#

简单密码

http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

方法1:

code_str =input()
dic ={"1":1,"abc":2,"def":3,"ghi":4,"jkl":5,"mno":6,"pqrs":7,"tuv":8,"wxyz":9,"0":0}
out_str = []
for x in code_str:
    if x.islower():
        # for key in dic: 
        #     dict.get(key, default=None)返回指定键的值,如果值不在字典中返回default值
        #    dict.items()以列表返回可遍历的(键, 值) 元组数组
        for key,value in dic.items():
            if x in key:
                out_str.append(str(value))     
    elif x.isupper():
        if x.lower()=='z':
            x = 'a'
        else:
            x = chr(ord(x.lower())+1)
        out_str.append(x)
    elif x.isdigit():
        out_str.append(x)
out_str_join = ''.join(out_str)
print(out_str_join)

方法2:

a = input()
d = {"1":1,"abc":2,"def":3,"ghi":4,"jkl":5,"mno":6,"pqrs":7,"tuv":8,"wxyz":9,"0":0}
L = ''
for x in a:
    if x.islower():
        for key,value in d.items():
            if x in key:
                L = L + str(value)
    elif x.isupper():
        if x.lower() != 'z':
            m = chr(ord(x.lower())+1)
        else:
            m = 'a'
        L = L + m
    else:
        L = L +x
print(L)
全部评论

相关推荐

评论
1
收藏
分享
牛客网
牛客企业服务