suvera-dev ๐Ÿฅฆ

20230330 TIL ๋ณธ๋ฌธ

Private/TIL

20230330 TIL

suvera 2023. 3. 31. 00:10

1. Swift ๋ณต์Šต 

๋ฐ˜๋ณต๋ฌธ ๊ตฌ๋ฌธ ๋„ค์ด๋ฐ.

var numbers: [Int] = [3, 2342, 6, 3252]

numbersLoop: for num in numbers {
	if num > 5 || num < 1 {
    	continue numbersLoop
    }
    
    var count: Int = 0
    
    printLoop: while true {
    
    	print(num)
        count += 1
        
        if count == num {
        	break printLoop
        }
    }
    
    removeLoop: while true {
		if numbers.first != num {
        	break numbersLoop
        }
        numbers.removeFirst()
    }
}

// 3
// 3
// 3
// numbers์—๋Š” 3์ด ์ œ๊ฑฐ๋จ

- ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์ œ์–ด ํ‚ค์›Œ๋“œ๊ฐ€ ์ž˜๋ชป๋œ ๋ฒ”์œ„์—์„œ ๋™์ž‘ํ•˜๋Š” ์‹ค์ˆ˜๋ฅผ ์ค„์—ฌ์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

- ๋˜, ๋„ค์ด๋ฐ์„ ํ•ด๋‘๋‹ˆ ์–ด๋–ค ๋ฐ˜๋ณต๋ฌธ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ๋„ ํŽธํ•œ ๊ฒƒ ๊ฐ™์•„์š”. ํ•„์š”ํ•  ๋•Œ ์จ๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹น.

 

 

2. ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ์Šค์œ„ํ”„ํŠธ

 

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ์Šค์œ„ํ”„ํŠธ - Struct, Class, Property, Method

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

suvera.tistory.com

์ด์ „์—๋„ ๊ณต๋ถ€ํ–ˆ๋˜ ๋‚ด์šฉ๋“ค์ด์ง€๋งŒ, ๋‹ค์‹œ๋ด๋„ ์ƒˆ๋กญ๊ฒŒ ๋Š๊ปด์ง€๋Š” ๊ฑด ์™œ์ผ๊นŒ์š”..

๋ฐ‘ ๋น ์ง„ ๋…์— ๋ฌผ ๋ถ“๊ธฐ๋ผ๋Š”๊ฒŒ ์ด๋Ÿฐ๊ฑธ๊นŒ์š” ? ใ…‹ใ…‹ใ…‹ &^^

ํ•˜์ง€๋งŒ ์–ด๋””์„œ ๋ดค๋Š”๋ฐ ๋ฐ‘์ด ๋น ์กŒ์œผ๋ฉด ๊ทธ๋ƒฅ ๊ทธ ์ด์ƒ์œผ๋กœ ๋ƒ…๋‹ค ๋“ค์ด ๋ถ€์œผ๋ผ๋„ค์š”.. 

 

์‚ฌ์‹ค.. ๊ฐœ๋ฐœ์ž๋กœ ์‚ด์•„๋‚จ์„ ์ˆ˜ ์žˆ์„์ง€.. ์ฐธ .. ๊ฑฑ์ •์ด๋„ค์š”.. 

์„ฑ๊ฒฉ ์ƒ ์ด๊ฒƒ ์ €๊ฒƒ ํ•˜๋Š”๊ฑธ ์ข‹์•„ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ

ํ•˜๋‚˜๋ฅผ ๊นŠ๊ฒŒ ํŒŒ๊ณ ๋“œ๋Š”๊ฑธ ์ž˜ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™๊ธดํ•ด์š”.

 

๊ทธ๋ž˜๋„ ์žฌ๋ฏธ๋ฅผ ๋Š๋ผ๋Š” ๊ฒƒ ์ค‘์—์„œ

๋จน๊ณ  ์‚ด ๋ฐฉ๋ฒ•์€ ์•„์ง ์ด๊ฒƒ ๋ฐ–์— ์—†์œผ๋‹ˆ๊นŒ ์ผ๋‹จ ํ•ด์•ผ๊ฒ ์ฃ .. ใ…‹ใ…‹ใ…‹

 

iOS ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ์ œ๋Œ€๋กœ ๊ฐœ๋ฐœ์„ ๊ณต๋ถ€ํ•˜๊ธฐ ์‹œ์ž‘ํ•œ๊ฑฐ ๊ฐ™์€๋ฐ,

๊ธฐ๋ณธ๊ธฐ๊ฐ€ ํƒ„ํƒ„ํ•˜์ง€ ๋ชปํ•œ ์ƒํƒœ์—์„œ ํ”„๋กœ์ ํŠธ๋ถ€ํ„ฐ ๋ƒ…๋‹ค ํ•ด๋ฒ„๋ ธ๋”๋‹ˆ ๊ณต๋ถ€ ๋ฐฉ๋ฒ•๋„ ์ž˜ ๋ชฐ๋ž๊ณ 

์–ด๋Š ์ˆœ๊ฐ„๋ถ€ํ„ฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๋ฐ ํ•œ๊ณ„๊ฐ€ ์žˆ๋”๋ผ๊ตฌ์š”.

๋‚ด ๋จธ๋ฆฌ์†์—์„œ ๋‚˜์˜ค๋Š” ์ฝ”๋“œ๊ฐ€ ์•„๋‹Œ, ๊ตฌ๊ธ€์— ์žˆ๋Š” ์ฝ”๋“œ๋“ค๋กœ ๋„๋ฐฐ๋œ.. ใ…‹ใ…‹ใ…‹

 

