Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Blood distribution by ssk8
def distribute(avail: int, need: int, out: int) -> tuple[int, int, int]:
if need >= avail:
return 0, need - avail, out + avail
else:
return avail - need, 0, out + need
def distribute_blood(
blood_avail: dict[str, int], blood_needs: dict[str, int]
) -> dict[str, dict[str, int]]:
blood_types = ("A", "B", "AB", "O")
dist = {t: {ts: 0 for ts in blood_types} for t in blood_types}
for type in blood_types:
blood_avail[type], blood_needs[type], dist[type][type] = distribute(
blood_avail[type], blood_needs[type], dist[type][type]
)
for type in ("A", "B"):
blood_avail[type], blood_needs["AB"], dist[type]["AB"] = distribute(
blood_avail[type], blood_needs["AB"], dist[type]["AB"]
)
for type in ("A", "B", "AB"):
blood_avail["O"], blood_needs[type], dist["O"][type] = distribute(
blood_avail["O"], blood_needs[type], dist["O"][type]
)
return dist
Feb. 16, 2024