ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Exercism - RNA Transcription(With.Kotlin)
    문제풀이/Exercism 2019. 7. 16. 22:33
    반응형

    [문제]

    Given a DNA strand, return its RNA complement (per RNA transcription).

    Both DNA and RNA strands are a sequence of nucleotides.

    The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

    The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

    Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

    • G -> C
    • C -> G
    • T -> A
    • A -> U

    [Solution1]

    fun transcribeToRna(dna: String): String {
        val transDna = CharArray(dna.length)
    
        for (i in 0 until dna.length){
            transDna[i] = when (dna[i]){
                "G"[0] -> "C"[0]
                "C"[0] -> "G"[0]
                "T"[0] -> "A"[0]
                "A"[0] -> "U"[0]
                else -> throw IllegalArgumentException("Unknow DNA : ${dna[i]}")
            }
        }
    
        return transDna.joinToString("")
    }

    단순하게 String 을 CharArray 로 변경해서 처리하였다. string 을 배열로 처리하는 부분("G"[0]...)이 보기 싫었다.

     

     

    [Solution2]

    fun transcribeToRna(dna: String): String {
        val transDna = CharArray(dna.length)
    
        for (i in 0 until dna.length){
            transDna[i] = transDnaUnit(dna[i])
        }
    
        return transDna.joinToString("")
    }
    
    fun transDnaUnit(dnaUnit: Char): Char
            = when (dnaUnit){
                'G' -> 'C'
                'C' -> 'G'
                'T' -> 'A'
                'A' -> 'U'
                else -> throw IllegalArgumentException("Unknow DNA : $dnaUnit")
            }

    ' '문자가 char 로 처리되는 것을 알았다.

    transDnaUnit 이라는 fun을 만들어서 처리하였다.

     

    [Solution3]

    fun transcribeToRna(dna: String): String {
        val transDna = CharArray(dna.length)
    
        dna.toCharArray().forEachIndexed {index, dnaUnit ->
            transDna[index] = transDnaUnit(dnaUnit)
        }
    
        return transDna.joinToString("")
    }
    
    fun transDnaUnit(dnaUnit: Char): Char
            = when (dnaUnit){
                'G' -> 'C'
                'C' -> 'G'
                'T' -> 'A'
                'A' -> 'U'
                else -> throw IllegalArgumentException("Unknow DNA : $dnaUnit")
            }

    for 문 말고 다른 Kotlin 의 List API를 사용해보라는 조언으로 forEachIndexed 를 사용하는 방법으로 코드를 수정

     

     

    [Solution4]

    fun transcribeToRna(dna: String): String {
    
        val dnaArray = dna.toCharArray()
    
        dnaArray.forEachIndexed { index, c ->
            dnaArray[index] = when (c){
                'G' -> 'C'
                'C' -> 'G'
                'T' -> 'A'
                'A' -> 'U'
                else -> throw IllegalArgumentException("Unknow DNA : $c")
            }
        }
    
        return dnaArray.joinToString("")
    }

    Solution3 에서 더 줄일 수 있는 형태로 수정

     

    [Solution5]

    fun transcribeToRna(dna: String): String
            = dna.toCharArray()
            .map {
                when (it) {
                    'G' -> 'C'
                    'C' -> 'G'
                    'T' -> 'A'
                    'A' -> 'U'
                    else -> throw IllegalArgumentException("Unknow DNA : $it")
                }
            }.joinToString("")

    forEach를 이용하지 않고 map 을 이용하여 처리하도록 수정 

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map.html

     

    map - Kotlin Programming Language

     

    kotlinlang.org

     

    반응형

    '문제풀이 > 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 - Hamming(With.Kotlin)  (0) 2019.07.21
    Exercism - Twofer (with.Kotlin)  (0) 2019.07.16
    exercism submit 방법  (0) 2019.07.10
    exercism 설치 방법  (0) 2019.07.09

    댓글

Designed by Tistory.