Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a15cadf5f240d05a264955e Challenge 320: Blood Bank 29 challenge-320

--description--

Given an array of the inventory at a blood bank and an array of patient blood type requests, return a string in the format "X of Y patients served". Where X is the maximum number of patients that can receive blood from the bank's inventory, and Y is the total number of patients.

Each entry in both arrays is one of the following blood types: "AB", "A", "B", or "O".

Compatibility rules:

  • "AB" can receive from any blood type.
  • "A" can receive from "A" and "O".
  • "B" can receive from "B" and "O".
  • "O" can only receive from "O".

Duplicate entries in the given arrays represent quantity.

--hints--

triage_blood(["O", "A", "B", "AB"], ["O", "A", "B", "AB"]) should return "4 of 4 patients served".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "A", "B", "AB"], ["O", "A", "B", "AB"]), "4 of 4 patients served")`)
}})

triage_blood(["A", "A", "B", "B", "AB"], ["O", "A", "B", "B", "B"]) should return "3 of 5 patients served".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["A", "A", "B", "B", "AB"], ["O", "A", "B", "B", "B"]), "3 of 5 patients served")`)
}})

triage_blood(["O", "A", "B", "AB"], ["AB", "AB", "AB", "AB", "AB"]) should return "4 of 5 patients served".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "A", "B", "AB"], ["AB", "AB", "AB", "AB", "AB"]), "4 of 5 patients served")`)
}})

triage_blood(["O", "O", "O", "O", "O"], ["O", "A", "B", "AB"]) should return "4 of 4 patients served".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "O", "O", "O", "O"], ["O", "A", "B", "AB"]), "4 of 4 patients served")`)
}})

triage_blood(["A", "O", "B", "AB", "B", "AB", "O", "A", "A"], ["O", "A", "B", "AB", "A", "B", "A", "A", "B", "A", "B"]) should return "8 of 11 patients served".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["A", "O", "B", "AB", "B", "AB", "O", "A", "A"], ["O", "A", "B", "AB", "A", "B", "A", "A", "B", "A", "B"]), "8 of 11 patients served")`)
}})

triage_blood(["O", "B", "AB", "AB", "O", "A", "A", "AB", "O", "B", "B", "AB", "A", "B", "AB"], ["O", "A", "B", "B", "A", "B", "AB", "A", "B", "A", "O", "AB", "AB", "O"]) should return "13 of 14 patients served".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "B", "AB", "AB", "O", "A", "A", "AB", "O", "B", "B", "AB", "A", "B", "AB"], ["O", "A", "B", "B", "A", "B", "AB", "A", "B", "A", "O", "AB", "AB", "O"]), "13 of 14 patients served")`)
}})

--seed--

--seed-contents--

def triage_blood(bank, patients):

    return bank

--solutions--

def triage_blood(bank, patients):
    b = {}
    p = {}
    for t in bank:
        b[t] = b.get(t, 0) + 1
    for t in patients:
        p[t] = p.get(t, 0) + 1

    saved = 0

    def serve(patient, donors):
        nonlocal saved
        for donor in donors:
            n = min(p.get(patient, 0), b.get(donor, 0))
            p[patient] = p.get(patient, 0) - n
            b[donor] = b.get(donor, 0) - n
            saved += n

    serve("O", ["O"])
    serve("A", ["A", "O"])
    serve("B", ["B", "O"])
    serve("AB", ["AB", "A", "B", "O"])
    return f"{saved} of {len(patients)} patients served"