Rosalind: Counting DNA Nucleotides

Python

Solution 1

1
2
3
4
5
def count_nucleotides(dna):
    return (dna.count("A"), 
			dna.count("C"), 
			dna.count("G"), 
			dna.count("T")) 

Solution 2

1
2
3
4
5
from collections import Counter

def count_nucleotides(dna):
    counts = Counter(dna)
    return (counts["A"], counts["C"], counts["G"], counts["T"])

Solution 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def count_nucleotides(dna):
    a, c, g, t = 0, 0, 0, 0
    for nt in dna:
        if nt == "A":
            a += 1
        elif nt == "C":
            c += 1
        elif nt == "G":
            g += 1
        elif nt == "T":
            t += 1
    return (a, c, g, t)

Solution 4

1
2
3
4
5
6
7
def count_nucleotides(dna):
    index = 0
    counts = {}
    while index < len(dna):
        counts[dna[index]] = counts.get(dna, 0) + 1
        index += 1
    return (counts["A"], counts["C"], counts["G"], counts["T"])
0%