【每日一题20220530】数字密码2

: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 

然后,给定一个加密后的数字列表和一个密钥数字,请编写一个函数,将每个数字,按顺序减去对应位置上的密钥数字,最后返回加密前的字符串文本。

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

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

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

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

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


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


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

def solution(ls,key):
    key_ls = [int(i) for i in list(str(key))]
    length = len(key_ls)
    total = ""
    for i in range(len(ls)):
        total += chr(96+ls[i]-key_ls[i%length])
    return total
def encode(message,key):
    return ''.join([chr(value+96-int(str(key)[index%len(str(key))])) for index,value in enumerate (message)])

def solution(nums: list, key: int)-> str:
    # your code here
    for i in range(len(nums)):
        nums[i] -= int(str(key)[i%len(str(key))])
    return "".join([chr(96+i) for i in nums])

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

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