Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First Blood solution in Clear category for Blood distribution by Alex_JC
def distribute_blood(blood_avail, blood_needs):
compatibility = {"O": ("O"),
"A": ("A", "O"),
"B": ("B", "O"),
"AB": ("AB", "A", "B", "O")}
def check(i, j, blood=0):
for n in compatibility[j]:
if n == i:
blood = min(blood_avail[n], blood_needs[j])
blood_needs[j] -= blood
blood_avail[n] -= blood
return blood
return {i: {j: check(i, j) for j in blood_avail} for i in blood_avail}
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!")
Jan. 26, 2024