코드로 우주평화

Complementary DNA 본문

나는 이렇게 학습한다/Algorithm & SQL

Complementary DNA

daco2020 2022. 3. 31. 21:13
반응형

Description:

Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms.

If you want to know more: http://en.wikipedia.org/wiki/DNA

In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You function receives one side of the DNA (string, except for Haskell); you need to return the other complementary side. DNA strand is never empty or there is no DNA at all (again, except for Haskell).

More similar exercise are found here: http://rosalind.info/problems/list-view/ (source)

Example: (input --> output)

"ATTGC" --> "TAACG"
"GTAT" --> "CATA"
dnaStrand []        `shouldBe` []
dnaStrand [A,T,G,C] `shouldBe` [T,A,C,G]
dnaStrand [G,T,A,T] `shouldBe` [C,A,T,A]
dnaStrand [A,A,A,A] `shouldBe` [T,T,T,T]

 

 

Solution:

1. Make a dictionary of DNA symbols.
2. The DNA symbol matching the key is returned as a string.

def DNA_strand(dna):
    dict = {'A': 'T', 'T': 'A','G': 'C','C': 'G'}
    return ''.join([dict[i] for i in dna])

 

 

Best Practice:

def DNA_strand(dna):
    return dna.translate(str.maketrans("ATCG","TAGC"))

'translate' is a method that translates a string.
If you put str.maketrans('old','new') as an argument, it will be replaced according to the corresponding strings.

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Delete occurrences of an element if it occurs more than n times  (0) 2022.04.02
Jaden Casing Strings  (0) 2022.04.01
Find the odd int  (0) 2022.03.30
Sum of the first nth term of Series  (0) 2022.03.29
Reverse words  (0) 2022.03.28