Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def generate_random_items(item_count, value_variants):
- items = []
- for item_index in range(item_count):
- value = random.choice(value_variants)
- items.append({
- 'id': item_index,
- 'name': f"Item {item_index + 1}",
- 'value': value
- })
- return items
- def get_item_value_sum(selection):
- result = 0
- for item in selection:
- result += item['value']
- return result
- def random_selection(items, value_sum):
- while True:
- selection = []
- while get_item_value_sum(selection) < value_sum:
- # Adding item that is not already selected
- while True:
- item = random.choice(items)
- if item not in selection:
- selection.append(item)
- break
- new_value_sum = get_item_value_sum(selection)
- if new_value_sum == value_sum:
- # Returning item ids
- return [ item['id'] for item in selection ]
- elif new_value_sum > value_sum:
- # Resetting set generation
- selection = []
- def generate_random_records(record_count, items, value_sum):
- records = []
- for item_index in range(record_count):
- selection = random_selection(items, value_sum)
- selection.sort()
- records.append({
- 'id': item_index,
- 'selection': selection
- })
- return records
- def make_combination_hash(combination):
- string_ints = [str(int) for int in combination]
- return ",".join(string_ints)
- if __name__ == "__main__":
- # Generating random items
- items = generate_random_items(
- item_count=10,
- value_variants=[1.5, 3.0]
- )
- # Generating item selections
- records = generate_random_records(
- record_count=200,
- items=items,
- value_sum=12
- )
- # Gathering records to groups with the same selection
- combinations = {}
- for record in records:
- combination_hash = make_combination_hash(record['selection'])
- if combination_hash in combinations:
- combinations[combination_hash].append(record['id'])
- else:
- combinations[combination_hash] = [record['id']]
- for combination_hash, combination_records in combinations.items():
- print(f"{combination_hash}: {len(combination_records)}")
- print(f"Generated {len(records)} records with {len(combinations)} combinations")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement