2018 KAKAO BLIND RECRUITMENT CODING TEST

import Foundation

var uidNicknameDictionary: [String : String] = [:]

//var resultDictionary: [String: String] = [:]

func changeNickname(_ result:inout [String], uid: String, nicknameAs: String) {
    for i in 0..<result.count {
        if result[i].contains("\(uidNicknameDictionary[uid]!)") {
            result[i] = result[i].replacingOccurrences(of: "\(uidNicknameDictionary[uid]!)", with: "\(nicknameAs)")
            uidNicknameDictionary[uid] = nicknameAs
        }
    }
}

func solution(_ record:[String]) -> [String] {
    
    var result: [String] = []
    
    record.forEach { (line) in
        var userInfo: [String] = line.components(separatedBy: " ")
        
        switch userInfo[0] {
        case "Enter" :
            for (key, value) in uidNicknameDictionary {
                if key == userInfo[1] {
                    
                }
            }
            
            uidNicknameDictionary.updateValue(userInfo[2], forKey: userInfo[1])
            result.append("\(uidNicknameDictionary[userInfo[1]]!)님이 들어왔습니다.")
            break
        case "Leave" :
            result.append("\(uidNicknameDictionary[userInfo[1]]!)님이 나갔습니다.")
            break
        case "Change" :
            changeNickname(&result, uid: userInfo[1], nicknameAs: userInfo[2])
            break
            
        default:
            break
        }
    }
    return result
}
import Foundation

var uidNicknameDictionary: [String : String] = [:]
var resultArray: [[String]] = []

func changeNickname(_ resultArray:inout [[String]], uid: String, nicknameAs: String) {
    for i in 0..<resultArray.count {
        if resultArray[i][0] == uid {
            if resultArray[i][1].contains("\(uidNicknameDictionary[uid]!)") {
                resultArray[i][1] = resultArray[i][1].replacingOccurrences(of: "\(uidNicknameDictionary[uid]!)", with: "\(nicknameAs)")
            }
        }
    }
}

func solution(_ record:[String]) -> [String] {
    var result: [String] = []
    
    record.forEach { (line) in
        var userInfo: [String] = line.components(separatedBy: " ")
        
        switch userInfo[0] {
        case "Enter" :
            let uid = userInfo[1]
            let nickname = userInfo[2]
            
            resultArray.forEach({ (results) in
                if results[0] == uid {
                    changeNickname(&resultArray, uid: uid, nicknameAs: nickname)
                }
            })
            uidNicknameDictionary.updateValue(nickname, forKey: uid)
            resultArray.append([uid, "\(uidNicknameDictionary[uid]!)님이 들어왔습니다."])
            break
        case "Leave" :
            let uid = userInfo[1]
            resultArray.append([uid, "\(uidNicknameDictionary[uid]!)님이 나갔습니다."])
            break
        case "Change" :
            changeNickname(&resultArray, uid: userInfo[1], nicknameAs: userInfo[2])
            break
            
        default:
            break
        }
    }
    
    resultArray.forEach { (results) in
        result.append(results[1])
    }
    return result
}

시간 초과, 정확도 12.5

check if a string contains

let string = "hello Swift"
if string.contains("Swift") {
    print("exists")
}

Using tuple

import Foundation

var nicknameOf: [String : String] = [:]
var ioMessages: [(message: String, uid: String)] = []

func solution(_ record:[String]) -> [String] {
    var result: [String] = []
    
    record.forEach { (line) in
        var userInfo: [String] = line.components(separatedBy: " ")
        
        let msg = userInfo[0]
        let uid = userInfo[1]
        
        switch msg {
        case "Enter" :
            // check it is already exist??
            
            ioMessages.append((msg, uid))
            nicknameOf[uid] = userInfo[2]
            break
        case "Leave" :
            ioMessages.append((msg, uid))
            break
        case "Change" :
            nicknameOf[uid] = userInfo[2]
            break
            
        default:
            break
        }
    }
    
    ioMessages.forEach { (msg, uid) in
        guard let nickname = nicknameOf[uid] else { return }
        switch msg {
        case "Enter" :
            result.append("\(nickname)님이 들어왔습니다.")
            break
        case "Leave" :
            result.append("\(nickname)님이 나갔습니다.")
            break
            
        default:
            break
        }
    }
    return result
}

너무 복잡하게 생각하기도 했다.

struct와 map활용해보자.

map을 사용할 때 struct를 중간 매개채로 사용하면서 마지막에는 toString()를 mapping하여 String 배열을 반환하게!

태그: ,

카테고리:

업데이트:

댓글남기기