【每日一题0111】键盘布局

:mage:t2: 我们的电脑键盘上的数字键区通常的布局是:

7 8 9
4 5 6
1 2 3
0   .  

而我们手机上常用的数字键盘布局是:

1 2 3
4 5 6
7 8 9
  0

请编写一个函数,将在PC键盘键入的对应键位,对应翻译成在手机上的对应输入键位。例如,PC键盘键位9则对应手机键位是3。

示例:
输入:0,输出:0。
输入:789,输出:123。

题目难度:简单
题目来源:CodeWars:Keypad horror

def solution(keys: str) -> str:
    # your code

assert solution("789") == "123"
assert solution("000") == "000"
assert solution("0789456123") == "0123456789"
assert solution("94561") == "34567"
def solution(keys: str) -> str:
    b = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0, '.']
    c = [1, 2, 3, 4, 5, 6, 7, 8, 9,  0]
    result=''
    for i in range(len(keys)):
        result += str(c[b.index(int(keys[i]))])
    return result


assert solution("789") == "123"
assert solution("000") == "000"
assert solution("0789456123") == "0123456789"
assert solution("94561") == "34567"
def solution(keys: str) -> str:
    key_mappings = {
        "7": "1",
        "8": "2",
        "9": "3",
        "1": "7",
        "2": "8",
        "3": "9"
    }
    string = ""
    for key in keys:
        key = key_mappings[key] if key in key_mappings else key
        string += key
    return string
dict1 = {
    "1": "7",
    "2": "8",
    "3": "9",
    "7": "1",
    "8": "2",
    "9": "3"
}


def solution(keys: str) -> str:
    str1 = ''
    for i in list(keys):
        if i in dict1:
            str1 += dict1[i]
        else:
            str1 += i
    return str1

def solution17(c_str:str):
    computer_b=[7,8,9,4,5,6,1,2,3,0]
    mobile_b=[1,2,3,4,5,6,7,8,9,0]
    m_str=''
    for i in c_str:
        location_index=computer_b.index(int(i))
        m_str+=str(mobile_b[location_index])
    return m_str
assert solution17("789") == "123"
assert solution17("000") == "000"
assert solution17("0789456123") == "0123456789"
assert solution17("94561") == "34567"
def solution(keys: str) -> str:
    com_list = ['7', '8', '9', '4', '5', '6', '1', '2', '3']
    return ''.join(['0' if i == '0' else str(com_list.index(i)+1) for i in list(keys)])
def solution(keys: str) -> str:
    mode = "0789456123"
    return "".join([str(mode.find(w)) for w in keys])
3 Likes

这个妙啊

def solution(keys: str) -> str:
    key = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0]
    value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    dict_data = dict(zip(key, value))
    result = []
    for x in keys:
        result.append(str(dict_data[int(x)]))
    return ''.join(result)


assert solution("789") == "123"
assert solution("000") == "000"
assert solution("0789456123") == "0123456789"
assert solution("94561") == "34567"
def solution(keys: str) -> str:
    pc = '7894561230.'
    phone = '1234567890'
    return ''.join([phone[(pc.index(i))] for i in keys])


assert solution("789") == "123"
assert solution("000") == "000"
assert solution("0789456123") == "0123456789"
assert solution("94561") == "34567"
关闭