Advertisement
magneto903

math_marathon_2020_task_1

Dec 6th, 2020 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. def sorted_k_partitions(seq, k):
  2.     n = len(seq)
  3.     groups = []  # a list of lists, currently empty
  4.  
  5.     def generate_partitions(i):
  6.         if i >= n:
  7.             yield list(map(tuple, groups))
  8.         else:
  9.             if n - i > k - len(groups):
  10.                 for group in groups:
  11.                     group.append(seq[i])
  12.                     yield from generate_partitions(i + 1)
  13.                     group.pop()
  14.  
  15.             if len(groups) < k:
  16.                 groups.append([seq[i]])
  17.                 yield from generate_partitions(i + 1)
  18.                 groups.pop()
  19.  
  20.     result = generate_partitions(0)
  21.  
  22.     result = [sorted(ps, key=lambda p: (len(p), p)) for ps in result]
  23.    
  24.     result = sorted(result, key=lambda ps: (*map(len, ps), ps))
  25.  
  26.     return result
  27.  
  28.  
  29. K = 11
  30. P = 1
  31.  
  32. list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  33. list_2 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
  34.  
  35. nums = list([i+1 for i in range(K)])
  36.  
  37. ways = sorted_k_partitions(nums, P)
  38.  
  39. #for way in ways:
  40. #    print(way)
  41.  
  42. def check_ans(way):
  43.     summa = sum(way[0])
  44.     for group in way:
  45.         if sum(group) != summa:
  46.             return False
  47.     return True
  48.  
  49.  
  50.  
  51.  
  52. def get_for_k(k, list):
  53.     nums = list[:k]
  54.  
  55.     for p in range(1, k+1):
  56.         print("K={} P={}".format(k, p))
  57.         ways = sorted_k_partitions(nums, p)
  58.         for way in ways:
  59.             if check_ans(way):
  60.                 print(way)
  61.  
  62. # первое - K-кол-во чисел, которые мы рассматриваем в последовательности, далее массив - ряд чисел
  63. get_for_k(12, list_1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement