SW Test/Programmers

[Programmers] Lv2. ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 7. 31. 16:23
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42888?language=kotlin

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/42888?language=kotlin

 

 

 

ํ’€์ด

  • enterlist์—๋Š” ์œ ์ € ID์™€ ์ถœ์ž…์„ ๋œปํ•˜๋Š” Boolean ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 0์€ ๋‚˜๊ฐ€๋Š” ๊ฒƒ์„ ๋œปํ•˜๊ณ  1์€ ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์œ ์ € ID์™€ ๋‹‰๋„ค์ž„ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” nicknamemap์„ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹‰๋„ค์ž„์€ ๊ณ„์† ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ณ„๋„์˜ ์ž๋ฃŒ๊ตฌ์กฐ์— ์ €์žฅํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • ์ดํ›„ recod์˜ ์ •๋ณด๋ฅผ ํ•˜๋‚˜์”ฉ ํŒŒ์‹ฑํ•˜์—ฌ enterlist์™€ nicknamemap์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ enterlist์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
class Solution {
    fun solution(record: Array<String>): Array<String> {
        val enterlist = mutableListOf<Pair<String, Boolean>>()
        val nicknamemap = mutableMapOf<String,String>()
        
        record.forEach{
            val list = it.split(" ")
            
            when(list[0]){
                "Enter" -> {
                    enterlist.add(list[1] to true)
                    nicknamemap.put(list[1], list[2])
                }
                "Leave" -> {
                    enterlist.add(list[1] to false)
                }
                "Change" -> {
                    nicknamemap.put(list[1], list[2])
                }
            }
        }
        
        return enterlist.map{
            "${nicknamemap.get(it.first)}๋‹˜์ด " + if(it.second) "๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค." else "๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค."
        }.toTypedArray()
    }
}

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
๋ฐ˜์‘ํ˜•