๊ทธ๋ฆฌ๊ณ  ์ฝ”๋“œ๋ฅผ ์งค ๋•Œ ๊ณต๋ถ€ํ–ˆ๋˜ ๋‚ด์šฉ๋“ค์„ ์˜์‹ํ•˜๊ณ  ์ ์šฉํ•˜๋ฉด์„œ ์งœ๋Š” ์Šต๊ด€์ด ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™์•„์š”.

์•ž์œผ๋กœ๋Š” ์ƒ๊ฐ์„ ๋” ํ™•์žฅ์‹œ์ผœ์„œ.. ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด์„œ ๋‹ค์–‘ํ•œ ๊ณ ๋ฏผ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€.. ๋˜์–ด์”€ ์ข‹๊ฒ ๋„ค์š”.. 

 

๊ทธ๋ž˜์„œ ์š”์ฆ˜์—” ์•Œ๊ณ ๋ฆฌ์ฆ˜, CS๋„ ์—ด์‹ฌํžˆํ•˜๊ณ 

iOS ์ด์™ธ์˜ ๋‹ค์–‘ํ•œ ์ง€์‹๋“ค์„ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค !

๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ ์ ์  ์ƒ๊ฐ์ด ํ™•์žฅ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”. 

์•ž์œผ๋กœ๋„ ๊พธ์ค€ํžˆ ์„ฑ์žฅํ•ด๋‚˜๊ฐ€์•ผ์ฃ  ! ํœด ..

 

๋‹ค๋“ค ํ™”์ดํŒ… ์ž…๋‹ˆ๋‹ค :)

 

 

3. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด -  N-Queen

 

Backtracking ๋ฐฑํŠธ๋ž˜ํ‚น ๋Œ€ํ‘œ ๋ฌธ์ œ ) NQueen - Swift๋กœ ๊ตฌํ˜„

NQueen ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ Swift ํ’€์ด๊ฐ€ ๋งŽ์ด ์—†์–ด์„œ ์˜ฌ๋ ค๋ด…๋‹ˆ๋‹ค. Backtracking ์ด๋ž€?๐Ÿ”ฅ ํ•ด๋ฅผ ์ฐพ๋Š” ๋„์ค‘ ํ•ด๊ฐ€ ์•„๋‹ˆ์–ด์„œ ๋ง‰ํžˆ๋ฉด, ๋˜๋Œ์•„๊ฐ€์„œ ๋‹ค์‹œ ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ธฐ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค. → ์ฆ‰, DFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ์•ฝ

suvera.tistory.com

 

4. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด - ์••์ถ•

์—ด์‹ฌํžˆ ํ‘ธ๋Š๋ผ.. ์ง„์ด ๋น ์ ธ์„œ ์ •๋ฆฌ๋Š” ๋‚ด์ผ ~.. ใ…‹ใ…‹ใ…‹ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด ๋ณด๊ณ  ์•ฝ๊ฐ„ ํ˜„ํƒ€ ์™”๋‹ค

์—‰๋ง์ง„์ฐฝ ๋‚ด ์ฝ”๋“œ.. ใ…‹ใ…‹ใ…‹ใ…‹ ๊ทผ๋ฐ ์˜ค๊ธฐ ์ƒ๊ฒจ์„œ ๋‹ต์€ ๋ชป๋ณด๋Š”.. 

 

func solution(_ msg:String) -> [Int] {

    var msgArray = Array(msg)
    let alphabet = ["A", "B", "C","D", "E", "F","G", "H", "I","J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

    var index = 1
    var dictionary = [String: Int]()

    for i in alphabet {
        dictionary[i] = index
        index += 1
    }

    func checkDic(word: String) -> Bool {
        if dictionary[word] != nil {
            return true
        } else {
            return false
        }
    }

    var dicIdx = 26
    var result = [Int]()

    var startIdx = 0
    var endIdx = 0

    func changeString() -> String {
        if startIdx == 0 || startIdx == msgArray.count-1 {
            return String(msgArray[startIdx])
        }

        var string = String(msgArray[startIdx...endIdx])

        while true {

            if Array(dictionary.keys).contains(string) == false {
                endIdx -= 1
                return String(msgArray[startIdx...endIdx])
            } else {
                endIdx += 1

                if endIdx == msgArray.count {
                    endIdx -= 1
                    return String(msgArray[startIdx...endIdx])
                }

                string = String(msgArray[startIdx...endIdx])
            }
        }

        return string
    }

    while endIdx < msgArray.count {

        let changedString = changeString()
        result.append(dictionary[changedString]!)

        if endIdx == msgArray.count-1 {
            break
        }

        let next = String(msgArray[endIdx+1])
        dicIdx += 1
        dictionary[changedString+next] = dicIdx

        endIdx += 1
        startIdx = endIdx
        
    }

    return result
}

- ๋‹ค๋ฅธ ํ’€์ด๋„ ์ฐพ์•„๋ด์•ผ๊ฒ ์–ด์š” !!

 

 

'Private > TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

20230402 TIL  (0) 2023.04.02
20230401 TIL  (0) 2023.04.02
20230331 TIL  (1) 2023.04.01
20230329 TIL  (2) 2023.03.30
20230328 TIL  (6) 2023.03.29
Comments