본문 바로가기
Python

Rosalind - Counting DNA Nucleotides

by 코딩하는 미토콘드리아 bioinformatics 2023. 9. 9.
반응형

33 위 정도 였던거 같은데 한 동안 못했더니 39 위로 내려가 있네요.

복습도 할 겸 문제 풀때 사용했던 코드를 하나씩 공유하고자 합니다.

궁금한 점은 댓글로 남겨 주시면 감사하겠습니다.

 

 

Rosalind - Counting DNA Nucleotides

문제 풀이

def count_bases 함수 선언

replace() 로 문자열에 줄내림 치환

for문을 통해서 freq 변수에 A, C, G, T 가 있으면 +1 씩 세어서 저장

 

코드 1:

def count_bases(filename):
    file = open(filename, 'r')
    string = file.read().replace('\n', '')
    freq = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
    for i in string:
        freq[i] += 1
    file.close()
    print(freq['A'], freq['C'], freq['G'], freq['T'])
count_bases("rosalind_dna.txt")
# 207 205 192 200

코드 2:

def countBases(seq):
    A = 0
    C = 0
    T = 0
    G = 0
    for i in range(0,len(seq)):
        if seq[i] == "A":
            A+=1
        if seq[i] == "C":
            C+=1
        if seq[i] == "T":
            T+=1
        if seq[i] == "G":
            G+=1
    return A, C, T, G
count_bases("rosalind_dna.txt")
#result
207 205 192 200
반응형