【每日一题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
2 个赞
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

1 个赞
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

def get_consective_items(items, key):
    num_list = []
    n = 0
    for i in str(items):
        if str(key) != i:
            num_list.append(n)
            n = 0
            continue
        n += 1
    num_list.append(n)
    return int(sorted(num_list)[-1])
assert get_consective_items(90000, 0) == 4
assert get_consective_items("abcdaaadse", "a") == 3
assert get_consective_items("abcdaaadse", "z") == 0
import re

def get_consective_items(items, key):
    results = re.findall(f"{str(key)}+", str(items))
    return len(max(results)) if results else 0


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

==> Process finished with exit code 0
def get_consective_items(items, key):
    max1=0
    items=str(items)
    key=str(key)
    for i in range(len(items)):
        if items[i] == key:
            result=1
            for j in range(i+1,len(items)):
                if items[j]==key:
                    result+=1
                else:break
            max1 =max(max1,result)
            if j ==len(items)-1:
                return max1
    return max1
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):
 return max([n:=n+1 if i == str(key) else 0 for i in str(items)])

def get_consective_items(items, key):
    items = str(items)
    key = str(key)
    if key not in items:
        return 0
    maxlist = []
    for i in range(len(items)):
        if items[i] == key:
            count = 0
            for j in range(i,len(items)):
                if items[i] == items[j]:
                    count += 1
                    if j==len(items)-1:
                        maxlist.append(count)
                else:
                    maxlist.append(count)
                    break
        else:
            continue
    return max(maxlist)
def get_consective_items(items, key):
    count,tmp = 0,0
    for i in str(items):
        if i != str(key):
            tmp = 0
            continue
        tmp+=1
        if tmp > count:
            count = tmp

    return count

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):
    str_items=str(items)
    l_max = 0
    try:
        left_step=str_items.index(str(key))
        for i in range(left_step, len(str_items)):
            if str_items[i] == str(key):
                l=1
                for j in range(i+1,len(str_items)):
                    if str_items[j] == str(key):
                        l+=1
                    else:
                        break
                if l>l_max:
                    l_max=l
            else:
                continue
    except:
        l_max = 0
    return l_max


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