-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_exercise.py
More file actions
executable file
·42 lines (32 loc) · 1.4 KB
/
Copy pathfunctions_exercise.py
File metadata and controls
executable file
·42 lines (32 loc) · 1.4 KB
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
# function that accepts a single amino acid
def my_function(protein, amino_acid):
protein_length = len(protein)
amino_acid_count = protein.upper().count(amino_acid.upper())
percentage = amino_acid_count / protein_length * 100
return percentage
# test suite
assert my_function("MSRSLLLRFLLFLLLLPPLP", "M") == 5
assert my_function("MSRSLLLRFLLFLLLLPPLP", "r") == 10
assert my_function("msrslllrfllfllllpplp", "L") == 50
assert my_function("MSRSLLLRFLLFLLLLPPLP", "Y") == 0
# function that accepts a list of amino acids
# set default set of amino acids (hydrophobic residues)
def my_function(protein, amino_acids=["A", "I", "L", "M", "F", "W", "Y", "V"]):
# get the length
protein_length = len(protein)
# count for each amino acid how often it occurs
# and sum those counts
sum = 0
for amino_acid in amino_acids:
upper_case_amino_acid = amino_acid.upper()
amino_acid_count = protein.upper().count(upper_case_amino_acid)
sum = sum + amino_acid_count
# calculate percentage and round to 1 digit
percentage = sum / protein_length * 100
rounded_percentage = round(percentage, 1)
return rounded_percentage
# test suite 2
assert my_function("MSRSLLLRFLLFLLLLPPLP", ["M"]) == 5
assert my_function("MSRSLLLRFLLFLLLLPPLP", ['M', 'L']) == 55
assert my_function("MSRSLLLRFLLFLLLLPPLP", ['F', 'S', 'L']) == 70
assert my_function("MSRSLLLRFLLFLLLLPPLP") == 65