【每日一题0823】寻找高频连续数字

:woman_mage:已知列表里有一组元素[1, 2.5, 6, 6, 7, 7, 7, 2,5, 12],请编写一个python3函数,找出列表中连续出现次数最多的数字及起始下标。

题目难度:中等
题目来源:面试真题
贡献者:玉栋同学

def most_feq(nums:list) -> (int, int):
    pass

assert most_feq([1, 2.5, 6, 6, 7, 7, 7, 2,5, 12]) == (7, 4)
from itertools import groupby
def most_feq(nums: list) -> (int, int, int):
    max_time = max(len(list(v)) for k, v in groupby(nums)) #最大次数
    max_index=1 #最大次数的起始位置
    for index, (k, v) in enumerate(groupby(nums)):
        if len(list(v)) == max_time:
            max_index+= index
            return  k,max_index
assert most_feq([1, 2.5, 6, 6, 7, 7, 7, 2,5, 12]) == (7, 4)
def most_feq(nums: list):
    max_time = 0  # 最大连续出现次数
    continuous_time = 1  # 第几次连续出现
    pre_element = None  # 记录上一个元素
    max_time_num = None  # 最大次数的值
    for index, i in enumerate(nums):
        if i == pre_element:
            max_time_num = i
            continuous_time += 1
            max_time = max(continuous_time, max_time)
        else:
            pre_element = i
            continuous_time = 1
    return max_time_num, max_time_num - max_time

assert most_feq([1, 2.5, 6, 6, 7, 7, 7, 2, 5, 12]) == (7, 4)
def most_feq(nums: list) -> (int, int):
    temp_dict = {}
    for n in nums:
        if n in temp_dict:
            temp_dict[n] += 1
        else:
            temp_dict[n] = 1
    max_k = max(temp_dict, key=temp_dict.get)
    return max_k, nums.index(max_k)


assert most_feq([1, 2.5, 6, 6, 7, 7, 7, 2, 5, 12]) == (7, 4)

试下

assert most_feq([1, 1,2.5, 6, 6, 7, 7, 7, 2, 5, 12]) == (7, 4)

max_index有点看不懂为啥以1开始,为啥加index,我改了一下,你看下哈

    max_time = max(len(list(v)) for k, v in groupby(nums))  # 最大次数
    max_index = 0  # 最大次数的起始位置
    for index, (k, v) in enumerate(groupby(nums)):
        len_arr: int = len(list(v))
        if len_arr == max_time:
            return k, max_index
        else:
            max_index += len_arr

    assert most_feq([1, 2, 6, 6, 7, 7, 7, 2, 1, 12]) == (7, 4)
    assert most_feq([1, 2.5, 6, 6, 6, 7, 7, 2, 7, 7, 5, 12]) == (6, 2)
def most_feq(nums:list) -> (int, int):
    if nums is None or len(nums) == 0:
        return None, -1
    max_item: int = nums[0]
    max_count: int = 1
    last_item: int = nums[0]
    last_count: int = 1
    for i in range(1, len(nums)):
        if last_item == nums[i]:
            last_count += 1
            if last_count > max_count:
                max_item = nums[i]
                max_count = last_count
        else:
            last_item = nums[i]
            last_count = 1
    return max_item, nums.index(max_item)


def test_most_feq():
    assert most_feq([1, 2, 6, 6, 7, 7, 7, 2, 1, 12]) == (7, 4)
    assert most_feq([1, 2.5, 6, 6, 6, 7, 7, 2, 7, 7, 5, 12]) == (6, 2)

试试运行下这个

assert most_feq([1,1, 2, 6, 6, 7, 7, 7, 2, 1, 12]) == (7, 4)

max_index=1 起始位置就是从nums第一个数起

你这个断言是不是错了,应该断言(7, 5)吧?前面加了一位呀
我之前的没做连续判断,修改了一下

def most_feq(nums: list) -> (int, int):
    index = []
    count = 1
    for x in range(len(nums)):
        if x == len(nums) - 1:
            index.append((x + 1 - count, count))
        elif nums[x] == nums[x+1]:
            count += 1
        else:
            index.append((x+1-count, count))
            count = 1
    max_index, _ = max(index, key=lambda y: y[1])
    return nums[max_index], max_index


