【每日一题20220517】数码

:mage:‍ 给定两个整数 l 和 r ,对于所有满足1 ≤ l ≤ x ≤ r ≤ 10^9 的 x ,请编写一个函数,找出x的所有约数。 对于每个约数,只保留最高位的那个数码。求1~9每个数码出现的次数。

【示例】
输入:(1, 4)
输出:(4,2,1,1,0,0,0,0,0)
解释:1≤x≤4,所以整数有1,2,3,4,约数分别为:1(1),2(1,2),3(1,3),4(1,2,4)。所以数码1出现4次,数码2出现2次,数码3出现1次,数码4出现1次。

题目难度:简单
题目来源:牛客网-数码

def solution(left: int, right: int)-> tuple:
    # your code here

assert solution(1, 4) == (4,2,1,1,0,0,0,0,0)
def solution(left: int, right: int)-> tuple:
    # your code here
    yueshu_dict = dict.fromkeys(range(1,10),0)
    for i in range(left,right+1):
        flag = 1
        while flag <= i:
            if i%flag == 0:
                yueshu_dict[flag] += 1
            flag += 1
    return tuple(yueshu_dict.values())

assert solution(1, 4) == (4,2,1,1,0,0,0,0,0)
def solution(left: int, right: int)-> tuple:
    # your code here
    if left >= 1 and right <= 10**9:
        approximation_list = []
        for i in (rng := range(left, right + 1)):
            n = 1
            while n <= i:
                if i % n == 0:
                    approximation_list.append(n)
                n += 1
        count_num = [approximation_list.count(i) for i in rng] + [0 for i in range(9-len(rng))]
        return tuple(count_num)
    else:
        -1

assert solution(1, 4) == (4,2,1,1,0,0,0,0,0)
def solution(left: int, right: int)-> tuple:
    if left<right:
        l=left
        r=right
    else:
        l=right
        r=left

    counts=[]
    list=[]
    for i in range(l,r+1):
        for j in range(1,i+1):
            if(i%j==0):
                list.append(j)
    list.sort()
    for i in range(1,10):
        num=list.count(i)
        counts.append(num)
    return tuple(counts)

assert solution(1, 4) == (4,2,1,1,0,0,0,0,0)
assert solution(4, 1) == (4,2,1,1,0,0,0,0,0)
def solution(l:int,r:int):
    list_num = []
    for i in list(range(l,r+1)):
        for j in list(range(1,i+1)):
            if i % j ==0:
                list_num.append(j)
    list_num2 = list(range(1,10))
    for x in range(len(list_num2)):
        if list_num2[x] in list_num:
            list_num2[x] = list_num.count(list_num2[x])
        else:
            list_num2[x] = 0
    return tuple(list_num2)


if __name__ == '__main__':
    print(solution(1, 4))
def solution(left: int, right: int)-> tuple:
    b=[y if j%y==0 else None for j in range(left,right+1) for y in range(left,right+1)]
    return tuple(b.count(i) for i in range(1,10))
def solution(left: int, right: int)-> tuple:
    result_map=dict().fromkeys(range(1,10),0)
    for x in range(left, right + 1):
        for i in range(1, int(math.sqrt(x)) + 1):
            if x % i == 0:
                if x // i != i:
                    result_map[int(str(i)[0])] += 1
                    result_map[int(str(x // i)[0])] += 1
                else:
                    result_map[int(str(i)[0])] += 1
    return tuple(result_map.value())
def solution(left: int, right: int)-> tuple:
    # your code here
    d = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0}
    for i in range(left, right + 1):
        for j in range(left, right + 1):
                if i % j == 0:
                    d[str(j)[0]] += 1
    return tuple(d.values())


assert solution(1, 4) == (4,2,1,1,0,0,0,0,0)
def solution(left: int, right: int)-> tuple:
    l=[j for i in range(left,right+1) for j in range(1,i+1) if i%j==0]
    d=dict((i,l.count(i)) for i in range(1,10))
    return tuple(d.values())

assert solution(1, 4) == (4,2,1,1,0,0,0,0,0)
def solution(left: int, right: int)-> tuple:
    res=[]
    res_list=[]
    for i in range(left,right+1):
        for j in range(1,i+1):
            if i%j==0:
                res_list.append(j)
    for i in range(1,10):
        res.append(res_list.count(i))
    return tuple(res)

assert solution(1, 4) == (4,2,1,1,0,0,0,0,0)