【每日一题1021】统计频率

:woman_mage: 给定一个字符串words和分隔符sep,请编写一个函数,统计字符串words中每个元素出现的次数,使用sep进行拼接。

示例:
输入:“hello world”,输出:" 1-1-3-3-2-1-1-2-1-3-1"

题目难度:简单
题目来源:codewars: Frequency sequence

def freq_seq(words: str, sep: str) -> str:
    pass

assert freq_seq("hello world", "-") == "1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") == "1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"
def freq_seq(words: str, sep: str) -> str:
    times_dict = {}
    for w in words:
        if w not in times_dict.keys():
            times_dict[w] = 1
        else:
            times_dict[w] += 1
    return sep.join([str(times_dict[w]) for w in words])


assert freq_seq("hello world", "-") == "1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") == "1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"
1 Like
def freq_seq(words:str,sep:str)->str:
    temp_list=[]
    for i in words:
        count=words.count(i)
        temp_list.append(str(count))
    return sep.join(temp_list)
assert freq_seq("hello world", "-")=="1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") =="1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"
2 Likes
def freq_seq(words: str, sep: str) -> str:
    return sep.join([str(list(words).count(item)) for item in list(words) ])

assert freq_seq("hello world", "-") == "1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") == "1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"

整复杂啦

做题可以,业务代码这样写可读性就降低了。

def freq_seq(words: str, sep: str) -> str:
    counts = {}
    for i in words:
        if i in counts.keys():
            counts[i] += 1
        else:
            counts[i] = 1
    return sep.join([str(counts[i]) for i in words])

assert freq_seq("hello world", "-") == "1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") == "1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"
def freq_seq(words: str, sep: str) -> str:
    # 定义统计字典
    counts = {}
    # 将传入字符串转为列表
    result = list(words)
    # 遍历每个字符
    for s in words:
        # 统计每个字符次数
        counts[s] = counts.get(s,0) + 1
    # 遍历列表
    for i in range(len(words)):
        # 将每个字符替换成该字符统计的次数
        result[i] = str(counts[result[i]])
    # 通过分隔符将列表转为字符串
    return sep.join(result)


assert freq_seq("hello world", "-") == "1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") == "1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"

Python参考题解:

def freq_seq(words: str, sep: str) -> str:
    return sep.join([str(words.count(x)) for x in words])

assert freq_seq("hello world", "-") == "1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") == "1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"

思路:利用列表推导式,遍历字符串words,同时使用字符串count方法统计字符出现的次数,最后用join方法拼接整个列表的字符。

知识点:

  1. 列表:推导式
  2. 字符串:count()join()方法。
def freq_seq(words: str, sep: str) -> str:
    list1 = []
    count_list = []
    for i in words:
        list1.append(i)
    for j in list1:
        count = str(list1.count(j))
        if count is None:
            pass
        else:
            count_list.append(count)
    return f'{sep}'.join(count_list)


assert freq_seq("hello world", "-") == "1-1-3-3-2-1-1-2-1-3-1"
assert freq_seq("19999999", ":") == "1:7:7:7:7:7:7:7"
assert freq_seq("^^^**$", "x") == "3x3x3x2x2x1"
public String freqSeq(String words,String step){
        String[] split = words.split("");
        String res ="";
        LinkedHashMap<String,Integer> hs = new LinkedHashMap<>();
        for (String s: split) {
            if (hs.get(s) !=null){
                hs.put(s, hs.get(s)+1);
            }else {
                hs.put(s, 1);
            }
        }
        for (String s: hs.keySet()) {
            res += hs.get(s)+step;
        }
        return res.substring(0, res.length()-1);
    }
def freq_seq(words:str,sep:str):
    res=[]
    for i in words:
        res.append(str(words.count(i)))
    return sep.join(res)
关闭