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

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao - [3์ฐจ]ํŒŒ์ผ๋ช… ์ •๋ ฌ (Lv.3) (feat. ์˜ˆ์™ธ ์ผ€์ด์Šค ์ ์šฉ์‹œ์ผœ ๋ณด๊ธฐ)

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

๐ŸŸ  TIL ์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ

๐Ÿ“ ํด๋กœ์ € ํ™œ์šฉํ•ด์„œ array ์ด์ค‘(?), ๋‹ค์ค‘ ์ •๋ ฌํ•˜๊ธฐ

ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•ด์„œ array์— ์—ฌ๋Ÿฌ ์กฐ๊ฑด๋ณ„๋กœ ์ •๋ ฌํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์—๋Š”, ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

let myGrades = [
    ["B", "๋‹ค"],
    ["A", "๋ฐ”"],
    ["C", "๋‚˜"],
    ["B", "๊ฐ€"],
    ["A", "๋ผ"],
    ["A", "๋งˆ"]
]

print(myGrades.sorted { ($0[0] < $1[0]) || ($0[1] > $1[1]) })
// [["A", "๋ฐ”"], ["A", "๋งˆ"], ["A", "๋ผ"], ["B", "๋‹ค"], ["B", "๊ฐ€"], ["C", "๋‚˜"]]

์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ๋˜๋ฉด, ABC ์˜ค๋ฆ„์ฐจ์ˆœ์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ, ๋’ค์— '๊ฐ€๋‚˜๋‹ค'๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ๋‹ค. || ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๊ทธ ๊ธฐ์ค€์„ ์œ ์ง€ํ•˜๊ณ  ๋’ค์— ์ถ”๊ฐ€ ์กฐ๊ฑด์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค

์›๋ž˜ ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์˜ ํด๋กœ์ € ๋‚ด๋ถ€์—์„œ sorted by ๋ฅผ ์ด์šฉํ•ด์„œ ์ •๋ ฌํ–ˆ์—ˆ๋Š”๋ฐ, ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค. ์‹ ๋ฐ•ํ–ˆ๋‹ค!

let customSort: ([String], [String]) -> Bool = { a, b in
    if a[0].lowercased() != b[0].lowercased() {
        return a[0].lowercased() < b[0].lowercased() // ๋‹ค๋ฅด๋‹ค๋ฉด ์‚ฌ์ „์ˆœ์ •๋ ฌ
    } else {
        if let n1 = Int(a[1]), let n2 = Int(b[1]), n1 != n2 {
            return n1 < n2 // ์ˆซ์ž๋‹ค๋ฅด๋‹ค๋ฉด ํฐ์ˆœ์œผ๋กœ ์ •๋ ฌ
        }
    }
    return false // ๋ฌธ์ž๋„ ๊ฐ™๊ณ  ์ˆซ์ž๋„ ๊ฐ™๋‹ค๋ฉด ์•ˆ๋ฐ”๊ฟˆ
}

newFiles.sort(by: customSort)

์ด๋ ‡๊ฒŒ ์ •๋ ฌ ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. by: ์•ˆ์— ์—ฐ์‚ฐ์ž๋ฅผ ๋„ฃ์–ด์•ผํ•  ๊ฒƒ๊ฐ™์ง€๋งŒ, ์—ฐ์‚ฐํ”„๋กœํผํ‹ฐ(?๋งž๋‚˜์š”?)๋ฅผ ๋Œ€์‹ ํ•ด์„œ ๋„ฃ์—ˆ๋‹ค. sorted(by:) ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ customSort(_:_:)์˜ ๊ทœ์น™์„ ๋”ฐ๋ผ ์–ด๋ ˆ์ด๋ฅผ ์ •๋ ฌํ•˜๊ณ  ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

์ •๋ ฌํ• ๋•Œ, ์ด๋ ‡๊ฒŒ ์กฐ๊ฑด์„ ์ด์šฉํ•ด์„œ ๊ธฐ์ค€์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ทธ ๊ทœ๊ทœ์น™์„ ๋”ฐ๋ฅด๊ฒŒ๋” ์ •๋ ฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊ผญ ๊ธฐ์–ตํ•ด์•ผ๊ฒ ๋‹ค. ์œ„์ฒ˜๋Ÿผ ์—ฐ์‚ฐํ”„๋กœํผํ‹ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜๋กœ๋„ ์ •๋ ฌ ํด๋กœ์ € ๊ทœ์น™์„ ์ƒ์„ฑํ•ด์„œ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]

