upposed to count how many times a certain base (represented as a character variable in Python) appears in a dna sequence (represented as a string variable in Python): def count1(dna, base): i = 0 for c in dna: if c == base: i += 1 return i def count2(dna, base): i = 0 for j in range(len(dna)): if dna[j] == base: i += 1 return i
The following functions are all supposed to count how many times a certain base (represented as a character variable in Python) appears in a dna sequence (represented as a string variable in Python):
- def count1(dna, base):
- i = 0
- for c in dna:
- if c == base:
- i += 1
- return i
- def count2(dna, base):
- i = 0
- for j in range(len(dna)):
- if dna[j] == base:
- i += 1
- return i
- def count3(dna, base):
- match = [c == base for c in dna]
- return sum(match)
- def count4(dna, base):
- return dna.count(base)
- def count5(dna, base):
- return len([i for i in range(len(dna)) if dna[i] == base])
- def count6(dna,base):
- return sum(c == base for c in dna)
Which of the correct functions defined in the above exercise is the fastest? Hint. You will need to generate a very large string to test them on, and the function clock() from the time module to time each function.
- count2
- count3
- count5
- count4
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 3 images
Sorry but you did not answer the question as stated:
Which of the correct functions defined in the above exercise is the fastest? Hint. You will need to generate a very large string to test them on, and the function clock() from the time module to time each function.
- count2
- count3
- count5
- count4