swift 오픈채팅방

quokka·2022년 2월 14일
0

코딩테스트

목록 보기
38/63

https://programmers.co.kr/learn/courses/30/lessons/42888

3, 9, 10, 11, 12, 14, 16, 24, 25, 26, 27, 28, 29 실패가 난다.

import Foundation

func solution(_ record:[String]) -> [String] {
    var result = [String]()
    var dic = [String: String]()
    
    for i in record {
        let array = i.split(separator: " ").map{String($0)}
        var sentence = ""
        if array[0] == "Enter" {
            dic.updateValue(array[2], forKey: array[1])
            sentence = array[1] + " " + array[0]
            result.append(sentence)
        } else if array[0] == "Change" {
            dic.updateValue(array[2], forKey: array[1])
        } else if array[0] == "Leave" {
            sentence = array[1] + " " + array[0]
            result.append(sentence)
        }
    }
    
    for (j, v) in result.enumerated() {
        let sentences = v.split(separator: " ").map({String($0)})
        let id = sentences[0]
        let sentence = sentences[1]
        result[j] = result[j].replacingOccurrences(of: id, with: dic[id]!)
        if sentence == "Enter" {
            result[j] = result[j].replacingOccurrences(of: " Enter", with: "님이 들어왔습니다.")
        } else if sentence  == "Leave" {
            result[j] = result[j].replacingOccurrences(of: " Leave", with: "님이 나갔습니다.")
        }
    }
    
    return result
}
print(solution(["Enter uid1234 Muzi", "Enter uid4567 Prodo","Leave uid1234","Enter uid1234 Prodo","Change uid4567 Ryan"]))
profile
iOS를 공부하는 개발자입니다~ㅎㅎ

0개의 댓글

관련 채용 정보