Advertisement
BlueDrink9

Simultaneous equation puzzle generator

Jan 19th, 2021
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. import itertools
  2. import string
  3.  
  4. weight_sizes = [2,3,4] # the variables to use
  5. weight_needed = 15
  6. max_weights_per_sol = 7
  7.  
  8. def main():
  9.     print_solutions()
  10.  
  11. def print_solutions():
  12.     weight_sizes.sort()
  13.     letters = assign_variables()
  14.     solutions = get_solutions()
  15.     print_var_letters(letters)
  16.     for sol in solutions:
  17.         print_solution(sol, letters)
  18.  
  19. def assign_variables():
  20.     wvars = {}
  21.     for i in range(len(weight_sizes)):
  22.         wvars[weight_sizes[i]] = string.ascii_lowercase[i]
  23.     return wvars
  24.  
  25. def print_solution(sol, letters):
  26.     out = ""
  27.     for weight in set(sol):
  28.         out += str(sol.count(weight))
  29.         out += letters[weight]
  30.         out += " + "
  31.     print(out[:-3])
  32.  
  33. def print_var_letters(letters):
  34.     print("Let ", end="")
  35.     for size, letter in letters.items():
  36.         print(letter + " = " + str(size) + ", ", end="")
  37.     print()
  38.  
  39. def get_solutions():
  40.     solutions = []
  41.     n = max_weights_per_sol
  42.     for n in range(1,max_weights_per_sol+1):
  43.         solutions_for_n = get_unique_solutions(n)
  44.         solutions += (solutions_for_n)
  45.     # solutions = get_unique_solutions(n)
  46.     return solutions
  47.  
  48. def get_unique_solutions(n):
  49.     sorted_sols = sort_with_sublists(calculate_solutions(n))
  50.     unique_sols = list(x for x,_ in
  51.             itertools.groupby(sorted_sols))
  52.     return unique_sols
  53.  
  54. def sort_with_sublists(lst):
  55.     for sub in lst:
  56.         sub = sub.sort()
  57.     lst.sort()
  58.     return lst
  59.  
  60. def calculate_solutions(n):
  61.     permutations = multichoose(n, weight_sizes)
  62.     permutations = remove_invalid_sums(permutations)
  63.     # print(list(permutations))
  64.     return containsonly(weight_sizes, permutations)
  65.  
  66. def remove_invalid_sums(lsts):
  67.     out = []
  68.     # Cannot sum to weight_needed if its smallest possible value is repeated
  69.     # more times than the largest possible quotient
  70.     maximum_lenth = weight_needed/min(weight_sizes)
  71.     for l in lsts:
  72.         if len(l) > maximum_lenth:
  73.             continue
  74.         if sum(l) == weight_needed:
  75.             out.append(l)
  76.     return out
  77.  
  78. def containsonly(nums, lst):
  79.     to_remove = []
  80.     for combo in lst:
  81.         for num in combo:
  82.             if num not in nums:
  83.                 to_remove.append(combo)
  84.     for combo in to_remove:
  85.         if combo in lst:
  86.             lst.remove(combo)
  87.     return lst
  88.  
  89. # https://github.com/ekg/multichoose/blob/master/multichoose.py
  90. # https://stackoverflow.com/questions/37711817/generate-all-possible-outcomes-of-k-balls-in-n-bins-sum-of-multinomial-catego
  91. def multichoose(k, objects):
  92.     """n multichoose k multisets from the list of objects.  n is the size of
  93.    the objects."""
  94.     j,j_1,q = k,k,k  # init here for scoping
  95.     r = len(objects) - 1
  96.     a = [0 for i in range(k)] # initial multiset indexes
  97.     while True:
  98.         yield [objects[a[i]] for i in range(0,k)]  # emit result
  99.         j = k - 1
  100.         while j >= 0 and a[j] == r: j -= 1
  101.         if j < 0: break  # check for end condition
  102.         j_1 = j
  103.         while j_1 <= k - 1:
  104.             a[j_1] = a[j_1] + 1 # increment
  105.             q = j_1
  106.             while q < k - 1:
  107.                 a[q+1] = a[q] # shift left
  108.                 q += 1
  109.             q += 1
  110.             j_1 = q
  111.  
  112.  
  113. main()
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement