ํ’€์ด

ํ•˜๋ผ๋Š” ๋Œ€๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ์ด๊ฑฐ ๋ถ€์บ  ์‹œํ—˜์—์„œ ๋‚˜์˜จ๊ฒƒ ๊ฐ™์€ ๊ธฐ๋ถ„์ด.. ์ผ๋‹จ ๊ทธ๋ฆฌ๊ณ  ํŒŒ์ด์ฌ๋ณด๋‹ค ์Šค์œ„ํ”„ํŠธ๊ฐ€ ๋” ํŽธํ•œ ์ด์ƒ ํ˜„์ƒ์ด ๋ฐœ๊ฒฌ๋๋‹คโ€ฆ

Code

import Foundation
 
 
func getGrade(_ id: Int, _ scores: [Int]) -> String {
    // ์ตœ์†Œ index๋“ค ๊ตฌํ•จ -> ์ผ๋‹จ ๊ทธ ์•ˆ์— id๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ -> ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ์›์†Œ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ์ธ์ง€
    // ์ตœ๋Œ€ index๋“ค ๊ตฌํ•จ
    var scores = scores
    let minScore = scores.min()!
    let maxScore = scores.max()!
    
    let minIndices = scores.enumerated().filter({ $0.element == minScore }).map { $0.offset}
    let maxIndices = scores.enumerated().filter({ $0.element == maxScore }).map { $0.offset}
    
    if minIndices.count == 1 && minIndices.contains(id) { // ์œ ์ผํ•œ ์ตœ์ €์ ์ธ ๊ฒฝ์šฐ
        scores.remove(at: id)
    } else if maxIndices.count == 1 && maxIndices.contains(id) { // ์œ ์ผํ•œ ์ตœ๊ณ ์ ์ธ ๊ฒฝ์šฐ
        scores.remove(at: id)
    }
    
    let meanScore = scores.reduce(0, { $0 + $1 })/scores.count
    
    switch meanScore {
    case 90...:
        return "A"
    case 80..<90:
        return "B"
    case 70..<80:
        return "C"
    case 50..<70:
        return "D"
    case ..<50:
        return "F"
    default:
        return ""
    }
}
 
 
func solution(_ scores:[[Int]]) -> String {
    var studentScore = Dictionary<Int, [Int]>()
    let numberOfStudents = scores.count
    
    for i in 0..<numberOfStudents {
        studentScore[i] = scores.map { $0[i] }
    }
    
    var answer = ""
    for i in 0..<numberOfStudents {
        answer += getGrade(i, studentScore[i]!)
    }
    
    return answer
}
 

Reference