如何使这个用于查找彩色数字的 Python 函数对任何数字进行操作?

How to make this Python function for finding colourful numbers operate on any number?

提问人:Alastair909 提问时间:4/12/2023 最后编辑:Tom KarzesAlastair909 更新时间:4/12/2023 访问量:226

问:

彩色数字是指在其数字列表中不包含重复项的数字 + 其数字子集的乘积。例如,263 的乘积是 [2, 6, 3, 2x6, 6x3, 2x6x3]。这些都是不同的数字,所以 263 是五颜六色的。

我编写了一些代码来允许长度不超过 3 的整数这样做。它确实有效。它创建数字和子集的列表,然后将该列表转换为集合以删除重复项,然后将列表与集合进行比较。如果没有重复项,则长度将相同。

但是,它应该针对 int 的任何长度进行缩放。它也远非简洁。我应该如何使其具有可扩展性并使其可读性?

这是我的功能。它适用于长度不超过 3 的值。

def is_colorful(num):

    str_num = str(num)
    combos = []

    if len(str_num) == 1:

        x = int(str_num[0])

        combos.append(x)

    if len(str_num) == 2:

        x = int(str_num[0])
        y = int(str_num[1])

        combos.append(x)
        combos.append(y)
        combos.append(x*y)

    if len(str_num) == 3:

        x = int(str_num[0])
        y = int(str_num[1])
        z = int(str_num[2])
        
        combos.append(x)
        combos.append(y)
        combos.append(z)
        combos.append(x*y)
        combos.append(y*z)
        combos.append(x*y*z)

    set_combos = set(combos)
    print(set_combos)
    
    return True if len(set_combos) == len(combos) else False
python 重复 设置 追加 可读性

评论

2赞 Tom Karzes 4/12/2023
对于 263 示例,您不需要考虑 2x3 吗?这与 6 不冲突吗?所以看起来 263 不是一个丰富多彩的数字。
0赞 Alastair909 4/12/2023
它是通过子集完成的,因此通过拆分每个数字或数字组来完成。263 拆分为 2,6,3、26、63,它不会拆分为 23,因为它们不连续
1赞 Kelly Bundy 4/12/2023
那么你说的是子字符串,而不是子集。
0赞 Tom Karzes 4/12/2023
@Alastair909 263 的数字是 {2, 6, 3}。它的一个子集是 {2, 3}。你似乎不明白“集合”和“子集”的定义。集合是无序的。它们不是序列。

答:

1赞 constantstranger 4/12/2023 #1

下面是一种扩展它的方法:

def is_colorful(num):
    # disqualify as colorful if:
    #    - there are multiple digits and one of them is 0 or 1
    #    - any digit is a duplicate
    n, prods = num, set()
    while n:
        digit = n % 10
        if prods and digit in (0,1) or digit in prods:
            return False
        n, prods = n // 10, prods | {digit}
    
    # disqualify as colorful if:
    #    - the product of the digits in any subset is a duplicate
    n, prods = num, set()
    while n:
        digit, curProds = n % 10, set()
        for prd in (p * digit for p in (f for group in (prods, [1]) for f in group)):
            if prd in prods:
                return False
            curProds.add(prd)
        n, prods = n // 10, prods | curProds
    return True

top = 1
while top <= 10_000_000:
    colorful = []
    for i in range(top):
        if is_colorful(i):
            colorful.append(i)
    print(f'for 0 to {top}, ', 
        f'the count of colorful numbers is {len(colorful)}), ',
        f'percent = {100 * len(colorful) / top}%')
    top *= 10

输出:

for 0 to 1, the count of colorful numbers is 1, percent = 100.0%
for 0 to 10, the count of colorful numbers is 10, percent = 100.0%
for 0 to 100, the count of colorful numbers is 74, percent = 74.0%
for 0 to 1000, the count of colorful numbers is 454, percent = 45.4%
for 0 to 10000, the count of colorful numbers is 2194, percent = 21.94%
for 0 to 100000, the count of colorful numbers is 7690, percent = 7.69%
for 0 to 1000000, the count of colorful numbers is 17530, percent = 1.753%
for 0 to 10000000, the count of colorful numbers is 23290, percent = 0.2329%