【每日一题20220315】最大相连数

:woman_mage:给定一个字符串格式的数字,请编写一个函数,找出所有5个相连数字中最大的数字。

示例:
输入: "1234567890"
输出:67890

题目难度:简单
题目来源:CodeWars:Largest 5 digit number in a series

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

assert solution("1234567890") == 67890
assert solution("1234567898765") == 98765
assert solution("731674765") == 74765
def solution(num: str) -> int:
    nums_list = []
    l = len(num) - 4
    for i in range(l):
        nums_list.append(int(num[i:i + 5]))
    return max(nums_list)


assert solution("1234567890") == 67890
assert solution("1234567898765") == 98765
assert solution("731674765") == 74765
def solution(num: str) -> str:
    return int(max(num[a:a + 5] for a in range(len(num) - 4)))
    def solution(num: str) -> int:
        l = len(num)
        if l >= 5:
            max = int(num[:5])
            for i in range(l-4):
                if int(num[i:i+5]) > max:
                    max = int(num[i:i+5])
            return max
def solution(num: str) -> int:
    num_list = [num[i:5+i] for i in range(len(num)-4)]
    int_list = [int(num) for num in num_list]
    return max(int_list)

assert solution("1234567890") == 67890
assert solution("1234567898765") == 98765
assert solution("731674765") == 74765
def solution(num: str) -> int:
    # your code here
    nums = num[:-4]
    for i in range(9, -1, -1):
        count = nums.count(str(i))
        c = [-1]
        for j in range(count):
            c.append(nums.find(str(i), c[j] + 1, len(nums)))
        h = []
        if len(c) > 1:
            for i in c:
                if i != -1:
                    h.append(int(num[i: i + 5]))
            if h:
                return max(h)
    return ''


assert solution("12345678909999999999") == 99999
assert solution("1234567898765") == 98765
assert solution("731674765") == 74765
def solution(num: str) -> int:
    # your code here
    return max([int(num[i:i+5]) for i in range(len(num)-4)])


assert solution("1234567890") == 67890
assert solution("1234567898765") == 98765
assert solution("731674765") == 74765
def solution(num: str) -> int:
    # 最大相连树
    res = []
    for i in range(len(num)-4):
        res.append(int(num[i:i+5]))
    print(res)
    return max(res)

assert solution("1234567890") == 67890
assert solution("1234567898765") == 98765
assert solution("731674765") == 74765
def solution(num: str) -> int:
    return max(int(num[i:i + 5]) for i in range(len(num) - 4))
def solution(num: str) -> int:
    # 找出最大的数字N
    num_list = [int(n) for n in num[:-4]]
    max_digit = max(num_list)

    # 找出N****,返回最大值,即为结果
    list_competor = []
    for i in range(len(num_list)):
        if num_list[i] == max_digit:
            list_competor.append(int(num[i:i+5]))
    return max(list_competor)
def solution(num: str)-> int:
    # result=[]
    # for i in range(len(num)-4):
    #     result.append(num[i:i+5])
    # return int(sorted(result,key=int)[-1])
    return max(int(num[i:i+5]) for i in range(len(num) - 4))

最先想到的是搞个移动窗口,发现好些小伙伴都这样实现

max([num[i:i + 5:] for i in range(len(num) - 4)])

于是换个思路:先找出老大,再让他们带小弟出来打架

import regex  # re 库 findall 方法不能重叠匹配

def solution(num: str) -> int:
    avail_leader = num[:len(num) - 4:]
    return int(max(regex.findall(f'{max(avail_leader)}....', num, overlapped=True)))
def solution(num: str) -> int:
    return max([int(num[i:i+5]) for i in range(len(num)-4)])

assert solution("1234567890") == 67890
assert solution("1234567898765") == 98765
assert solution("731674765") == 74765