프로그래머스 (Swift)/lv 1
[lv1] 로또의 최고 순위와 최저 순위
은더기
2024. 9. 2. 11:15
문제
내 정답 코드
import Foundation
func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
// lottos의 0의 count를 파악
// lottos에서 win_nums와 동일한 숫자의 count를 파악
// 최고 순위 -> 동일 count와 0의 count를 더한 count
// 최저 순위 -> 동일 count
let ranking: [Lotto] = [Lotto(rank: 1, correctCount: 6), Lotto(rank: 2, correctCount: 5), Lotto(rank: 3, correctCount: 4), Lotto(rank: 4, correctCount: 3), Lotto(rank: 5, correctCount: 2), Lotto(rank: 6, correctCount: 1), Lotto(rank: 6, correctCount: 0)]
let zeroCount = lottos.filter { $0 == 0 }.count
var winCount = win_nums.filter { lottos.contains($0) }.count
return [(ranking.filter { $0.correctCount == zeroCount+winCount }.first?.rank)!, (ranking.filter { $0.correctCount == winCount }.first?.rank)!]
}
struct Lotto {
let rank: Int
let correctCount: Int
}
#
1. Lotto 구조체를 만들고, 맞춘 개수에 따른 순위로 인스턴스를 넣어준다.
2. lottos의 0 개수를 저장한다.
3. lottos와 win_nums에서 동일한 수의 개수를 저장한다.
4. 2번 3번을 더한 개수의 순위가 최고순위이고, 3번 개수의 순위가 최저 순위이다.
배운 기술