Blood distribution

Blood distribution

Your mission is to distribute available blood of different types to patients requiring transfusions, considering each blood type's compatibility restrictions. The blood supply is not always sufficient to meet all demands. You'll be provided with the quantities of each blood type and the needs of each patient type. Your goal is to optimally allocate the available blood, respecting compatibility rules. Keep in mind that there could be multiple optimal solutions, those that utilize the maximum possible amount of the available blood supply.

Compatibility Restrictions:

example

Input: The blood quantities (blood_avail) and the blood needs (blood_needs) for each blood type as dictionaries (dict) with string keys (str) and integer values (int).

Output: The output will be the distribution of blood quantities for each blood type as dictionary of dictionaries (dict) with string keys (str) and integer values (int).

Examples:

distribute_blood({'A': 30, 'B': 60, 'AB': 30, 'O': 60}, {'A': 30, 'B': 40, 'AB': 50, 'O': 60}) == 
{
   'A': {'A': 30, 'B': 0,  'AB': 0,  'O': 0},
   'B': {'A': 0,  'B': 40, 'AB': 20, 'O': 0},
  'AB': {'A': 0,  'B': 0,  'AB': 30, 'O': 0},
   'O': {'A': 0,  'B': 0,  'AB': 0, 'O': 60}
}

distribute_blood({'A': 40, 'B': 30, 'AB': 30, 'O': 40}, {'A': 30, 'B': 35, 'AB': 45, 'O': 30}) == 
{
   'A': {'A': 30, 'B': 0,  'AB': 10, 'O': 0},
   'B': {'A': 0,  'B': 30, 'AB': 0,  'O': 0},
  'AB': {'A': 0,  'B': 0,  'AB': 30, 'O': 0},
   'O': {'A': 0,  'B': 5,  'AB': 5,  'O': 30}
}

How it is used: This mission focuses on efficiently distributing available blood quantities to meet patients' needs based on compatibility. The function `distribute_blood` calculates the appropriate blood allocation, respecting compatibility restrictions. By completing this mission, you will gain insights into efficient blood distribution algorithms and their practical applications.

Preconditions:
0 ≤ blood quantities;
0 ≤ blood needs.

33
dig