func backward(_ s1: String, _ s2: String) -> Bool {
    return s1 > s2
}
var reversedNames = names.sorted(by: backward)
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]

์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋„, by์•ˆ์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜์˜ ๊ทœ์น™์„ ๋”ฐ๋ผ ๊ฐ’์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

 

๐Ÿ“ map์„ ์ด์šฉํ•ด์„œ Array์— ์žˆ๋Š” ์›์†Œ๋“ค์„ ํ•ฉ์ณ์„œ ์ถœ๋ ฅํ•˜๊ธฐ

์›๋ž˜ ๋‚˜๋Š” for๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค. ๊ทผ๋ฐ ์ด๊ฑฐ... ๊ณ ์ฐจํ•จ์ˆ˜ map์œผ๋กœ ์‘์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์›๋ž˜๋Œ€๋กœ๋ผ๋ฉด, [["a","b","c"], ["d","e","f"]] ๋ฅผ ["abc", "def"]๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

for f in sortedFiles {
    var temp = ""
    for i in 0..<f.count {
        temp += f[i]
    }
    answer.append(temp)
}

for๋ฌธ์œผ๋กœ ํ•œ [array]์”ฉ ๊บผ๋‚ธ ํ›„, ๊ฑฐ๊ธฐ์„œ ๋˜ ๊ธธ์ด๋งŒํผ for ๋ฌธ์„ ๋Œ๋ ค์„œ ๋”ํ•ด์ฃผ์—ˆ๋‹ค. ๋ฌด๋ ค ์ด์ค‘ํฌ๋ฌธ..์„์‚ฌ์šฉํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ฅผ ๊ณ ์ฐจํ•จ์ˆ˜ map ๊ณผ join ์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค.

newFiles.map{$0.joined()}

newfile์— ์žˆ๋Š” array๋“ค์„ ํ•œ๋ฉ์ด์”ฉ ๊บผ๋‚ธ ํ›„ ($0), ์—ฌ๊ธฐ์žˆ๋Š” ๊ฒƒ๋“ค์„ string ์œผ๋กœ ์ „๋ถ€ ์—ฎ์–ด์ฃผ๋Š” join ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๊ฐ„๋‹จ... ์Šค.. ํ•˜๋‹ค. .. ์•ž์œผ๋กœ ์ด๋Ÿฐ ์ผ์ด ์žˆ์„๋•Œ map ๊ณผ join ์„ ๊ผญ ๊ธฐ์–ตํ•ด๋‚ด์ž!

 

๐ŸŸ  ๋ฌธ์ œ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/17686

 

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

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

programmers.co.kr

 

๐ŸŸ  ๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

file ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ

  • ์šฐ์„  ์ฃผ์–ด์ง„ file๋“ค์ด head, number, tail ์— ๋”ฐ๋ผ์„œ ์ •๋ ฌ ์ˆœ์„œ๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ, file์˜ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•ด์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
  • isnumber, isletter์„ ์ด์šฉํ•ด์„œ split ํ•ด์ฃผ์—ˆ๋‹ค.
    • isLetter๋Š” ์˜์–ด ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋“ฏํ–ˆ๋‹ค. ์˜จ์ ์ด๋‚˜ ํ•˜์ดํ”ˆ, ๋„์–ด์“ฐ๊ธฐ (๊ณต๋ฐฑ)์€ ์ธ์‹ํ•˜์ง€ ๋ชปํ•ด์„œ, ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผํ–‡๋‹ค. 
    • isASCII๋„ ์žˆ์—ˆ๋Š”๋ฐ ์ด๋Š” ๋ฌธ์ž ์ „์ฒด๋ฅผ ์ง€์นญํ•˜๊ณ  ์žˆ์–ด์„œ letter๋„, ์˜จ์  ๊ณต๋ฐฑ ๋“ฑ๋“ฑ๋„ ๋ชจ๋‘ ํ•ด๋‹นํ•˜๋Š” ์กฐ๊ฑด์ด๋ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.
    • ์–ด์ฐจํ”ผ ํŒŒ์ผ๋ช…์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์˜จ์ , ํ•˜์ดํ”ˆ(-), ๊ณต๋ฐฑ๋ฟ์ด๋ผ์„œ ์„ธ๊ฐœ๋ฅผ ์ด์šฉํ•ด์„œ ๋”ฐ๋กœ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ์—ฌ๊ธฐ์— ํฌํ•จํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋กœ ์กฐ๊ฑด์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.
  • istail์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฃผ์—ˆ๋‹ค.
    • ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž์ด head, ๋’ค๊ฐ€ tail ์ธ๋ฐ, ์ด๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ bool๊ฐ’์„ ํ•˜๋‚˜ ์ค€ ๊ฒƒ์ด๋‹ค.
    • ๋งŒ์•ฝ istail ์ด true๊ฐ’์„ ๊ฐ€์ง€๋ฉด, ์ด๋ฏธ ์ค‘๊ฐ„์ง€์ ์„ ์ง€๋‚ฌ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ, ๊ทธ ์ดํ›„์— ๋งŒ๋‚˜๋Š” ๋ฌธ์ž๋“ค์€ ๋ชจ๋‘ tail์— ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

