ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Exercism - Hamming(With.Kotlin)
    문제풀이/Exercism 2019. 7. 21. 10:07
    반응형

    [문제]

    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 :)

     

    [Solution1]

    class Hamming {
    
        companion object {
            fun compute (origin: String , targets: String) : Int{
                var hammingDistance = 0
    
                if(origin.length > targets.length){
                    throw IllegalArgumentException("left and right strands must be of equal length.")
                }
    
                if(origin.length < targets.length){
                    throw IllegalArgumentException("left and right strands must be of equal length.")
                }
    
                for (i in 0 until origin.length){
                    if(origin[i] != targets[i]){
                        hammingDistance++
                    }
                }
    
                return hammingDistance
            }
        }
    
    }

    for 문과 if 문으로 단순하게 작성한 코드

     

    [Solution2]

    class Hamming {
    
        companion object {
            fun compute (origin: String , targets: String) : Int{
    
                require(origin.length == targets.length) { "left and right strands must be of equal length." }
    
                return origin.zip(targets).count { it.first != it.second }
            }
        }
    
    }

    Kotlin의 require와 funtion operators를 사용하라고 하여 zip 과 count 를 사용하여 수정함

    반응형

    '문제풀이 > Exercism' 카테고리의 다른 글

    Exercism - Scrabble Score (With.Kotlin)  (0) 2019.07.30
    Exercism - Space Age (With.Kotlin)  (0) 2019.07.26
    Exercism - Gigasecond(With.Kotlin)  (0) 2019.07.23
    Exercism - Leap(With.Kotlin)  (0) 2019.07.22
    Exercism - RNA Transcription(With.Kotlin)  (0) 2019.07.16
    Exercism - Twofer (with.Kotlin)  (0) 2019.07.16
    exercism submit 방법  (0) 2019.07.10
    exercism 설치 방법  (0) 2019.07.09

    댓글

Designed by Tistory.