๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ์ด์ƒํ•œ ๋ฌธ์ž ๋งŒ๋“ค๊ธฐ Programmers(Lv.1)

by seolhee2750 2021. 6. 14.
๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด s๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.

 

์ œํ•œ ์กฐ๊ฑด
  • ๋ฌธ์ž์—ด ์ „์ฒด์˜ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ, ๋‹จ์–ด(๊ณต๋ฐฑ์„ ๊ธฐ์ค€)๋ณ„๋กœ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ํŒ๋‹จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋กœ ๋ณด์•„ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ
s return
"try hello world" "TrY HeLlO WoRlD"

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
func solution1(_ s:String) -> String {
    var arr = s.components(separatedBy: " ")

    for i in 0..<arr.count {
        var temp = ""
        for (j, char) in arr[i].enumerated() {
            if j % 2 == 0 {
                temp += char.uppercased()
            }
            else {
                temp += char.lowercased()
            }
        }
        arr[i] = temp
    }

    return arr.joined(separator: " ")
}
  • components๋ฅผ ์ด์šฉ, ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด s๋ฅผ ๋‚˜๋ˆ ์„œ ๋ฐฐ์—ด๋กœ ์ƒ์„ฑํ–ˆ๋‹ค.
  • ๋ฐฐ์—ด ์•ˆ์—์„œ ์ง์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.
  • ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์„ joined ํ•ด์ฃผ์—ˆ๋‹ค.

 

์ธ์ƒ์ ์ธ ํ’€์ด
import Foundation
func solution(_ s:String) -> String {
    let a = s.components(separatedBy: " ").map { $0.enumerated().map { $0.offset % 2 == 0 ? $0.element.uppercased() : $0.element.lowercased() } }
    return a.map{ $0.map { $0 }.joined() }.joined(separator: " ")
}
  • components, map, enumerated ๋“ฑ,, ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ์ค„๋กœ ๋๋ƒˆ๋‹ค.
  • ์‚ฌ์‹ค ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ ๋ดค์„ ๋•Œ ์ข‹์€ ์ฝ”๋“œ์ธ์ง€๋Š” ํ™•์‹ค์น˜ ์•Š์ง€๋งŒ
    ํ•จ์ˆ˜๋“ค์˜ ๋‹ค์–‘ํ•œ ํ™œ์šฉ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๊ธฐ ์ข‹์•„์„œ ๊ฐ€์ ธ์™€ ๋ดค๋‹ค. 

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ข€ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.
  • ๋‹ค์–‘ํ•˜๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์— ์žˆ์–ด์„œ ๊ณต๋ถ€๊ฐ€ ๋” ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 


 

๐Ÿ– [ ์ถ”๊ฐ€ ] 1์ฃผ์ผ ํ›„ ๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ

func solution2(_ s:String) -> String {
    var result = ""
    var count = 0
    
    for i in s {
        if i == " " {
            result.append(" ")
            count = 0
        }
        else {
            count % 2 == 0 ? result.append(String(i.uppercased())) : result.append(String(i.lowercased()))
            count += 1
        }
    }
    return result
}
  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด ๊ทธ ์ž์ฒด์—์„œ, ๊ณต๋ฐฑ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ฒ€์‚ฌํ•˜์˜€๋‹ค.
  • ๊ณต๋ฐฑ์ด ์•„๋‹๋•Œ๋Š” ์ŠคํŠธ๋ง ์š”์†Œ์—์„œ ์ง, ํ™€์„ ํŒ๋ณ„ํ•˜์—ฌ
    ์ง์ˆ˜๋ฒˆ์งธ์˜ ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ์‹์„ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์ž‘์„ฑํ–ˆ๋‹ค.
  • ๊ตณ์ด ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์•˜๊ณ , ๊ทธ์— ๋”ฐ๋ผ components ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ด์œ ๋„ ์—†๊ธฐ์—
    ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋ณด๋‹ค ๋” ๊น”๋”ํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 


 

๋ฌธ์ œ

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

๋Œ“๊ธ€