[Swift]프로그래머스 모의고사
by Roel Downey728x90
반응형
모의고사
문제
풀이
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
반응형
'알고리즘_자료구조 > 문제풀이' 카테고리의 다른 글
[Swift]프로그래머스 2016 (0) | 2019.09.03 |
---|---|
[Swift]프로그래머스 체육복 (0) | 2019.09.01 |
[Swift]프로그래머스 가운데 글자 가져오기 (0) | 2019.08.30 |
[Swift] 프로그래머스 K번째수 (0) | 2019.08.29 |
[Swift] 프로그래머스 두 정수 사이의 합 (0) | 2019.08.29 |
블로그의 정보
What doing?
Roel Downey