3.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 698a1a73ade5ac0e19180fa0 | Challenge 197: Blood Type Compatibility | 29 | challenge-197 |
--description--
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 letter:
"A","B","AB", or"O" - And an Rh factor:
"+"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:
- Negative (
"-") can donate to both"-"and"+". - Positive (
"+") can donate only to"+".
Both letter and Rh rule must pass for a donor to be able to donate to the recipient.
--hints--
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)`)
}})
--seed--
--seed-contents--
def can_donate(donor, recipient):
return donor
--solutions--
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