【每日一题20220426】加密算法

:mage:‍ 给定一个随机字符串(可能包含数字、字母和符号),请编写一个函数,将它们进行加密,具体规则是:(1)统计每个ascii字母的出现次数 (2)返回一个长度为26(a-z)的字符串,将每个字母自己的次数填入到自己对应的位置上。每个字母最多出现9次。

【示例】
输入:“$aaaa#bbb*cc^fff!z”
输出:43200300000000000000000001
解释:a出现4次,填入第一位;b出现3次,填在第二位;c出现2次,填在第三位;f出现3次,填在第六位;z出现1次,填在第26位。

题目难度:简单
题目来源:CodeWars-Simple decrypt algo

def solution(src: str)-> str:
    # your code here

assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"
def solution(src: str):
    list_src = list(src)
    h = "00000000000000000000000000"
    list_h = list(h)
    i=0
    while i < len(list_src):
        if str.isalpha(list_src[i]):
            count_num = list_src.count(list_src[i])
            location = ord(list_src[i])-97
            list_h.pop(location)
            list_h.insert(location,str(count_num))
            i+=count_num

        else:
            i+=1
    return ''.join(list_h)
def solution(src: str) -> str:
    # your code here
    dic = {}
    str1 = ''
    ls = [i for i in string.ascii_letters[:26]]
    for k in ls:
        for i in src:
            if i in ls:
                if src.count(i) <= 9:
                    dic[i] = src.count(i)
                else:
                   dic[i] = 9
        if k in list(dic.keys()):
            str1 = str1 + str(dic[k])
        else:
            str1 = str1 + '0'
    return str1

if __name__ == '__main__':

    assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
    assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"
def solution(src: str)-> str:
    # your code here
    ascii_list = [chr(97+i) for i in range(26)]
    ascii_dict = dict.fromkeys(ascii_list,"0")
    for item in src:
        if ascii_dict.get(item):
            ascii_dict[item] = str(int(ascii_dict[item]) + 1)
    return "".join(list(ascii_dict.values()))


assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"
def solution(src: str)-> str:
    # your code here
    result = [x for x in '0' * 26]
    for string in set(list(src)):
        if 97 <= ord(string) <= 122:
            result[ord(string) - 97] = str(list(src).count(string))
    return "".join(result)
1 个赞

def solution(src: str) -> str:
    from string import ascii_lowercase as al
    dic = dict.fromkeys(al, 0)
    for s in set(src.lower()):
        if al.__contains__(s):
            dic[s] = src.lower().count(s)
    return "".join(str(s) for s in dic.values())

def solution(src: str) -> str:
    result = "".join([chr(x) for x in range(97, 123)])
    for s in list(src):
        anscil = ord(s)
        if 97 <= anscil <= 122:
            if result.find(s) >= 0:
                result = result.replace(s, str(src.count(s)))
    for r in list(result):
        try:
            f = float(r)
        except ValueError:
            result = result.replace(r, "0")
    return result

assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"
def solution(src: str) -> str:
    l = [chr(97 + i) for i in range(26)]
    result = []
    for i in l:
        if i in src:
            result.append(src.count(i))
        else:
            result.append(0)
    return ''.join([str(i) for i in result])


assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"
def solution(src: str)-> str:
    li = [str(src.count(chr(i))) if chr(i) in src else "0" for i in range(97, 123)]
    return "".join(li)

assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"
def solution(src: str)-> str:
    import string
    #from collections import Counter
    return ''.join(str(test_key.count(i)) for i in string.ascii_lowercase)
    #return ''.join([str(Counter(src)[i]) for i in string.ascii_lowercase])
from string import ascii_lowercase
def decrypt(test_key):
    return ''.join(str(test_key.count(a)) for a in ascii_lowercase)
def solution(src: str)-> str:
    import re
    a = re.findall('[a-z]', src)
    ls = [i for i in string.ascii_letters[:26]]
    b=[k for k,v in itertools.groupby(a)]
    for i in ls:
        if i in b and a.count(i)<=9:ls[ls.index(i)]=str(a.count(i))
        elif i in b and a.count(i)>9:ls[ls.index(i)] = '9' 
        else:ls[ls.index(i)]='0'
    return ''.join(ls)
def solution(src: str)-> str:
    return ''.join([str(src.count(i)) if src.count(i) <= 9 else '9' for i in list(string.ascii_lowercase)])
def solution(src: str)-> str:
    # your code here
    l = list('0' for i in range(26))
    for i in src:
        if ord(i) >= 97 and ord(i) <= 122:
            # if int(l[ord(i) - 97]) < 9:
            #     l[ord(i) - 97] = str(int(l[ord(i) - 97]) + 1)
            # else:
            #     l[ord(i) - 97] = str(9)

            l[ord(i) - 97] = str(int(l[ord(i) - 97]) + 1) if int(l[ord(i) - 97]) < 9 else str(9)
    return ''.join(l)



assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"
assert solution("aaaaaaaaaa") == "90000000000000000000000000"
def solution(src: str) -> str:
    base_list = [chr(i) for i in range(97, 123)]
    src_list = [src[i] for i in range(len(src)) if src[i].isalpha()]
    return ''.join([str(src_list.count(base_list[i])) for i in range(len(base_list))])


assert solution("$aaaa#bbb*cc^fff!z") == "43200300000000000000000001"
assert solution("z$aaa#ccc%eee1234567890") == "30303000000000000000000001"