-
Notifications
You must be signed in to change notification settings - Fork 0
/
bite136.py
94 lines (81 loc) · 3.01 KB
/
bite136.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Write a function which checks the red blood cell compatibility between donor and recipient.
https://en.wikipedia.org/wiki/Blood_type#Red_blood_cell_compatibility
For simplicity, the appearance of 8 basic types of blood is considered.
The input of blood type can be in the form of:
pre defined Bloodtype enum e.g.: Bloodtype.ZERO_NEG
value of the pre-defined Bloodtype 1..7
pre defined text e.g. "0-", "B+", "AB+", ...
If input value is not a required type TypeError is raised.
If input value is not in defined interval ValueError is raised.
Keywords: enum, exception handling, multi type input
"""
from enum import Enum
class Bloodtype(Enum):
ZERO_NEG = 0
ZERO_POS = 1
B_NEG = 2
B_POS = 3
A_NEG = 4
A_POS = 5
AB_NEG = 6
AB_POS = 7
blood_type_text = {
"0-": Bloodtype.ZERO_NEG,
"0+": Bloodtype.ZERO_POS,
"B-": Bloodtype.B_NEG,
"B+": Bloodtype.B_POS,
"A-": Bloodtype.A_NEG,
"A+": Bloodtype.A_POS,
"AB-": Bloodtype.AB_NEG,
"AB+": Bloodtype.AB_POS,
}
# complete :
def check_bt(donor, recipient):
""" Checks red blood cell compatibility based on 8 blood types
Args:
donor (int | str | Bloodtype): red blood cell type of the donor
recipient (int | str | Bloodtype): red blood cell type of the recipient
Returns:
bool: True for compatability, False otherwise.
"""
try:
if type(donor) == str:
donor = blood_type_text[donor].value
if type(recipient) == str:
recipient = blood_type_text[recipient].value
if type(donor) == Bloodtype:
donor = donor.value
if type(recipient) == Bloodtype:
recipient = recipient.value
except:
raise ValueError
if type(donor) != int or type(recipient) != int:
raise TypeError
if donor not in range(0, 8) or recipient not in range(0, 8):
raise ValueError
tuple_check = _particular_antigen_comp(donor, recipient)
if -1 in tuple_check:
return False
else:
return True
# hint
def _particular_antigen_comp(donor: int, recipient: int) -> tuple:
"""Returns a particalar antigen compatibility, where each tuple member
marks a compatibility for a particular antigen (A, B, Rh-D).
If tuple member is non-negative there is a compatibility.
For red blood cell compatibility is required that
all tuple members are non-negative (i.e. compatibility for all 3 antigens).
0- bloodtype is represented as 0 ; AB+ is represented as 7; see Bloodtype enum
Examples:
_particular_antigen_comp(0, 7) -> (1, 1, 1) 0- can donate to AB+
_particular_antigen_comp(1, 3) -> (0, 1, 0) 0+ can donate to B+
_particular_antigen_comp(2, 5) -> (1, -1, 1) B+ cannot donate to A+
_particular_antigen_comp(7, 0) -> (-1, -1, -1) AB+ cannot donate to 0-
"""
return (
((recipient // 4) % 2) - ((donor // 4) % 2),
((recipient // 2) % 2) - ((donor // 2) % 2),
(recipient % 2) - (donor % 2),
)
print(check_bt(3, ["AB", "Rh+"]))