assert most_feq([1, 5, 2.5, 5, 6, 6, 7, 7, 7, 2, 5, 5]) == (7, 6)
assert most_feq([1, 1, 2.5, 6, 6, 7, 7, 7, 2, 5, 12]) == (7, 5)

    public ArrayList<Integer> mostFeq(ArrayList<Integer> arrayList) {
        // todo: 已知列表里有一组元素[1, 2,5, 6, 6, 7, 7, 7, 2,5, 12],请编写一个python3函数,找出列表中连续出现次数最多的数字及起始下标。
        int start = 0;
        int end = 1;
        int len = arrayList.size();
        Map<String, ArrayList<Integer>> map = new HashMap<>();
        int step = 0;
        int max = 0;
        while (end < len) {
            if (arrayList.get(end).equals(arrayList.get(start))) {
                step++;
                end++;
            } else {
                ArrayList<Integer> integers1 = new ArrayList<>();
                integers1.add(arrayList.get(start));
                integers1.add(start);
                integers1.add(start + step);
                if (end - start + 1 > max) {
                    map.put("max", integers1);
                    max = end - start + 1;
                }
                start = end;
                end++;
                step = 0;
            }
        }
        ArrayList<Integer> integers2 = new ArrayList<>();
        integers2.add(arrayList.get(arrayList.size() - 1));
        integers2.add(start);
        integers2.add(end);
        if (end - start + 1 > max) {
            map.put("max", integers2);
        }
        return map.get("max");
    }

    @Test
    public void mostFeqTest() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(56);
        list.add(56);
        list.add(56);
        list.add(56);
        list.add(56);
        list.add(56);
        list.add(56);
        list.add(56);
        list.add(56);
        list.add(74);
        list.add(99);
        list.add(99);
        list.add(99);
        list.add(86);
        list.add(180);
        list.add(181);
        list.add(181);
        list.add(181);
        list.add(181);
        list.add(181);
        list.add(181);
        list.add(181);
        ArrayList<Integer> integers = mostFeq(list);
    }
def most_feq(nums:list) -> (int, int):
    temp_nums=set(nums)
    # 假设一个最大的数
    max_number = nums[0]
    # 假设次数
    count = 0
    for i in temp_nums:
        temp=nums.count(i)
        if temp > count:
            count = temp
            max_number= i
    return (max_number, nums.index(max_number))

def most_feq(nums:list) -> (int, int):
    """  找出列表中连续出现次数最多的数字及起始下标"""
    # nums = [1, 2.5, 6, 6, 7, 7, 7, 2, 5, 12]
    item = 1
    dic = {}
    for i in range(0,len(nums)-1):
        j = i+1
        if nums[i] != nums[j]:
            dic[nums[i]] = item
        else:
            j += 1
            item += 1
    max_nums = max(dic, key=dic.get)
    max_index = nums.index(max_nums)
    return max_nums,max_index

 assert most_feq([1, 2.5, 6, 6, 7, 7, 7, 2, 5, 12]) == (7, 4)

def most_feq(nums: list) -> (int, int):
    max_num = 0  # 最大的数
    con_time = 0  # 连续次数
    max_time = 0  # 最大的连续次数
    max_index = 0  # 最大连续次数的元素下标
    pro_num = 0  # 上一个元素
    for i in range(0, len(nums) - 1):
        if pro_num == nums[i]:
            con_time += 1
            if con_time >= max_time:
                max_num = nums[i]
                max_time = con_time
            if  con_time >= 2: # 连续次数大于2,最大值的下标=当前值的下标-连续次数
                max_index = i - con_time
            elif max_time == 1: # 少于2次的直接拿上一个元素的下标
                max_index = i - 1
        else:
            pro_num = nums[i]
            con_time = 0
    return max_num, max_index


assert most_feq([1, 2.5, 6, 6, 7, 7, 7, 2, 5, 12]) == (7, 4)
assert most_feq([1, 2.5, 6, 6, 6, 7, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 5, 12]) == (2, 10)
assert most_feq([1, 2.5, 6, 6, 7, 7, 2, 2, 2, 8, 8, 8, 8, 8, 5, 12]) == (8, 9)
assert most_feq([1, 1, 2.5, 6, 6, 8, 8, 8, 2, 5, 12]) == (8, 5)
assert most_feq([1, 0.2, 4, 4, 6, 7, 8]) == (4, 2)
关闭