Advertisement
PetrovskiyYY

Grouping persons to groups with minimal size based on their selection with making minimal changes of

Sep 4th, 2022
1,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | Source Code | 0 0
  1. import random
  2.  
  3.  
  4. def generate_random_items(item_count, value_variants):
  5.     items = []
  6.     for item_index in range(item_count):
  7.         value = random.choice(value_variants)
  8.         items.append({
  9.             'id': item_index,
  10.             'name': f"Item {item_index + 1}",
  11.             'value': value
  12.         })
  13.     return items
  14.  
  15.  
  16. def get_item_value_sum(selection):
  17.     result = 0
  18.     for item in selection:
  19.         result += item['value']
  20.     return result
  21.  
  22.  
  23. def random_selection(items, value_sum):
  24.     while True:
  25.         selection = []
  26.         while get_item_value_sum(selection) < value_sum:
  27.             # Adding item that is not already selected
  28.             while True:
  29.                 item = random.choice(items)
  30.                 if item not in selection:
  31.                     selection.append(item)
  32.                     break
  33.  
  34.             new_value_sum = get_item_value_sum(selection)
  35.             if new_value_sum == value_sum:
  36.                 # Returning item ids
  37.                 return [ item['id'] for item in selection ]
  38.             elif new_value_sum > value_sum:
  39.                 # Resetting set generation
  40.                 selection = []
  41.  
  42.  
  43. def generate_random_records(record_count, items, value_sum):
  44.     records = []
  45.     for item_index in range(record_count):
  46.         selection = random_selection(items, value_sum)
  47.         selection.sort()
  48.         records.append({
  49.             'id': item_index,
  50.             'selection': selection
  51.         })
  52.     return records
  53.  
  54.  
  55. def make_combination_hash(combination):
  56.     string_ints = [str(int) for int in combination]
  57.     return ",".join(string_ints)
  58.  
  59.  
  60. if __name__ == "__main__":
  61.     # Generating random items
  62.     items = generate_random_items(
  63.         item_count=10,
  64.         value_variants=[1.5, 3.0]
  65.     )
  66.  
  67.     # Generating item selections
  68.     records = generate_random_records(
  69.         record_count=200,
  70.         items=items,
  71.         value_sum=12
  72.     )
  73.  
  74.     # Gathering records to groups with the same selection
  75.     combinations = {}
  76.     for record in records:
  77.         combination_hash = make_combination_hash(record['selection'])
  78.         if combination_hash in combinations:
  79.             combinations[combination_hash].append(record['id'])
  80.         else:
  81.             combinations[combination_hash] = [record['id']]
  82.  
  83.     for combination_hash, combination_records in combinations.items():
  84.         print(f"{combination_hash}: {len(combination_records)}")
  85.  
  86.     print(f"Generated {len(records)} records with {len(combinations)} combinations")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement