์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’, JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ

๊ธฐ๊ฐ€์ •ํ›ˆ 2023. 6. 3. 21:09

 

 

1. ๋ฌธ์ œ ๋งํฌ - ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’

 

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

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

programmers.co.kr

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

 

2. ์ฝ”๋“œ

func solution(_ s:String) -> String {
    let arr = s.split(separator: " ").map { Int(String($0))!}.sorted()
    return String(arr.first!) + " " + String(arr.last!)   
}

 

3.  ์ฝ”๋“œ ์„ค๋ช…

๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„์ด ๋˜์–ด ์žˆ์–ด split(separator: " ") ์ด์šฉํ•˜์—ฌ ํ•˜๋‚˜์”ฉ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์—ˆ์œผ๋ฉฐ, map๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ์š”์†Œ๋“ค์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ณ€ํ™˜๋œ ์ˆซ์ž ๋ฐฐ์—ด์„ sorted()๋ฅผ ํ•˜์—ฌ ์ •๋ ฌ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’, ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์€ ์ œ์ผ ํฐ ๊ฐ’์ด๋ฏ€๋กœ first, last๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

-> ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ๋‹ค์‹œ ์ƒ๊ฐํ•ด ๋ณด๋‹ˆ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’, ํฐ ๊ฐ’์€ min() , max()๋กœ๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์ƒ๊ฐ๋‚ฌ์Šต๋‹ˆ๋‹ค.

 

 


1. ๋ฌธ์ œ ๋งํฌ - JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ

 

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

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

programmers.co.kr

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

 

2. ์ฝ”๋“œ

func solution(_ s:String) -> String {
    
    var arr = Array(s.map { String($0.lowercased()) })
        
    for i in 0..<arr.count{
        
        if Int(arr[i]) != nil{
            continue
        }else if i == 0 {
            arr[i] = arr[i].uppercased()
        }
        
        if i + 1 < arr.count {
            if arr[i] == " " && arr[i+1] != " " {            
                arr[i+1] = arr[i+1].uppercased()
            }
        }
    }

    return arr.joined()
}

 

3.  ์ฝ”๋“œ ์„ค๋ช…

์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด s๋ฅผ ์ „๋ถ€ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ( lowercased() ) ๋ฐฐ์—ด arr๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋ฐฐ์—ด arr์„ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ if ๋ฌธ์€ Int()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€ํ™˜ํ•˜์—ฌ ์ˆซ์ž์ธ ๊ฒฝ์šฐ continue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ์ˆœํšŒ๋กœ ๋„˜๊น๋‹ˆ๋‹ค.

else if ๋ถ€๋ถ„์—์„œ๋Š” ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๊ณ  ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์ธ ๊ฒฝ์šฐ ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ํ•ด๋‹น ์œ„์น˜์— ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ if ๋ฌธ์—์„œ๋Š” ํ˜„์žฌ ์š”์†Œ๊ฐ€ ๊ณต๋ฐฑ์ด๊ณ  ๋‹ค์Œ ์š”์†Œ๊ฐ€ ๊ณต๋ฐฑ์ด ์•„๋‹Œ ๊ฒฝ์šฐ, ๋‹ค์Œ ์š”์†Œ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ•ด๋‹น ์œ„์น˜์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

joined()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด arr์˜ ๊ฐ’๋“ค์„ ํ•ฉ์ณ์„œ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.