์ •๋ ฌํ•˜๊ธฐ

์ •๋ ฌ ์ฝ”๋“œ๋Š” ์ธํ„ฐ๋„ท์„ ์ฐธ๊ณ ํ–ˆ๋‹ค. ์›๋ž˜ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ, ๋Œ€๋ฌธ์ž๊ฐ€ ๋จผ์ € ์ •๋ ฌ๋˜๊ฑฐ๋‚˜, ๋‘๋ฒˆ์งธ ์กฐ๊ฑด๋•Œ๋ฌธ์— ์•ž ์กฐ๊ฑด์ด ์œ ์ง€๋˜์ง€ ์•Š์€ ์ฑ„๋กœ ๋‘๋ฒˆ์žฌ ์กฐ๊ฑด์œผ๋กœ ์ •๋ ฌ์ด ๋˜์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ฒผ๋‹ค.

var sortedFiles = newFiles.sorted(by: { $0[0] < $1[0] })
sortedFiles = sortedFiles.sorted(by: { Int($0[1])! < Int($1[1])! })

๋งž๋‹ค. ์กฐ๊ฑด์„ ๋‘๋ฒˆ ๋”ฐ๋กœ ์ฃผ์—ˆ์œผ๋‹ˆ๊นŒ, ์ฒซ๋ฒˆ์งธ ์ •๋ ฌํ•œ ๊ฒƒ์ด ๋‘๋ฒˆ์žฌ๊นŒ์ง€ ์œ ์ง€๋˜์ง€ ์•Š์€ ์ฑ„๋กœ ๋”ฐ๋กœ ์ •๋ ฌ๋๋˜ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์กฐ๊ฑด์ด ๋‚˜์™€์žˆ๋Š”๋ฐ, ๋‚˜๋Š” ๊ทธ๋ƒฅ ์ •๋ ฌํ•ด๋ฒ„๋ ธ๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€์†Œ๋ฌธ์ž์— ๋Œ€ํ•œ ์šฐ์„ ์ˆœ์œ„๋„ ๊ณ ๋ คํ•œ์ฑ„๋กœ ์ •๋ ฌ์ด ์ง„ํ–‰๋๋‹ค. (a์™€ A๋ฅผ ๋น„๊ตํ•จ, ์Šค์œ„ํ”„ํŠธ๋Š” ๋Œ€๋ฌธ์ž๊ฐ€ ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์€๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.)

๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

let customSort: ([String], [String]) -> Bool = { a, b in
    if a[0].lowercased() != b[0].lowercased() {
        return a[0].lowercased() < b[0].lowercased() // ๋‹ค๋ฅด๋‹ค๋ฉด ์‚ฌ์ „์ˆœ์ •๋ ฌ
    } else {
        if let n1 = Int(a[1]), let n2 = Int(b[1]), n1 != n2 {
            return n1 < n2 // ์ˆซ์ž๋‹ค๋ฅด๋‹ค๋ฉด ํฐ์ˆœ์œผ๋กœ ์ •๋ ฌ
        }
    }
    return false // ๋ฌธ์ž๋„ ๊ฐ™๊ณ  ์ˆซ์ž๋„ ๊ฐ™๋‹ค๋ฉด ์•ˆ๋ฐ”๊ฟˆ
}

