Juni_Dev_log

(200204_TIL) 'Hamming' 본문

CodingTest/Exercism

(200204_TIL) 'Hamming'

Juni_K 2021. 2. 4. 23:03

▶ Hamming 🧬

Hamming

📌 (Problem)

Introduction

Calculate the Hamming Distance between two DNA strands.

Your body is made up of cells that contain DNA.

Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells.

In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

 

When cells divide, their DNA replicates too.

Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information.

If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".

We read DNA using the letters C,A,G and T. Two strands might look like this:

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^^^^     ^ ^         ^

They have 7 differences, and therefore the Hamming Distance is 7.

The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

Implementation notes

The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work. The general handling of this situation (e.g., raising an exception vs returning a special value) may differ between languages.

 


 

- Hamming disatnce 란, 부모의 세포가 자식의 세포로 복제될 때, 복제되는 과정 속에서 실수와 잘못된 정보의 인코딩을 하게 되면서, 복제한 DNA 와 다른 유전자 DNA 가 나올 수도 있다. 이 두 DNA의 차이를 Hamming distance 라고 부른다.

- DNA는 C, A, G, T 로 이루어져있다.

- 위 예시에서는 7개의 차이가 있기 때문에, Hamming Distance 는 7이다.

- Hamming distance 를 계산하는 코드를 만들어보자.

- 두 DNA의 길이는 동일하며, 다른 길이일 때는 계산을 하지 않는다.

 

(Tip)

- 점수로 사용할 변수를 선언 

ex) var Hamming distance;

 

- 함수의 매개변수로 두 가지 DNA 가 들어간다.  두 DNA 의 철자를 비교해서 틀린 부분이 있을 때, Hamming distance를 1 추가하면된다.

-> if ( a  !==  b ) { Hamming distance ++  return Hamming distance}  

 

- 두 DNA의 길이가 다를 때 , error를 보여준다.

=>

(두 개가 다를 때) new Error('left and right strands must be of equal length')

(왼쪽이 없을 때) new Error('left strand must not be empty')

(오른쪽이 없을 때) new Error('right strand must not be empty')

💯 (Solution)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export const compute = (DNA1,DNA2) => {
 
    // 다른 부분 측정할 변수 선언 (초기값은 0으로 지정)
    let Hamming_distance=0;
 
    // 둘 다 빈 값일 때, 둘이 완전히 똑같을 때는 0을 반환
    if(DNA1.length === 0 && DNA2.length === 0){
        return Hamming_distance;
    }
 
    // 첫번째 매개변수가 빈 값일 때
    if(DNA1.length === 0){
        throw new Error('left strand must not be empty');
    }
    // 두번째 매개변수가 빈 값일 때
    if(DNA2.length === 0){
        throw new Error('right strand must not be empty');
    }
 
    // 두 매개변수의 길이가 다를 때
    if(DNA1.length !== DNA2.length){
        throw new Error('left and right strands must be of equal length');
    }
 
    // 길이가 똑같을 때, 다른 인덱스값을 찾게되면 + 하는 방식
    for(var i = 0; i < DNA1.length; i++){
        if(DNA1[i] !== DNA2[i]){
            Hamming_distance++;
        }
    }
    return Hamming_distance;
};
cs

- DNA의 차이를 점수로 측정하는 변수를 선언한다. (Hamming_distance)

- 둘 다 빈값인 경우, 0을 그대로 반환한다.

- 함수의 첫 번째 매개변수 값이 빈 값일 때, Error를 보낸다.

- 함수의 두 번째 매개변수 값이 빈 값일 때, Error를 보낸다.

- 함수의 두 매개변수가 값의 길이가 다를 때, Error를 보낸다.

- 위 조건들에 다 해당되지 않는 경우(길이는 같고 값은 존재한 경우), for문 을 통해서 Hamming_distance 에 1씩 더한다.

- return 으로 Hamming_distance 를 반환한다.

'CodingTest > Exercism' 카테고리의 다른 글

(200207_TIL) 'Word Count'  (0) 2021.02.07
(200205_TIL) 'Raindrops'  (0) 2021.02.05
(200203_TIL) 'ETL'  (0) 2021.02.03
(200202_TIL) 'Triangle'  (0) 2021.02.02
(200201_TIL) 'Collatz Conjecture'  (0) 2021.02.01
Comments