Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import combinations
- def generate_combinations(variables, limit):
- memo = {}
- def product_count(values):
- count = 1
- for value in values:
- count *= len(value)
- return count
- def combine_variables(vars_list):
- if not vars_list:
- return [{}]
- key = tuple((var, tuple(values)) for var, values in vars_list)
- if key in memo:
- return memo[key]
- results = []
- current_var, options = vars_list[0]
- for subset in (combinations(options, i) for i in range(1, len(options) + 1)):
- for option in subset:
- if product_count([option]) > limit:
- break
- for result in combine_variables(vars_list[1:]):
- new_combination = {current_var: option, **result}
- if product_count(new_combination.values()) <= limit:
- results.append(new_combination)
- memo[key] = results
- return results
- all_combinations = combine_variables(list(variables.items()))
- def merge_combinations(combinations):
- merged = []
- for combo in combinations:
- if not any(all(set(combo[var]).issubset(m[var]) for var in combo) for m in merged):
- merged.append(combo)
- return merged
- return merge_combinations(all_combinations)
- variables = {
- "gender": ["total", "women", "men"],
- "country of birth": ["Norway", "Finland", "Sweden", "Denmark"],
- "year": ["2019", "2020", "2021", "2022", "2023"]
- }
- row_limit = 13
- optimal_configs = generate_combinations(variables, row_limit)
- print(f"Optimal Request Configurations (Total: {len(optimal_configs)}):")
- for config in optimal_configs:
- print(config)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement