ํ๋ก๊ทธ๋๋จธ์ค - ์คํ์ฑํ
๋ฐฉ(Swift)
2022. 10. 17. 23:55ใํ๋ก๊ทธ๋๋จธ์ค-Swift
ํ๋ก๊ทธ๋๋จธ์ค - ์คํ์ฑํ ๋ฐฉ(Swift)
๋ฌธ์ ์ค๋ช
https://school.programmers.co.kr/learn/courses/30/lessons/42888
๋์ ํ์ด
๋์ ๋๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ id์ ์ฐ๊ฒฐ๋ ๋๋ค์์ ์ง์ ํ๊ณ ํ๋ฒ๋ ๋์์ ํ๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
์ด ํ์ด์ ๋น์ทํ ๋ค๋ฅธ ์ฌ๋์ ํ์ด๋ฅผ ๋ณด๋ ์ฒซ ๋ฒ์งธ for๋ฌธ์์ Enter์ Change์ธ์ง ํ์ธํ๋ ๊ฒ์ด ์๋ user์ count๋ก ๊ฐ์ ๋ฃ์ด์ฃผ์๋ค.
import Foundation
func solution(_ record:[String]) -> [String] {
var dic:[String: String] = [:]
var answer:[String] = []
for x in record {
let user = x.components(separatedBy: " ")
if user[0] == "Enter" || user[0] == "Change" {
let id = user[1], nickName = user[2]
dic[id] = nickName
}
}
for x in record {
let user = x.components(separatedBy: " ")
if user[0] == "Enter" {
answer.append("\(dic[user[1]]!)๋์ด ๋ค์ด์์ต๋๋ค.")
} else if user[0] == "Leave" {
answer.append("\(dic[user[1]]!)๋์ด ๋๊ฐ์ต๋๋ค.")
}
}
return answer
}
๋ค๋ฅธ ์ฌ๋์ ํ์ด
๋ฌธ์์ด์์ contains๋ฅผ ํตํด Change๋ฅผ ๋ถ๋ฆฌ์ํจ๋ค๋ ์ ๊ณผ ๋ฐฐ์ด์ mappingํ ๋ actions๋ฐฐ์ด์ฒ๋ผ Enter์ Leave๋ฅผ ํค ๊ฐ์ผ๋ก ๋ ๊ฒ์ด ์ ๊ธฐํ ํ์ด์๋ค.
import Foundation
func solution(_ record:[String]) -> [String] {
let actions: [String: String] = ["Enter":"๋์ด ๋ค์ด์์ต๋๋ค.", "Leave":"๋์ด ๋๊ฐ์ต๋๋ค."]
var dic:[String: String] = [:]
record.forEach { x in
let user = x.components(separatedBy: " ")
if user.count > 2 {
dic[user[1]] = user[2]
}
}
return record.filter{ !$0.contains("Change") }.map { x in
let user = x.components(separatedBy: " ")
let newString = dic[user[1]]! + actions[user[0]]!
return newString
}
}
'ํ๋ก๊ทธ๋๋จธ์ค-Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค - ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ(Swift) (0) | 2022.10.18 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค - ์์ถ(Swift) (0) | 2022.10.18 |
ํ๋ก๊ทธ๋๋จธ์ค - ๋ด์ค ํด๋ฌ์คํฐ๋ง(Swift) (1) | 2022.10.15 |
ํ๋ก๊ทธ๋๋จธ์ค - ํ๋ฆฐํฐ(Swift) (0) | 2022.10.14 |
ํ๋ก๊ทธ๋๋จธ์ค - ๊ธฐ๋ฅ๊ฐ๋ฐ(Swift) (0) | 2022.10.14 |