본문 바로가기
알고리즘/코드시그널

Codesignal - evenDigingsOnly

by 패쓰킴 2021. 5. 3.
728x90

Check if all digits of the given integer are even.

 

Example

  • For n = 248622, the output should be
    evenDigitsOnly(n) = true;
  • For n = 642386, the output should be
    evenDigitsOnly(n) = false.

Input/Output

  • [execution time limit] 20 seconds (swift)
  • [input] integer n
  • Guaranteed constraints:
    1 ≤ n ≤ 109.
  • [output] boolean
    • true if all digits of n are even, false otherwise.

풀이

주어지는 숫자의 각 자리수에 해당하는 숫자가 짝수일때만 리턴 true

func evenDigitsOnly(n: Int) -> Bool {
    let strN = String(n)

    for i in strN.indices {
        guard let num = Int(String(strN[i])) else { return false }
        if num % 2 != 0 {
            return false
        }
    }
    return true
}
728x90

'알고리즘 > 코드시그널' 카테고리의 다른 글

Codesignal - find Email Domain  (0) 2021.05.06
Codesignal - extractEachKth  (0) 2021.05.03
Codesignal - digitDegree  (0) 2021.05.02
Codesignal - differentSymbolsNaive  (0) 2021.05.02
Codesignal - depositProfit  (0) 2021.05.02

댓글