Focus On Develop ๐ŸคŸ๐ŸคŸ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2016๋…„ ๋ณธ๋ฌธ

iOS [Swift]/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2016๋…„

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2020. 12. 16. 23:08

<๋ฌธ์ œ>


<์ „๋žต์งœ๊ธฐ>

์ด ๋ฌธ์ œ๋Š” ๋งค๋‹ฌ ๋ช‡์ผ๊นŒ์ง€ ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š”๊ฒŒ ํฌ์ธํŠธ๋‹ค.. (๋‹ค๋“ค ์ฃผ๋จน์ง€๊ณ  ํฐ๋‹ฌ ์ž‘์€๋‹ฌ ์•„์‹œ์ฅฌ..!?ใ…‹ใ…‹๐Ÿ˜) ์—์ด~ ์ฝ”๋”ฉ์ธ๋ฐ ์š”์ผ ๊ตฌํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• ์žˆ์„๊นŒ? ๋ผ๊ณ  ์ ‘๊ทผํ•˜๋ฉด ์–ด๋ ค์›Œ์ง„๋‹ค.. "๋งค๋‹ฌ ๋ช‡์ผ๊นŒ์ง€ ์žˆ๋Š”์ง€ ์ฝ”๋“œ๊ฐ€ ์•Œ๊ณ  ์žˆ์–ด์•ผํ•œ๋‹ค"๋ฅผ ๋ช…์‹ฌํ•˜์ž. ํ•˜๋“œ์ฝ”๋”ฉ์ด ์•„๋‹ˆ๋‹ˆ๊นŒ ์•ˆ์‹ฌํ•˜์ž..ใ…‹ใ…‹ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด 1์›”1์ผ์ด ๊ธˆ์š”์ผ์ด๋‹ˆ๊นŒ ์š”์ผ์€ ๊ธˆ~๋ชฉ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , 1์›” 1์ผ๋ถ€ํ„ฐ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋‚ ์งœ๊นŒ์ง€ ์ด ์ผ์ˆ˜ % 7๋กœ ๊ตฌํ•˜๋ฉด ๋  ๋“ฏ ํ•˜๋‹ค. ์—ฌ๊ธฐ์„œ ํ•œ๊ฐ€์ง€ ์ฃผ์˜ํ•ด์•ผ ํ• ์ ์€, ์˜ˆ๋ฅผ๋“ค์–ด 1์›”2์ผ์ด๋ผ๊ณ  "์ผ์ˆ˜"๊ฐ€ 2๊ฐ€๋˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ 1์ด๋œ๋‹ค๋Š” ์ ์ด๋‹ค.ใ…‹ (-1์„ ์žŠ์ง€๋ง์ž)

 

<์ฝ”๋“œ>

let days31: [Int] = [1, 3, 5, 7, 8, 10, 12]
let days30: [Int] = [4, 6, 9, 11]
let weekDays: [String] = ["FRI","SAT","SUN","MON","TUE","WED","THU"]

func solution(_ a:Int, _ b:Int) -> String {
    var totalDays = 0
    var result: String
    
    for i in 1 ..< a {
        if days31.contains(i) {
            totalDays += 31
        } else if days30.contains(i) {
            totalDays += 30
        } else if i == 2 {
            totalDays += 29
        }
    }
    
    totalDays += (b-1)
    return weekDays[(totalDays % 7)]
}

 

<Lesson Learned>

์ •๋‹ต์€ ๋ฌด๋‚œํžˆ ๋‚˜์™”๋Š”๋ฐ.. ์‘..? ์ด๋ฒˆ์—๋„ ๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ reduce๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋“œ๊ฐ€ ๋‘์ค„์ด๋„ค..?? ์‘..?? ๐Ÿ˜ญ ๐Ÿ˜ญ

์–ธ์ œ๊นŒ์ง€ Lesson Learned๋ฅผ ์“ฐ๋ฉด์„œ map, filter, reduce๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์จ์•ผํ• ๊นŒ ใ…œ.ใ…œ ์ด๋ฒˆ์—๋Š” ์ง„์งœ ์–ด๋–ป๊ฒŒ ์จ์•ผํ• ์ง€ ๋ชฐ๋ž๋Š”๋ฐ.. ํ‘ํ‘

 

 

์ถœ์ € : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2016๋…„

Comments