newFiles.sort(by: customSort)

๋ฌธ์ œ ์กฐ๊ฑด์—์„œ ๋Œ€์†Œ๋ฌธ์ž์˜ ๊ตฌ๋ถ„์€ ์—†๋‹ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, a์™€ A๋ฅผ ๋น„๊ตํ•˜์ง€ ์•Š๊ณ  lowercase๋กœ ๋ณ€ํ˜•์‹œ์ผœ์„œ ๋น„๊ตํ•ด์ฃผ์—ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋ฉด A๋ฅผ ์ž ๊น a๋กœ ๋ฐ”๊ฟ”์„œ ๋น„๊ตํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, ๋‘๊ฐœ์— ๋™์ผํ•œ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋ถ€์—ฌ๋œ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์˜ ์ •๋ ฌ๋œ ์ˆœ์„œ๊ฐ€ ๊ฐ™๋‹ค๋ฉด, number์ธก์—์„œ ์ •๋ ฌ์„ ์ง„ํ–‰ํ•œ๋‹ค. ์ˆซ์ž๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ํฐ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ฃผ์—ˆ๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ if๋ฌธ์„ ํ†ต๊ณผํ•˜์ง€ ์•Š์€ ๊ฒƒ๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ๋ฌธ์ž๋„ ๊ฐ™๊ณ  ์ˆซ์ž๋„ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ ์•„๋ฌด๋ จ ์ •๋ ฌ๋„ ํ•ด์ฃผ์ง€ ์•Š์•˜๋‹ค.

 

๐ŸŸ  ์ฒซ๋ฒˆ์งธ ์‹œ๋„ (์‹คํŒจ 9๊ฐœ, 55์ )

์ฝ”๋“œ๋Š” ๋”๋ณด๊ธฐ๋ฅผ ํด๋ฆญํ•ด๋ณด์ž.

๋”๋ณด๊ธฐ

ํ‹€๋ฆฐ ์ฝ”๋“œ

func solution(_ files:[String]) -> [String] {
    var answer = [String]()
    var newFiles = splitFiles(files)
    
    let customSort: ([String], [String]) -> Bool = { a, b in
        if a[0].lowercased() != b[0].lowercased() {
            return a[0].lowercased() < b[0].lowercased() // ๋‹ค๋ฅด๋‹ค๋ฉด ์‚ฌ์ „์ˆœ์ •๋ ฌ
        } else {
            if let n1 = Int(a[1]), let n2 = Int(b[1]), n1 != n2 {
                return n1 < n2 // ์ˆซ์ž๋‹ค๋ฅด๋‹ค๋ฉด ํฐ์ˆœ์œผ๋กœ ์ •๋ ฌ
            }
        }
        return false // ๋ฌธ์ž๋„ ๊ฐ™๊ณ  ์ˆซ์ž๋„ ๊ฐ™๋‹ค๋ฉด ์•ˆ๋ฐ”๊ฟˆ
    }

    newFiles.sort(by: customSort)
    return newFiles.map{$0.joined()}
}

func splitFiles(_ files: [String]) -> [[String]] {
    let symbol = [".", "-", " "]
    var splitFiles = [[String]]()
    for f in files {
        var head = ""
        var num = ""
        var tail = ""
        var istail = false
        for char in f {
            if (char.isLetter && istail) || (istail && symbol.contains(String(char))) {
                tail += String(char)
            } else if char.isNumber {
                num += String(char)
                istail = true
            } else if (!istail && char.isLetter) || (!istail && symbol.contains(String(char))) {
                head += String(char)
            }
        }
        splitFiles.append([head, num, tail])
    }
    return splitFiles
}

print(solution(files))

๊ฒฐ๊ณผ๋Š”,, ใ…Š... ์ถฉ๊ฒฉ..

์™œํ‹€๋ ธ๋Š”์ง€ ์•Œ์•„๋ณด์Ÿˆ... 

 

