본문 바로가기
알고리즘/Codewars

[Codewars] Complementary DNA (7 kyu) / JavaScript

by fluss 2022. 12. 1.

https://www.codewars.com/kata/554e4a2f232cdd87d9000038

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

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". Your 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"
 

설명:

디옥시리보핵산(DNA)은 세포의 핵에서 발견되고 생물의 발달과 기능에 대한 "지시"를 전달하는 화학물질입니다.

더 알고 싶다면: http://en.wikipedia.org/wiki/DNA

In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". Your 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).

DNA 문자열에서 기호 "A"와 "T"는 "C"와 "G"처럼 서로를 보완합니다. 당신의 함수는 한쪽의 DNA를 받고(문자열, Haskell은 제외) 다른 보완하는 쪽을 반환해야 합니다. DNA 가닥은 비어있지 않거나 DNA가 없습니다(다시, Haskell은 제외).

유사한 연습들은 이곳에서 찾을 수 있다: http://rosalind.info/problems/list-view/ (출처)

 

 

예시: (입력 --> 출력)

"ATTGC" --> "TAACG"
"GTAT" --> "CATA"
 

풀이

문자열을 하나씩 잘라 글자가  'A'라면 'T'로 'T'라면 'A'로 'G'라면 'C'로 나머지는 'G'로 바꾸어주었다.

 

코드

function DNAStrand(dna){
  dna = dna.split('')
  for(let i = 0; i < dna.length; i++){
    if(dna[i] === 'A') dna[i] = 'T';
    else if(dna[i] === 'T') dna[i] = 'A';
    else if(dna[i] === 'G') dna[i] = 'C';
    else dna[i] = 'G';
  }
  return dna.join('');
}

댓글