Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao - [3์ฐจ]๋ฐฉ๊ธˆ๊ทธ๊ณก (Lv.2) (feat. ๋ฌธ์ž์—ด ์น˜ํ™˜ replacingOccurrences)

๊ฐ์ž ๐Ÿฅ” 2022. 9. 10. 16:57
๋ฐ˜์‘ํ˜•

๐Ÿ“ 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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐ŸŸ  ๋ฌธ์ œ ํ’€์ด

๊ทธ๋ƒฅ ๋ฌธ์ œ๋ฅผ ์ฝ์œผ๋ฉด์„œ ์ฐจ๊ทผ์ฐจ๊ทผ ๋จธ๋ฆฟ์†์œผ๋กœ ๋กœ์ง์„ ๊ทธ๋ ค๋‚˜๊ฐ”๋‹ค. 

์ด๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ํ•จ์ˆ˜๋กœ ๋‚˜๋ˆ ์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

๐ŸŸ  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์ฐจ ํ’€์ด (์ •๋‹ต!!)

๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๊ผผ๊ผผํ•˜๊ฒŒ ์‚ดํŽด๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€๋„๋ก ํ•˜์Ÿˆ ๐Ÿ˜ฑ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%EB%B0%A9%EA%B8%88%EA%B7%B8%EA%B3%A1/main.swift

 

GitHub - deslog/Algorithm: โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ

โœจ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Swift ๋ฌธ์ œํ’€์ด โœจ. Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

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))

 

๋ฐ˜์‘ํ˜•