๐ TIL - swift ํน์ ๋ฌธ์์ด ๋ณ๊ฒฝ, ๋ฌธ์์ด ์นํ, swift replace
๋ฌธ์ ๋ฅผ ํ๋ฉด์ ๋ด๊ฐ ๋ฐฐ์ด ํ๊ฐ์ง ์ ๊ธฐ! ๋ฌธ์์ด ์นํ~~
replacingOccurrences(of: , with: )
let musicCode = "CC#BCC#BCC#BCC#B"
let newMusicCode = musicCode.replacingOccurrences(of: "C#", with: "V")
.replacingOccurrences(of: "D#", with: "W")
.replacingOccurrences(of: "F#", with: "X")
.replacingOccurrences(of: "G#", with: "Y")
.replacingOccurrences(of: "A#", with: "Z")
print(newMusicCode)
// ๊ฒฐ๊ณผ: CVBCVBCVBCVB
pyhton์์ replace๋ก ๋ฌธ์์ด ์นํ์ ํ๋ ์ ์ด ์๋๋ฐ, ์ด๋ swift์์ replacingOccurrences์ด๋ค. ๋ฐ์ํ๋ ๊ฒ๋ค์ ์ ๋ถ replaceํด์ค๋ค๋ ๋ป์ผ๋ก ๋ฐ์๋ค์ผ ์ ์์ด์, ์ ๋ ๊ฒ .(์ ) ์ผ๋ก ์ฐ๊ฒฐํด์ ์ํ๋ ํญ๋ชฉ์ ์ ๋ถ ๋ฐ๊ฟ ์ ์๋ค.
โ ๏ธ ์ฝ๋๋ ์์๋๋ก ์งํํ๋ฏ๋ก, ๋ง์ฝ ๋ฐ๊พธ์ด์ผํ๋ ๋ฌธ์์ด์ด C#๊ณผ C##์ผ ๊ฒฝ์ฐ, C#์ด ๋จผ์ replace ๋์ด๋ฒ๋ ค์ ๋ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ์ ์๋ค.
๐ ๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/17683
๐ ๋ฌธ์ ํ์ด
๊ทธ๋ฅ ๋ฌธ์ ๋ฅผ ์ฝ์ผ๋ฉด์ ์ฐจ๊ทผ์ฐจ๊ทผ ๋จธ๋ฆฟ์์ผ๋ก ๋ก์ง์ ๊ทธ๋ ค๋๊ฐ๋ค.
์ด๊ฒ์ ๊ทธ๋๋ก ํจ์๋ก ๋๋ ์ ๊ตฌํํ๋ค.
๐ 1์ฐจ ํ์ด ( ๋ฐํ์์๋ฌ & ์คํจ )
1์ฐจ ํ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค.
//
// main.swift
// Algorithm
//
// Created by LeeJiSoo on 2022/09/10.
//
import Foundation
let m = "CC#BCC#BCC#BCC#B"
let musicinfos = ["03:00,03:30,FOO,CC#B", "04:00,04:08,BAR,CC#BCC#BCC#B"]
func divideMusicinfos(_ music: [String]) -> [[String]] {
var newMusicinfos = [[String]]()
for i in 0..<music.count {
let tempCode = music[i].split(separator: ",").map{ String($0) }
newMusicinfos.append(tempCode)
}
return newMusicinfos
}
func makeNewCode(_ code: String) -> String {
let newCode = code.replacingOccurrences(of: "C#", with: "V")
.replacingOccurrences(of: "D#", with: "W")
.replacingOccurrences(of: "F#", with: "X")
.replacingOccurrences(of: "G#", with: "Y")
.replacingOccurrences(of: "A#", with: "Z")
return newCode
}
func runMusic(_ startTime: String, _ endTime: String, _ code: String) -> String {
let start = startTime.split(separator: ":").map{ Int(String($0))! }
let end = endTime.split(separator: ":").map{ Int(String($0))! }
let timeDiff = (60 * end[0] + end[1]) - ( 60 * start[0] + start[1])
let code = makeNewCode(code).map { String($0) }
var runCode = ""
// ๋ถ๋น ์ฝ๋ ํ๋๋ผ์ ํ๋์ฉ ๋ฃ์ด์ค์ผํจ.
for i in 0..<timeDiff {
runCode += code[i % code.count]
}
return runCode
}
func solution(_ m:String, _ musicinfos:[String]) -> String {
let newM = makeNewCode(m)
let newMusicinfos = divideMusicinfos(musicinfos)
var result = "(None)"
for music in newMusicinfos { // music : [String]
let title = music[2]
let runCode = runMusic(music[0], music[1], music[3])
if runCode.contains(newM) {
result = title
}
}
return result
}
print(solution(m, musicinfos))
์ด๋ ๊ฒ ํ ๊ฒฐ๊ณผ, ํ ์คํธ 4์์ ๋ฐํ์ ์๋ฌ, ํ ์คํธ 20, 27, 28์์ ์คํจ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์๋ค.
๐ค ์คํจ ์ด์ ์ฐพ๊ธฐ
๋ฌธ์ ๋ฅผ ๋ค์ ์ฐจ๊ทผ์ฐจ๊ทผ ์ฝ์ด๋ณด๋ ์์ ๊ฐ์ ์กฐ๊ฑด์ ๋์ณค์๋ค. ๋ค์ค๊ฐ ๋ค์ ๋ ธ๋์ฝ๋ M์ด ์ค๋ณต๋ ์ ์์ผ๋, ์ค๋ณต๋๋ ๊ฒฝ์ฐ, ์ฌ์์๊ฐ์ด ๊ฐ์ฅ ๊ธด (๊ทธ๋๊น ๋ด๊ฐ ๊ตฌํ timeDiff๊ฐ ๊ฐ์ฅ ๊ธด) ์์ ์ ๋ฐํํด์ผํ๊ณ , ์ด๊ฑฐ๋ง์ ๊ฐ์ ๊ฒฝ์ฐ์๋ ๋จผ์ ์ ๋ ฅ๋ ์์ ์ ๋ชฉ์ ๋ฐํํด์ผํ๋ค. ์กฐ๊ฑด์ ์ถ๊ฐํด์ ๋ค์ ๋๋ ค๋ณด์.
๐ 2์ฐจ ํ์ด (์ ๋ต!!)
๋ฌธ์ ์ ์กฐ๊ฑด์ ๊ผผ๊ผผํ๊ฒ ์ดํด๋ณด๊ณ ๋ฌธ์ ๋ฅผ ํ๋๋ก ํ์ ๐ฑ
import Foundation
let m = "CC#BCC#BCC#BCC#B"
let musicinfos = ["03:00,03:30,FOO,CC#B", "04:00,04:08,BAR,CC#BCC#BCC#B"]
func divideMusicinfos(_ music: [String]) -> [[String]] {
var newMusicinfos = [[String]]()
for i in 0..<music.count {
let tempCode = music[i].split(separator: ",").map{ String($0) }
newMusicinfos.append(tempCode)
}
return newMusicinfos
}
func makeNewCode(_ code: String) -> String {
let newCode = code.replacingOccurrences(of: "C#", with: "V")
.replacingOccurrences(of: "D#", with: "W")
.replacingOccurrences(of: "F#", with: "X")
.replacingOccurrences(of: "G#", with: "Y")
.replacingOccurrences(of: "A#", with: "Z")
return newCode
}
func makeTimeDiff(_ startTime: String, _ endTime: String) -> Int {
let start = startTime.split(separator: ":").map{ Int(String($0))! }
let end = endTime.split(separator: ":").map{ Int(String($0))! }
return (60 * end[0] + end[1]) - (60 * start[0] + start[1])
}
func makeRunCode(_ timeDiff: Int, _ code: String) -> String {
var runCode = ""
let code = makeNewCode(code).map { String($0) }
// ๋ถ๋น ์ฝ๋ ํ๋๋ผ์ ํ๋์ฉ ๋ฃ์ด์ค์ผํจ.
for i in 0..<timeDiff {
runCode += code[i % code.count]
}
return runCode
}
func solution(_ m:String, _ musicinfos:[String]) -> String {
let newM = makeNewCode(m)
let newMusicinfos = divideMusicinfos(musicinfos)
var maxDiff = -9999
var result = "(None)"
for music in newMusicinfos { // music : [String]
let title = music[2]
let timeDiff = makeTimeDiff(music[0], music[1])
let runCode = makeRunCode(timeDiff, music[3])
if runCode.contains(newM) {
// ์ด๋ฐ์๋ ๋ฌด์กฐ๊ฑด ์ง์
ํด์ maxDiff ๊ฐฑ์
// ๊ทธ ์ดํ์๋ maxDiff๊ฐ ๋ ํด ๊ฒฝ์ฐ์๋ง ์ง์
// ๊ฐ์ผ๋ฉด ๊ฐฑ์ ํ ํ์์์ด ๊ทธ๋ฅ ์ด๋ฐ ๋
ธ๋ ์ถ๋ ฅํ๋ฉด ๋๋๊น ๋ฐ๋ก ์กฐ๊ฑด ์ฒ๋ฆฌ ํ์ ์์
if maxDiff < timeDiff {
maxDiff = timeDiff
result = title
}
}
}
return result
}
print(solution(m, musicinfos))