A colourful number is one that includes no duplicates in the list of it digits + the products of the subsets of its digits. For example, the products of 263 are [2, 6, 3, 2x6, 6x3, 2x6x3]. These are all different numbers, so 263 is colourful.
I made some code to allow this for integers up to length 3. It does work. It makes a list of the digits and subsets, then converts that list into a set to remove duplicates, then compares list to set. If there are no duplicates the lengths will be the same.
However it should scale for any length of int. It is also very far from concise. How should I make it scale and make it readable?
Here is my function. It works for values up to length 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