87 lines
2.4 KiB
Python
87 lines
2.4 KiB
Python
from collections import Counter
|
|
from pprint import pprint
|
|
|
|
basis = [1,2,3,4,5,6]
|
|
|
|
def get_all_subsets(l):
|
|
if l == []:
|
|
return [[]]
|
|
subs = get_all_subsets(l[1:])
|
|
return subs + [[l[0]] + el for el in subs]
|
|
|
|
all_subsets = get_all_subsets(basis)
|
|
all_subsets = sorted([sub for sub in all_subsets if len(sub) >= 2])
|
|
# all_subsets = sorted([sub for sub in all_subsets if len(sub) >= 2])
|
|
# all_subsets = sorted([sub for sub in all_subsets if sum(sub) < 9])
|
|
|
|
def product(sub):
|
|
if len(sub) == 0:
|
|
return 1
|
|
return sub[0] * product(sub[1:])
|
|
|
|
def sort_keys(counter: Counter) -> Counter:
|
|
out = Counter()
|
|
for key in sorted(counter.keys()):
|
|
out[key] = counter[key]
|
|
return out
|
|
|
|
def print_sorted_by_key(counter: Counter) -> Counter:
|
|
print('{')
|
|
for key in sorted(counter.keys()):
|
|
print(f' {key}: {counter[key]},')
|
|
print('}')
|
|
|
|
|
|
sum_counter = Counter(sum(sub) for sub in all_subsets)
|
|
product_counter = Counter(product(sub) for sub in all_subsets)
|
|
product_counter_unique_sum = Counter(product(sub) for sub in all_subsets if sum_counter[sum(sub)] > 1)
|
|
|
|
#debug
|
|
print_sorted_by_key(sum_counter)
|
|
print_sorted_by_key(product_counter)
|
|
|
|
print(sum([3,4,5,6]))
|
|
print(product([3,4,5,6]))
|
|
print('a', [(sub, sum(sub)) for sub in all_subsets if sum(sub)==18])
|
|
print('b', [(sub, product(sub)) for sub in all_subsets if product(sub)==360])
|
|
print(sum([3,4,5,6]))
|
|
print(sum([1,3,4,5,6]))
|
|
|
|
|
|
print('xxx')
|
|
|
|
# solve it one way
|
|
for sub in all_subsets:
|
|
if sum_counter[sum(sub)] > 1 and product_counter_unique_sum[product(sub)] == 1:
|
|
print(sub, sum(sub), product(sub))
|
|
|
|
print('xxx')
|
|
|
|
# solve it one way
|
|
for sub in all_subsets:
|
|
if sum_counter[sum(sub)] > 1 and product_counter[product(sub)] == 1:
|
|
print(sub, sum(sub), product(sub))
|
|
|
|
print('yyy')
|
|
|
|
# solve it another way
|
|
for answer_sum in range(100):
|
|
if sum_counter[answer_sum] == 0:
|
|
continue
|
|
has_some_determined = False
|
|
has_some_undetermined = False
|
|
for sub in all_subsets:
|
|
if not sum(sub) == answer_sum:
|
|
continue
|
|
if product_counter[product(sub)] > 1:
|
|
has_some_undetermined += True
|
|
if product_counter[product(sub)] == 1:
|
|
has_some_determined += True
|
|
|
|
if not (has_some_determined and has_some_undetermined):
|
|
continue
|
|
|
|
print(answer_sum, has_some_determined, has_some_undetermined)
|
|
# print(sub, sum(sub), product(sub))
|
|
# print()
|