[Kotlin] Korean Coding Test - Programmers - Check personality types

Kotlin

Language :

Question

Link

I want to make my own Kakao personality type test paper. The Personality Type Test categorizes personality types into four indicators. Personality is determined by one of two types in each metric.

surface numberPersonality type
Indicators 1Ryan type (R), Tube type (T)
Indicators 2cone type (C), Frodo type (F)
Indicators 3Jay-Z type (J), Muji type (M)
Indicators 4Apeach type (A), Neo type (N)

There are four indicators, so there can be a total of 16 personality types (=2 x 2 x 2 x 2). For example, there are personality types such as "RFMN" and "TCMA".

There are a total of n questions on the test sheet, and each question has 7 options as below.

  • very disagreeable
  • disagreeable
  • slightly disagreeable
  • I don't know
  • slightly agree
  • agree
  • very agreeable

Each question determines personality type scores by one indicator.

For example, you can score a question as shown in the table below with index 4.

OptionsPersonality Type Score
very disagreeableNeo type 3 point
disagreeableNeo type 2 point
slightly disagreeableNeo type 1 point
I don't knowNo personality type gets points
slightly agreeApeach type 1 point
agreeApeach type 2 point
very agreeableApeach type 3 point

At this point, if the examiner chooses the slightly agree option in the question, they will receive 1 Apeach type (A) personality type. If the examiner chooses the highly disagreeable option, he or she will receive a Neo type (N) personality type of 3.

As in the example above, it is not given only if the neo type is disagreeable and the apich type is agreeable, but depending on the question, the neo type is agreeable and the apich type is disagreeable.

But each option has a fixed size score.

  • If you choose the Very Agree or Very Disagree option, you get 3 points.
  • If you choose Agree or Disagree, you get 2 points.
  • If you choose a slightly agreeable or slightly disagreeable option, you get 1 point.
  • If you select the Don't Know option, you won't get points.

The results add personality type scores for all questions to determine that the personality type that scored higher for each metric is the personality type of the examiner. However, if each personality type score is the same on one indicator, the quick personality type of the two personality types is judged to be the personality type of the examiner.

A one-dimensional string array survey containing the indicators for judging each question and a one-dimensional integer array choices containing the options selected by the examiner for each question are given as parameters. At this time, complete the solution function so that the examiner's personality type test results are returned in order of the index number.

Restrictions

Length of 1 ≤ survey (=n) ≤ 1,000

  • The elements of the survey are "RT", "TR", "FC", "CF", "MJ", "JM", "AN", and "NA".
  • The first character in survey[i] refers to the type of personality that you receive when you select the disagreeable related option in question i+1.
  • The second character in survey[i] represents the type of personality you receive when you select the consent-related option in question i+1.

Length of choices = length of survey

  • Choices[i] means the choice of the i+1th question chosen by the examiner.
  • 1 ≤ element of choices ≤ 7
choices1234567
Meaningvery disagreeabledisagreeableslightly disagreeableI don't knowslightly agreeagreevery agreeable

Input/Output example

Input/Output example #1

  • Input
  • survey: ["AN", "CF", "MJ", "RT", "NA"]
  • choices: [5, 3, 2, 7, 5]
  • Output: "TCMA"

Input/Output example #2

  • Input
  • survey: ["TR", "RT", "TR"]
  • choices: [7, 1, 3]
  • Output:  "RCJA"

Code

kotlin

class Solution {
    
    // Score by choice
    private val choiceScore = intArrayOf(3, 2, 1, 0, 1, 2, 3)
    // Type Score
    private val typeScore = mutableMapOf(
        'R' to 0, 'T' to 0,
        'C' to 0, 'F' to 0,
        'J' to 0, 'M' to 0,
        'A' to 0, 'N' to 0
    )
    
    fun solution(survey: Array<String>, choices: IntArray): String {
        for (index in survey.indices) {
            val choice = choices[index]
            // Skip the option 'I don't know'
            if (choice == 4) continue
            // Score based on your choice
            val types = survey[index]
            val type = if (choice < 4) types[0] else types[1]
            typeScore[type] = choiceScore[choice - 1] + typeScore.getOrDefault(type, 0)
        }

        // If you have the same score, choose the fastest personality type in dictionary order between the two personality types
        var answer = if (typeScore.getOrDefault('R', 0) >= typeScore.getOrDefault('T', 0)) "R" else "T"
        answer += if (typeScore.getOrDefault('C', 0) >= typeScore.getOrDefault('F', 0)) "C" else "F"
        answer += if (typeScore.getOrDefault('J', 0) >= typeScore.getOrDefault('M', 0)) "J" else "M"
        answer += if (typeScore.getOrDefault('A', 0) >= typeScore.getOrDefault('N', 0)) "A" else "N"
        return answer
    }
    
}

text

TEST 1 〉	Pass (2.35ms, 62.2MB)
TEST 2 〉	Pass (2.37ms, 62.5MB)
TEST 3 〉	Pass (2.19ms, 60.9MB)
TEST 4 〉	Pass (2.50ms, 62.9MB)
TEST 5 〉	Pass (2.44ms, 60.2MB)
TEST 6 〉	Pass (2.35ms, 61MB)
TEST 7 〉	Pass (2.36ms, 60.3MB)
TEST 8 〉	Pass (2.35ms, 61.9MB)
TEST 9 〉	Pass (2.54ms, 60.3MB)
TEST 10 〉	Pass (2.73ms, 61MB)
TEST 11 〉	Pass (2.44ms, 62.8MB)
TEST 12 〉	Pass (4.51ms, 59.7MB)
TEST 13 〉	Pass (3.45ms, 61.5MB)
TEST 14 〉	Pass (4.40ms, 61.6MB)
TEST 15 〉	Pass (2.90ms, 60.4MB)
TEST 16 〉	Pass (3.05ms, 61.3MB)
TEST 17 〉	Pass (2.76ms, 60.9MB)
TEST 18 〉	Pass (3.13ms, 61.4MB)
TEST 19 〉	Pass (3.05ms, 60.9MB)
TEST 20 〉	Pass (2.99ms, 61MB)

민갤

Back-End Developer

백엔드 개발자입니다.