Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sorted_k_partitions(seq, k):
- n = len(seq)
- groups = [] # a list of lists, currently empty
- def generate_partitions(i):
- if i >= n:
- yield list(map(tuple, groups))
- else:
- if n - i > k - len(groups):
- for group in groups:
- group.append(seq[i])
- yield from generate_partitions(i + 1)
- group.pop()
- if len(groups) < k:
- groups.append([seq[i]])
- yield from generate_partitions(i + 1)
- groups.pop()
- result = generate_partitions(0)
- result = [sorted(ps, key=lambda p: (len(p), p)) for ps in result]
- result = sorted(result, key=lambda ps: (*map(len, ps), ps))
- return result
- K = 11
- P = 1
- list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
- list_2 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
- nums = list([i+1 for i in range(K)])
- ways = sorted_k_partitions(nums, P)
- #for way in ways:
- # print(way)
- def check_ans(way):
- summa = sum(way[0])
- for group in way:
- if sum(group) != summa:
- return False
- return True
- def get_for_k(k, list):
- nums = list[:k]
- for p in range(1, k+1):
- print("K={} P={}".format(k, p))
- ways = sorted_k_partitions(nums, p)
- for way in ways:
- if check_ans(way):
- print(way)
- # первое - K-кол-во чисел, которые мы рассматриваем в последовательности, далее массив - ряд чисел
- get_for_k(12, list_1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement