curriculum/challenges/english/blocks/daily-coding-challenges-python/698a1a73ade5ac0e19180fa0.md
Given a donor blood type and a recipient blood type, determine whether the donor can give blood to the recipient.
Each blood type consists of:
"A", "B", "AB", or "O""+" or "-"Blood types will be one of the valid letters followed by an Rh factor. For example, "AB+" and "O-" are valid blood types.
Letter Rules:
"O" can donate to other letter type."A" can donate to "A" and "AB"."B" can donate to "B" and "AB"."AB" can donate only to "AB".Rh Rules:
"-") can donate to both "-" and "+"."+") can donate only to "+".Both letter and Rh rule must pass for a donor to be able to donate to the recipient.
can_donate("B+", "B+") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("B+", "B+"), True)`)
}})
can_donate("O-", "AB-") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O-", "AB-"), True)`)
}})
can_donate("O+", "A-") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O+", "A-"), False)`)
}})
can_donate("A+", "AB+") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("A+", "AB+"), True)`)
}})
can_donate("A-", "B-") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("A-", "B-"), False)`)
}})
can_donate("B-", "AB+") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("B-", "AB+"), True)`)
}})
can_donate("B-", "A+") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("B-", "A+"), False)`)
}})
can_donate("O-", "O+") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O-", "O+"), True)`)
}})
can_donate("O+", "O-") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O+", "O-"), False)`)
}})
can_donate("AB+", "AB-") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("AB+", "AB-"), False)`)
}})
def can_donate(donor, recipient):
return donor
def can_donate(donor, recipient):
donor_type = donor[:-1]
donor_rh = donor[-1]
recipient_type = recipient[:-1]
recipient_rh = recipient[-1]
abo_compatibility = {
"O": ["A", "B", "AB", "O"],
"A": ["A", "AB"],
"B": ["B", "AB"],
"AB": ["AB"]
}
abo_match = recipient_type in abo_compatibility[donor_type]
rh_match = (
donor_rh == "-" or
(donor_rh == "+" and recipient_rh == "+")
)
return abo_match and rh_match