3,609 views
33 votes
33 votes
Complete the function, count_bases(s). It takes as input a DNA sequence as a string, s. It should compute the number of occurrences of each base (i.e., 'A', 'C', 'G', and 'T') in s. It should then return these counts in a dictionary whose keys are the bases.

User Gjijo
by
2.6k points

1 Answer

27 votes
27 votes

Answer:

The function is as follows:

def count_bases(DNA_str):

retDNA = {}

for i in DNA_str:

if i in retDNA:

retDNA[i]+=1

else:

retDNA[i] = 1

return retDNA

Explanation:

This defines the function

def count_bases(DNA_str):

This initializes a dictionary

retDNA = {}

This iterates through the dictionary

for i in DNA_str:

If an item exists in the dictionary

if i in retDNA:

The count is updated by 1

retDNA[i]+=1

Otherwise

else:

The count is set to 1

retDNA[i] = 1

This returns the DAN

return retDNA

User Prmths
by
3.1k points