【每日一题0719】 连续计数

:woman_mage:赫敏想知道目标值在一个目标对象中最长的连续元素的大小。请使用python来编写一个函数,将收到两个参数: items 和 key,任务是返回给定items中连续key的最长段的长度。

备注:items和key将是一个整数或字符串(只包含字母),如果key没有出现在items中,则返回0。

示例:
items=90000, key=0 返回–> 4
items=“abcdaaadse”, key=“a” 返回–> 3
items=“abcdaaadse”, key=“z” 返回–> 0

题目难度:中等
题目来源:codewars

def get_consective_items(items, key): 
    pass

assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0

一直找

def get_consective_items(items,key):
    items,key=str(items),str(key)
    res=0
    for i in range(1,len(items)+1):
        if items.find(key*i)!=-1:
            res=i
    return res

assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0
1 Like
def get_consective_items(items, key):
    max_num = 0
    next_num = 0
    for i in range(len(str(items))):
        if str(items)[i] == str(key):
            next_num = next_num + 1
            if max_num < next_num:
                max_num = next_num
        else:
            next_num = 0
    return max_num

assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0
def get_consective_items(items, key):
    a = 0
    list2 = []
    key = str(key)
    items = list(str(items))
    for x in items:
        if x == key:
            a += 1
        else:
            list2.append(a)
            a = 0
    list2.append(a)
    return  max(list2)

assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0
def get_consective_items(items, key):
    res = []
    max_num = 0
    length = len(str(items))
    for i in range(length):
        if str(items)[i] == str(key):
            max_num += 1
        else:
            res.append(max_num)
            max_num = 0
    else:
        res.append(max_num)
    return max(res)


assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadseaaaaaa", "a") == 6
assert get_consective_items("abcdaaadse", "z") == 0
def get_consective_items(items,key):
    items,key = str(items),str(key)
    if key not in items:
        return 0
    list1 = re.findall(f'{key}+',items)
    list2 = list(map(lambda x:len(x),list1))
    return max(list2)

assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadseaaaaaa", "a") == 6
assert get_consective_items("abcdaaadse", "z") == 0
class Solution:
    @classmethod
    def get_consective_items(cls, item, key):
        """
        1、使用re模块findall方法查找key在item的位置,返回list
        2、返回list中最长的元素的长度
        :param item:
        :param key:
        :return:
        """
        a = re.findall(f'{str(key)}+', str(item))
        if len(a) != 0:
            return len(max(a))
        return 0


assert_that(Solution.get_consective_items(90000, 0), equal_to(4))
assert_that(Solution.get_consective_items("abcdaaadse", "a"), equal_to(3))
assert_that(Solution.get_consective_items("abcdaaadse", "z"), equal_to(0))

def get_consective_items(items, key):
    """ 0719 连续计数"""
    # items = "90000"
    # key = 0
    num = 0
    li = []
    for i in range(len(str(items))):
        if str(items)[i] == str(key):
            num += 1
            continue
        else:
            li.append(num)
            num = 0
    li.append(num)
    return max(li)
def get_consective_items(items, key):
    def iter_():
        if isinstance(items, int):
            n = 0
            tmp = items
            while tmp > 0:
                yield tmp % 10
                tmp = tmp // 10
        else:
            yield from items

    max_len = 0
    tmp_len = 0
    for i in iter_():
        if i == key:
            tmp_len += 1
        else:
            max_len = max(max_len, tmp_len)
            tmp_len = 0
    return max(max_len, tmp_len)


assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0

def get_consective_items(items, key):
    import re
    flag = re.findall(f'{str(key)}+', str(items))
    return len(max(flag)) if flag !=[] else 0

assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0
def get_consective_items(items, key):
    con_list = [0]
    count = 0
    for i in str(items):
        if i == str(key):
            count += 1
        else:
            if count > 0:
                con_list.append(count)
            count = 0
    con_list.append(count)
    return max(con_list)


assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0
关闭