【每日一题20220531】数字密码3

: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 

如果将每个字母对应的字母表编号,按顺序加上对应位置上的密钥数字,得到效果:

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

我们的任务是,给定一串字母和加密后的数字列表,请编写一个函数,找出加密的密钥数字。

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

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

assert solution("scout", [20, 12, 18, 30, 21]) == 1939
assert solution("nomoretears", [15, 17, 14, 17, 19, 7, 21, 7, 2, 20, 20]) == 12
def solution(message: str, nums: list) -> int:
    # your code here
    i = 0
    alphabet_num = [(ord(i) - 96) for i in message]
    while True:
        if i < len(alphabet_num):
            for k in nums:
                if i < len(alphabet_num):
                    alphabet_num[i] = str(k - alphabet_num[i])
                    i += 1
                else:
                    break
        else:
            break
    for iin range(1, len(nums) + 1):
        key = alphabet_num[:i]
        if (key * len(nums))[:len(nums)] == alphabet_num:
            return int(''.join(key))


assert solution("scout", [20, 12, 18, 30, 21]) == 1939
assert solution("nomoretears", [15, 17, 14, 17, 19, 7, 21, 7, 2, 20, 20]) == 12
def solution(message:str, nums: list)-> int:
    # your code here
    new_list = [str(nums[i] - (ord(message[i])-96)) for i in range(len(nums)) ]
    length = len(nums)
    for i in range(1,length+1):
        if (new_list[:i]*length)[:length] == new_list:
            return int(''.join(new_list[:i]))
2 个赞
def solution(message:str, nums: list)-> int:
    encrypt = [value - ord(message[index]) + 96 for index,value in enumerate(nums)]
    guess =0
    ln=len(encrypt)
    while guess <ln:
        pairs=ln//(guess+1)
        if pairs == 1 and encrypt[guess+1:] == encrypt[:ln-guess-1]:
            return int(''.join([str(i) for i in encrypt[:guess+1]]))
        elif pairs == 1 and encrypt[guess+1:] != encrypt[:ln-guess-1]:
            guess +=1
        elif pairs > 1:
            for count in range(2,pairs+1):
                if encrypt[:guess+1] != encrypt[(guess+1)*(count-1):(guess+1)*count]:
                    guess += 1
                    break
            else:
                return int(''.join([str(i) for i in encrypt[:guess+1]]))
    
def solution(message: str, nums: list) -> int:
    message_int = [ord(message[i]) - 96 for i in range(len(message))]
    res_list = [str(nums[j] - message_int[j]) for j in range(len(message_int))]
    for i in range(1, len(res_list)):
        key = res_list[:i]
        if ''.join(key * len(nums))[:len(nums)] == ''.join(res_list):
            return int(''.join(res_list)[:i])


assert solution("scout", [20, 12, 18, 30, 21]) == 1939
assert solution("nomoretears", [15, 17, 14, 17, 19, 7, 21, 7, 2, 20, 20]) == 12