Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 성격 유형 검사하기 #37

Closed
hwangJi-dev opened this issue Dec 20, 2022 · 0 comments
Closed

[Algorithm] 성격 유형 검사하기 #37

hwangJi-dev opened this issue Dec 20, 2022 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Dec 20, 2022

💬 문제

[코딩테스트 연습 - 성격 유형 검사하기](https://school.programmers.co.kr/learn/courses/30/lessons/118666)

💬 Idea

  • 먼저 survey를 돌면서 점수를 얻게되는 성격 유형을 구한다 (type)
  • 이후 성격 배열을 돌며 성격 점수판에 점수를 채운다.
    • ex) RT 중 앞 성격유형에 점수를 주는 경우는 마이너스, 뒤 성격유형에 점수를 주는 경우는 플러스로 점수를 채운다.
  • 성격 점수 배열을 돌며 최종적으로 마이너스가 찍힌 점수판인 경우 뒤 성격유형을 더하고, 플러스가 찍힌 점수판일 경우 앞 성격유형을 더해서 최종 성격 유형을 도출한다.

💬 풀이

func solution(_ survey:[String], _ choices:[Int]) -> String {
    let personality: [String] = ["RT", "CF", "JM", "AN"]
    var personalityScore: [Int] = [0, 0, 0, 0]
    var result: String = ""
    
    for (index, i) in survey.enumerated() {
        let type = String(choices[index] < 4 ? i.first! : i.last!)
        for (idx, j) in personality.enumerated() {
            if j.contains(type) {
                personalityScore[idx] += type == String(j.first!) ? -abs(choices[index] - 4) : abs(choices[index] - 4)
            }
        }
    }
    
    for (index, score) in personalityScore.enumerated() {
        result += score <= 0 ? String(personality[index].first!) : String(personality[index].last!)
    }
    
    return result
}

💬 더 나은 방법?

  • 이중, 다중 for문을 사용하지 않는 더 효율적인 방법!
func solution(_ survey:[String], _ choices:[Int]) -> String {
    var personalityDict: [String: Int] = ["R": 0, "T": 0, "C": 0, "F": 0, "J": 0, "M": 0, "A": 0, "N": 0]
    var result: String = ""
    
    for (index, i) in survey.enumerated() {
        personalityDict[choices[index] < 4 ? String(i.first!) : String(i.last!)]! += abs(choices[index] - 4)
    }
    
    result += personalityDict["R"]! >= personalityDict["T"]! ? "R" : "T"
    result += personalityDict["C"]! >= personalityDict["F"]! ? "C" : "F"
    result += personalityDict["J"]! >= personalityDict["M"]! ? "J" : "M"
    result += personalityDict["A"]! >= personalityDict["N"]! ? "A" : "N"
    
    return result
}
@hwangJi-dev hwangJi-dev self-assigned this Dec 20, 2022
@hwangJi-dev hwangJi-dev changed the title [Algorithm] 성격 유형 검사 (re) [Algorithm] 성격 유형 검사하기 Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant