Roel Notebook

[Swift]프로그래머스 모의고사

by Roel Downey
728x90
반응형

모의고사

 

문제 

- 문제 링크: 프로그래머스

 

 

풀이

import Foundation

func solution(_ answers:[Int]) -> [Int] {
    let answer = (
        one: [1, 2, 3, 4, 5], // index % 5
        two: [2, 1, 2, 3, 2, 4, 2, 5], // index % 8
        three: [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] // index % 10
    )
    var result = [1:0, 2:0, 3:0]

    for (index, value) in answers.enumerated() {
        if value == answer.one[index % 5] {
            result[1] = result[1]! + 1
        }
        
        if value == answer.two[index % 8] {
            result[2] = result[2]! + 1
        }
        
        if value == answer.three[index % 10] {
            result[3] = result[3]! + 1
        }
    }
    
    return result.sorted{ $0.key < $1.key }.filter{ $0.value == result.values.max() }.map{ $0.key }
}
728x90
반응형

블로그의 정보

What doing?

Roel Downey

활동하기