코테연습

11.Complementary DNA

hxunz 2022. 3. 20. 23:15

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]

 

function DNAStrand(dna) {
  const words = [...dna];
  
  const result = words.map((word) => {
    if (word === 'A') {
      return 'T'
    }
    else if (word === 'T') {
      return 'A'
    }
    else if (word === 'C') {
      return 'G'
    }
    else 
      return 'C'
  })
  return result.join('');
}

 

전개 연산자를 통해서 주어진 string을 배열로 나타냈다.

반복문 사용이 익숙하지 않아서 어제 사용했었던 map함수를 다시 이용해봤다. 

조건식이 넘 많이 들어간것같아서 더 간단하게 할 수 있을것같은데 방법을 다시 한번 생각해봐야겠다.