๐ŸŸ  ๋‘๋ฒˆ์งธ ์‹œ๋„ (์ •๋‹ต!)

์™œ ํ‹€๋ ธ์„๊นŒ!! ๋Ÿฐํƒ€์ž„์—๋Ÿฌ๋„, ์‹œ๊ฐ„์ดˆ๊ณผ๋„ ์•„๋‹ˆ๊ณ  ์‹คํŒจ์ธ๊ฒƒ์œผ๋กœ ๋ณด์•„, ๋ช‡๊ฐœ์˜ ์˜ˆ์™ธ์ผ€์ด์Šค์—์„œ ์ •๋‹ต์„ ์ž˜ ๋ฆฌํ„ดํ•˜์ง€ ๋ชปํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ์˜ˆ์™ธ์‚ฌํ•ญ์ด ์žˆ์„์ง€ 'ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์งˆ๋ฌธํ•˜๊ธฐ'๋ฅผ ๋’ค์ง€๋ฉด์„œ, ์—ฌ๋Ÿฌ ์‚ฌ๋žŒ๋“ค์˜ ์—๋Ÿฌ์‚ฌํ•ญ๋“ค์„ ๋ณด๋ฉด์„œ ์›์ธ์„ ์ฐพ์•„๋‚˜์„ฐ๋‹ค. (์ด๊ฑฐ ์ด๋ ‡๊ฒŒ ์˜ˆ์™ธ์ผ€์ด์Šค๋ฅผ ์งˆ๋ฌธํ•˜๊ธฐ, ์ด๋Ÿฐ๊ฑฐ ์—†์œผ๋ฉด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•ด๋‚ด์ฃ ? ใ… ใ…  ์‹œํ—˜๋ณผ๋•Œ ์ด๋Ÿฐ ์˜ˆ์™ธ์ผ€์ด์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๋ƒ” ๋ง์ด๋Œœ ใ… ใ… )

์—ฌ๋Ÿฌ์‚ฌ๋žŒ๋“ค์˜ ์ผ€์ด์Šค๋ฅผ ์‚ดํŽด๋ณด๋‹ค๊ฐ€, '์ˆซ์ž๋กœ ๋๋‚˜๋Š” ํŒŒ์ผ๋ช…'์ผ ๊ฒฝ์šฐ์—, ์ˆซ์ž๋ฅผ ์ œ๋Œ€๋กœ tail์— ๋„ฃ์ง€ ๋ชปํ•˜๋Š” ์ด์Šˆ๊ฐ€ ์žˆ์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๋ฐ”๋กœ ์ผ€์ด์Šค๋ฅผ ์ ์šฉํ•ด๋ณด์•˜๋‹ค.

let files = ["O00321", "O49qcGPHuRLR5FEfoO00321"]

๊ฒฐ๊ณผ๊ฐ€ ์œ„์ฒ˜๋Ÿผ ๋‚˜์™”๋‹ค. head๋Š” ์˜์–ด O๊ฐ€ ์ž˜ ๋“ค์–ด๊ฐ€์ง€๋งŒ, ๋‘๋ฒˆ์งธ ์ผ€์ด์Šค number์— ์ฃผ๋ชฉํ•ด๋ณด์ž. 49๋งŒ number์— ๋“ค์–ด๊ฐ€๊ณ , qcGPHuRLR5FEfoO00321๋Š” ์ „๋ถ€ tail ์— ๋“ค์–ด๊ฐ€์•ผํ•˜๋Š”๋ฐ, tail ๋ถ€๋ถ„์— ๋“ค์–ด๊ฐ€์•ผํ•  ์ˆซ์ž๋“ค์ด ์ „๋ถ€ number์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค.

๋‚ด๊ฐ€ ์ง  ์กฐ๊ฑด์„ ์‚ดํŽด๋ณด์ž.

if (char.isLetter && istail) || (istail && symbol.contains(String(char))) {
    tail += String(char)
} else if char.isNumber {
    num += String(char)
    istail = true
} else if (!istail && char.isLetter) || (!istail && symbol.contains(String(char))) {
    head += String(char)
}

