Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Blood distribution by book1978
def distribute_blood(blood_avail, blood_needs):
d = ["AB", "B", "A", "O"]
p = {"A": ["A", "AB"], "B": ["B", "AB"], "AB": ["AB"], "O": ["AB", "A", "B", "O"]}
res = {
"A": {"A": 0, "B": 0, "AB": 0, "O": 0},
"B": {"A": 0, "B": 0, "AB": 0, "O": 0},
"AB": {"A": 0, "B": 0, "AB": 0, "O": 0},
"O": {"A": 0, "B": 0, "AB": 0, "O": 0},
}
for i in d:
for k in p[i]:
x = min(blood_avail[i], blood_needs[k])
blood_avail[i] -= x
blood_needs[k] -= x
res[i][k] += x
return res
if __name__ == "__main__":
assert distribute_blood(
{"A": 150, "B": 100, "AB": 0, "O": 0}, {"A": 100, "B": 100, "AB": 50, "O": 0}
) == {
"A": {"A": 100, "B": 0, "AB": 50, "O": 0},
"B": {"A": 0, "B": 100, "AB": 0, "O": 0},
"AB": {"A": 0, "B": 0, "AB": 0, "O": 0},
"O": {"A": 0, "B": 0, "AB": 0, "O": 0},
}
assert distribute_blood(
{"A": 10, "B": 10, "AB": 20, "O": 20}, {"A": 20, "B": 10, "AB": 30, "O": 0}
) == {
"A": {"A": 10, "B": 0, "AB": 0, "O": 0},
"B": {"A": 0, "B": 10, "AB": 0, "O": 0},
"AB": {"A": 0, "B": 0, "AB": 20, "O": 0},
"O": {"A": 10, "B": 0, "AB": 10, "O": 0},
}
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 2, 2024