【每日一题20220526】数字密码1

:mage:‍ 数字密码的规则首先是将字母表中的每个字母进行编号:

a   b   c   d   e   f   g   h   i   j   k   l   m 
1   2   3   4   5   6   7   8   9   10  11  12  13 
n   o   p   q   r   s   t   u   v   w   x   y   z 
14  15  16  17  18  19  20  21  22  23  24  25  26 

然后给定一串字母和一个密钥数字,便编写一个函数,将每个字母对应的字母表编号,按顺序加上对应位置上的密钥数字,最后返回加密后的列表。

【示例】
输入:“scout”, 1939
输出:[20, 12, 18, 30, 21]
解释:

   s   c  o   u   t 
   19  3  15  21  20 
+   1  9   3   9   1 
 --------------------------- 
    20 12  18  30  21

题目难度:中等
题目来源:Codewars-Digital cypher

def solution(message:str, key:int)-> list:
    # your code here

assert solution("scout", 1939) == [20, 12, 18, 30, 21]
assert solution("masterpiece",1939) == [14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]
def solution(message: str, key: int) -> list:
    # your code here
    return [int(i) + int(j) for i, j in zip([(ord(i)-96) for i in message], (str(key) * ((len(message) // len(str(key)) + 1)))[:len(message)])]


assert solution("scout", 1939) == [20, 12, 18, 30, 21]
assert solution("masterpiece", 1939) == [14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]
1 个赞
def solution(message:str, key:int)-> list:
    int_list = list(str(key))
    k = len(int_list)
    new_list = []
    for i in range(len(message)):
        new_element = ord(message[i])-96 + int(int_list[i%k])
        new_list.append(new_element)
    return new_list

assert solution("scout", 1939) == [20, 12, 18, 30, 21]
assert solution("masterpiece", 1939) == [14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]

def solution(message: str, key: int) -> list:
    # your code here
    i = 0
    alphabet_num = [(ord(i)-96) for i in message]
    while True:
        if i < len(alphabet_num):
            for k in str(key):
                if i < len(alphabet_num):
                    alphabet_num[i] += int(k)
                    i += 1
                else:
                    break
        else:
            break
    return alphabet_num

def encode(message,key):
    return [ord(value)-96+int(str(key)[index%len(str(key))]) for index,value in enumerate (message)]

def solution(message:str, key:int)-> list:
    numbers = []
    for i in range(len(message)):
        if i >= len(str(key)):
            i1 = i % len(str(key))
            numbers.append(ord(message[i]) + int(str(key)[i1]) - 96)
        else:
            numbers.append(ord(message[i]) + int(str(key)[i]) - 96)
    return numbers


assert solution("scout", 1939) == [20, 12, 18, 30, 21]
assert solution("masterpiece",1939) == [14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]
def solution(message:str, key:int)-> list:
    numbers = []
    for i in range(len(message)):
        numbers.append(ord(message[i]) + int(str(key)[i % len(str(key))]) - 96)
    return numbers


assert solution("scout", 1939) == [20, 12, 18, 30, 21]
assert solution("masterpiece",1939) == [14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]
def solution(message:str, key:int)-> list:
    dict_data = dict(zip([chr(i) for i in range(97,123)],[i for i in range(1,27)]))
    new_key = list(str(key) * (len(message) // len(str(key))) + str(key)[0:len(message) % len(str(key))])
    result = [int(new_key[i]) + [dict_data.get(i) for i in list(message)][i] for i in range(len(message))]
    return result

assert solution("scout", 1939) == [20, 12, 18, 30, 21]
assert solution("masterpiece",1939) == [14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]
assert solution("a",1939) == [2]
def solution(message:str, key:int)-> list:
    key_list=[(str(key) * (len(message) // len(str(key)) + 1))[:len(message)][i] for i in range(len(message))]
    return [ord(message[i])-96+int(key_list[i]) for i in range(len(message))]


assert solution("scout", 1939) == [20, 12, 18, 30, 21]
assert solution("masterpiece", 1939) == [14, 10, 22, 29, 6, 27, 19, 18, 6, 12, 8]