Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/698a1a73ade5ac0e19180fa0.md
T
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

160 lines
3.2 KiB
Markdown

---
id: 698a1a73ade5ac0e19180fa0
title: "Challenge 197: Blood Type Compatibility"
challengeType: 29
dashedName: 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`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("B+", "B+"), True)`)
}})
```
`can_donate("O-", "AB-")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O-", "AB-"), True)`)
}})
```
`can_donate("O+", "A-")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O+", "A-"), False)`)
}})
```
`can_donate("A+", "AB+")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("A+", "AB+"), True)`)
}})
```
`can_donate("A-", "B-")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("A-", "B-"), False)`)
}})
```
`can_donate("B-", "AB+")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("B-", "AB+"), True)`)
}})
```
`can_donate("B-", "A+")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("B-", "A+"), False)`)
}})
```
`can_donate("O-", "O+")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O-", "O+"), True)`)
}})
```
`can_donate("O+", "O-")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("O+", "O-"), False)`)
}})
```
`can_donate("AB+", "AB-")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(can_donate("AB+", "AB-"), False)`)
}})
```
# --seed--
## --seed-contents--
```py
def can_donate(donor, recipient):
return donor
```
# --solutions--
```py
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
```