isNumber์— ํ•ด๋‹นํ•˜๋ฉด ๋ฌด์กฐ๊ฑด num string์—๋‹ค๊ฐ€ ์ถ”๊ฐ€์‹œ์ผœ์ฃผ๋Š”๊ฒƒ์ด ๋ฌธ์ œ์˜€๋‹ค. ๊ทธ๋Ÿผ tail๋ถ€๋ถ„์— ๋“ค์–ด๊ฐˆ ์ˆซ์ž๋“ค์„ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ณ„ํ•ด์ฃผ๋ฉด ์ข‹์„๊นŒ? 

ํŒŒ์ผ๋ช…์€ '์ˆซ์ž'๋กœ head์™€ tail๋กœ ๊ตฌ๋ณ„๋œ๋‹ค. ๊ทธ๋ž˜์„œ A12B34 ๊ฐ€ ์žˆ๋‹ค๊ณ  ์น ๋•Œ, 12๋ฅผ ๊ธฐ์ค€์œผ๋กœ A๋Š” head, B34๋Š” tail ๋กœ ๊ฐ€์•ผํ•œ๋‹ค. ๋’ค์— 3๊ณผ 4๋Š” ๊ทธ๋Ÿฌ๋ฉด, ์ˆซ์ž์ด๋ฉด์„œ, tail ์ด ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด, tail ์— ๋„ฃ์œผ๋ฉด๋œ๋‹ค. 1๊ณผ 2๋Š” ์ˆซ์ž์ด๋ฉด์„œ, tail ์ด ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ, num์—๋‹ค๊ฐ€ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ์ด๋‹ค. ์กฐ๊ฑด์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊พธ์–ด์„œ ์ง„ํ–‰ํ•ด๋ณด๋‹ˆ, ์„ฑ๊ณตํ–ˆ๋‹ค โค๏ธ‍๐Ÿ”ฅ

if (char.isLetter && istail) || (istail && symbol.contains(String(char))) || (!tail.isEmpty && char.isNumber && istail) {
    tail += String(char)
} else if char.isNumber && tail.isEmpty {
    num += String(char)
    istail = true
} else if (!istail && char.isLetter) || (!istail && symbol.contains(String(char))) {
    head += String(char)
}

ํœด,, ๋น„๊ต์  ์˜ค๋ฅ˜๋ฅผ ๋นจ๋ฆฌ์ฐพ์•„์„œ ์•ฝ 30๋ถ„๋งŒ์— ํ•ด๊ฒฐํ–ˆ๋‹ค. ๋„๋Œ€์ฒด... ์ฝ”ํ…Œ๋ณผ๋•Œ ์˜ˆ์™ธ์ผ€์ด์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•ด๋‚ต๋ƒ๊ณ ์˜ค ใ… ใ… ใ…  ๋„ˆ๋ฌด์–ด๋ ต๋‹ค.

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_%ED%8C%8C%EC%9D%BC%EB%AA%85%20%EC%A0%95%EB%A0%AC/main.swift

 

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

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

github.com

func solution(_ files:[String]) -> [String] {
    var newFiles = splitFiles(files)

    let customSort: ([String], [String]) -> Bool = { a, b in
        if a[0].lowercased() != b[0].lowercased() {
            return a[0].lowercased() < b[0].lowercased()
        } else {
            if let n1 = Int(a[1]), let n2 = Int(b[1]), n1 != n2 {
                return n1 < n2 
            }
        }
        return false
    }

    newFiles.sort(by: customSort)
    return newFiles.map{$0.joined()}
}

func splitFiles(_ files: [String]) -> [[String]] {
    let symbol = [".", "-", " "]
    var splitFiles = [[String]]()
    for f in files {
        var head = ""
        var num = ""
        var tail = ""
        var istail = false
        for char in f {
            if (char.isLetter && istail) || (istail && symbol.contains(String(char))) || (!tail.isEmpty && char.isNumber && istail) {
                tail += String(char)
            } else if char.isNumber && tail.isEmpty {
                num += String(char)
                istail = true
            } else if (!istail && char.isLetter) || (!istail && symbol.contains(String(char))) {
                head += String(char)
            }
        }
        print("head์€ \(head), number์€ \(num), tali ์€ \(tail)")
        splitFiles.append([head, num, tail])
    }
    return splitFiles
}

 

๋ฐ˜์‘ํ˜•