본문 바로가기
python-algorithm

백준 1969 DNA

by 무적김두칠 2022. 11. 10.

https://www.acmicpc.net/problem/1969

 

1969번: DNA

DNA란 어떤 유전물질을 구성하는 분자이다. 이 DNA는 서로 다른 4가지의 뉴클레오티드로 이루어져 있다(Adenine, Thymine, Guanine, Cytosine). 우리는 어떤 DNA의 물질을 표현할 때, 이 DNA를 이루는 뉴클레오

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from collections import Counter
def cal_hamming(dna,target):
    hamming = 0
    for compared_dna in dna:
        for i in range(len(target)):
            if compared_dna[i]!= target[i]:
                hamming+=1
    return hamming
 
if __name__ == '__main__':
    n,m = map(int, input().split())
    dna=[input() for i in range(n)]
    answer =''
    for i in range(m):
        tmp=[]
        for j in range(n):
            tmp.append(dna[j][i])
        sorted_counter =sorted(Counter(tmp).items(), key=lambda x: (-x[1],x[0] ))
        answer+=sorted_counter[0][0]
    print(answer)
    print(cal_hamming(dna,answer))
cs

해밍거리를 구하는 함수 : cal_hamming(dna,target)
Calculating hamming distance
같은 글자(==DNA)가 가장 많은 글자 구하고 알파벳 순으로 정렬 :
sorted_counter =sorted(Counter(tmp).items(), key=lambda x: (-x[1],x[0] ))

Find max frequent word , and sort asc

 

반응형

댓글