【每日一题20220507】舍5逐末

:mage:‍ 给定两个分别代表区间的起点和终点数字,请编写一个函数,计算它们中舍去含有5的数字之后的数字个数。 输入数字均为正数,并且包含终点数字。

【示例】
输入:1,9
输出:8
解释:4,17会舍弃5和15,得到4,6,7,8,9,10,11,12,13,14,16,17,一共是12个数字。

题目难度:简单
题目来源:CodeWars-Don’t give me five!

def solution(start: int, end: int)-> int:
    # your code here

assert solution(1, 9) == 8
assert solution(4, 17) == 12
def solution(start: int, end: int)-> int:
    # your code here
    return len([item for item in range(start,end+1) if "5" not in str(item)])

assert solution(1, 9) == 8
assert solution(4, 17) == 12
assert solution(0, 106) == 87

你这个耗时太长了哦,最好还是数学题

示例的输入和解释没有对应上耶 :joy:

def solution(start: int, end: int) -> int:
    diff = int((end - start) / 5)
    return end - start - diff + 1

好的多谢,现在初学阶段对优化耗时这些还没有过多考虑,以后要在这些方面也多思考了。

去除的是带有5的数字,不是5的倍数哦,如52

1 个赞

不对吧,这实现的是剔除5的倍数吧,题目是剔除含有5的数字

确实

def solution(start: int, end: int)-> int:
    # your code here
    return len(list(filter(lambda x: "5" not in str(x), [i for i in range(start, end + 1)])))
def solution(start: int, end: int)-> int:
    return len([i for i in range(start,end+1) if '5' not in str(i)])
def solution(start: int, end: int)-> int:
    func=lambda x,y:x if '5' in str(y) else x+[y]
    return len(reduce(func, [[], ] + list(range(start, end + 1))))
def solution(start: int, end: int) -> int:
    return len([i for i in range(start, end + 1) if '5' not in str(i)])


assert solution(1, 9) == 8
assert solution(4, 17) == 12
def solution(start: int, end: int)-> int:
    # your code here
    # count = 0
    # for i in range(start, end + 1):
    #     if '5' not in str(i):
    #         count += 1
    # return count
    return len([i for i in range(start, end + 1) if '5' not in str(i)])

assert solution(1, 9) == 8
assert solution(4, 17) == 12

    lists = ['5' not in str(i) for i in range(start,end+1)]
    return sum(lists)
def solution051903(start: int, end: int)-> int:
    return sum('5' in str(i) for i in range(start, end + 1))
def solution(start: int, end: int)-> int:
    return len([i for i in range(start,end+1) if '5' not in str(i)])

assert solution(1, 9) == 8
assert solution(4, 17) == 12