-
Exercism - Twofer (with.Kotlin)문제풀이/Exercism 2019. 7. 16. 00:41반응형
[문제]
Two-fer or 2-fer is short for two for one. One for you and one for me.
Given a name, return a string with the message:
One for X, one for me.
Where X is the given name.
However, if the name is missing, return the string:
One for you, one for me.
Here are some examples:
NameString to return
Alice One for Alice, one for me. Bob One for Bob, one for me. One for you, one for me. Zaphod One for Zaphod, one for me.
[Solution 1]
object Twofer { fun twofer(name:String? = null) : String{ if(name == null){ return "One for you, one for me." } return "One for $name, one for me." } }
맨처음 풀이는 그냥 간단하게 null 을 이용해서 구분을 하였습니다.
[Solution 2]
object Twofer { fun twofer(name:String = "you") : String{ return "One for $name, one for me." } }
그 다음은 "you"를 기본값으로 변경하여 리턴하도록 변경하였습니다.
[Solution 3]
object Twofer { fun twofer(name:String = "you") = "One for $name, one for me." }
마지막은 fun을 줄여서 단순화 하였습니다. 아래 링크 참고
https://kotlinlang.org/docs/reference/functions.html#single-expression-functions
반응형'문제풀이 > 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 - RNA Transcription(With.Kotlin) (0) 2019.07.16 exercism submit 방법 (0) 2019.07.10 exercism 설치 방법 (0) 2019.07.09