๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ Computer Science/Programming

[๐Ÿ’ป Programming] Clean Code - ๊น”๋”ํ•œ ์ฃผ์„๊ณผ ํ˜•์‹์€ ์–ด๋–ป๊ฒŒ ์จ์•ผํ• ๊นŒ?

by Fomagran ๐Ÿ’ป 2021. 8. 7.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma๐Ÿ‘Ÿ ์ž…๋‹ˆ๋‹ค!

 

ํ‰์†Œ์— ์ฝ”๋“œ๋ฅผ ๊ฐ€๋…์„ฑ ์žˆ๊ฒŒ ์ž‘์„ฑํ•  ๋•Œ ํ˜•์‹์„ ์–ด๋–ป๊ฒŒ ๋งž์ถฐ์•ผ ๋ ๊นŒ? ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ๊ณผ

 

๋˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋ณผ ๋•Œ ์• ๋งคํ•œ ์ฝ”๋“œ๋ฅผ ์ ์—ˆ์„ ๋•Œ ์ฃผ์„์œผ๋กœ ์–ด๋–ป๊ฒŒ ์„ค๋ช…ํ•ด์•ผ ํ• ๊นŒ? ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์„ ํ–ˆ์—ˆ๋Š”๋ฐ์š”.

 

ํด๋ฆฐ ์ฝ”๋“œ์—์„œ ์ข‹์€ ์ฃผ์„๊ณผ ๋‚˜์œ ์ฃผ์„, ๊ฐ€๋…์„ฑ ์žˆ๊ฒŒ ํ˜•์‹์„ ๋งž์ถ”๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ๊ฒŒ ๋˜์–ด ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

(์ œ๊ฐ€ ํ‰์†Œ์— ๋งŽ์ด ์“ฐ๋˜ ๋‚˜์œ ๋ฒ„๋ฆ‡์ด๋‚˜ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋“œ๋Š” ๊ฒƒ ์œ„์ฃผ๋กœ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.)

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


์ฃผ์„

 

์ด ์ฑ…์˜ ์ €์ž์ธ ๋ฐฅ ์•„์ €์”จ๋Š” ์ฃผ์„์— ๋Œ€ํ•ด์„œ ์ด๋ ‡๊ฒŒ ๋งํ•ฉ๋‹ˆ๋‹ค.

 

์ฃผ์„์€ ๊ธฐ๊ปํ•ด์•ผ  ํ•„์š”์•…์ด๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ์ž์ฒด๋ฅผ ์น˜๋ฐ€ํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด ์˜๋„๋ฅผ ํ‘œํ˜„ํ•  ๋Šฅ๋ ฅ์ด ์žˆ๋‹ค๋ฉด ์ฃผ์„์€ ๊ฑฐ์˜ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ฃผ์„์„ ๋ฌด์‹œํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

 

๋ฐ”๋กœ ์ฃผ์„์€ ์˜ค๋ž˜๋˜๋ฉด ์˜ค๋ž˜๋ ์ˆ˜๋ก ๊ทธ๋ฆ‡๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๊ฐ€ ๋ณ€ํ™”ํ•˜๊ณ  ์ง„ํ™”ํ• ์ˆ˜๋ก ์กฐ๊ฐ์ด ๋‚˜๋‰˜์–ด์ง€๊ณ  ํ•ฉ์ณ์ง€๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์ฃผ์„๊นŒ์ง€ ์œ ์ง€ํ•˜๊ณ  ๋ณด์ˆ˜ํ•˜๊ธฐ๋Š” ํ˜„์‹ค์ ์œผ๋กœ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๊ทธ๋ฆ‡๋œ ์ฃผ์„์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒŒ ๊ฑฐ์ง“๋ง์„ ํ•˜๊ณ  ์ž˜๋ชป๋œ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ

 

์ด๊ฒƒ์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์‹œ๊ฐ„๊ณผ ์—๋„ˆ์ง€๋ฅผ ๋บ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ๋•Œ์— ๋”ฐ๋ผ ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ์œ ์ตํ•œ ์ฃผ์„์ด ์žˆ๊ณ  ์ •๋ง๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ๋‚˜์œ ์ฃผ์„์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ง€๊ธˆ๋ถ€ํ„ฐ๋Š” ์ข‹์€ ์ฃผ์„๊ณผ ๋‚˜์œ ์ฃผ์„์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


์ข‹์€ ์ฃผ์„ ๐Ÿ‘๐Ÿป

 

1. ๋ฒ•์ ์ธ ์ฃผ์„

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ €์ž‘๊ถŒ ์ •๋ณด๋‚˜ ์†Œ์œ ๊ถŒ ์ •๋ณด๋Š” ์†Œ์Šค ํŒŒ์ผ ์ฒซ ๋จธ๋ฆฌ์— ๋ช…์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

// Copyright (C) 2003, 2004, 2005 by Object Montor, Inc. All right reserved.
// GNU General Public License

 

2. ์ •๋ณด๋‚˜ ์˜๋„๋ฅผ ์„ค๋ช…ํ•˜๋Š” ์ฃผ์„

 

์ถ”์ƒ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’์ด๋‚˜ ์ •๊ทœํ‘œํ˜„์‹์ด ์–ด๋–ค ๊ฒƒ์„ ๋œปํ•˜๋Š”์ง€์˜ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜,

 

ํ•ด๋‹น ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ’์ด๋‚˜ ์ค‘์š”์„ฑ์„ ๊ฐ•์กฐํ•˜๋Š” ์ฃผ์„์ด๋‚˜,

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์–ด๋–ค ๊ฒƒ์„ ์˜๋„ํ–ˆ๋Š”์ง€๋ฅผ ๋ฏธ๋ฆฌ ๋ช…์‹œํ•ด๋†“์œผ๋ฉด ํƒ€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์‹œ๊ฐ„์„ ์•„๊ปด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

3. TODO ์ฃผ์„

 

์•„์ง ์ž‘์„ฑํ•˜์ง€ ๋ชปํ•œ ์ฝ”๋“œ๋‚˜ ์•„์ง ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ•œ ์ฝ”๋“œ๋ฅผ ๋ฏธ๋ž˜์— ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค๋ฉด TODO ์ฃผ์„์œผ๋กœ ์•ž์œผ๋กœ ํ•  ์ผ์„

 

์„ค๋ช…ํ•ด๋†“์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋‚˜์œ ์ฃผ์„ โŒ

 

1. ๊ฐ™์€ ์ด์•ผ๊ธฐ๋ฅผ ํ•˜๋Š” ์ฃผ์„

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•ด์ฃผ๋Š” ์ฃผ์„ ๋Œ€์‹  ์ฝ”๋“œ๋กœ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

(์ €๋„ ํ‰์†Œ์— ์ด๋Ÿฐ์‹์œผ๋กœ ๋งŽ์ด ๋‹ฌ์•„์„œ ์กฐ๊ธˆ ์ฐ”๋ฆฌ๋„ค์š”..)

 

//์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜
var name = "Fomagran"

 

2. ๊ธฐ๋กํ•˜๋Š” ์ฃผ์„

 

๋•Œ๋•Œ๋กœ ์‚ฌ๋žŒ๋“ค์€ ์–ด๋–ค ์‚ฌ๋žŒ์ด ์–ธ์ œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๋Š”์ง€๋ฅผ ์ ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋Ÿฐ ๊ฒƒ๋“ค์€ ์†Œ์Šค ์ฝ”๋“œ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์—๊ฒŒ ๋งก๊ฒจ์•ผํ•ฉ๋‹ˆ๋‹ค.

 

/*
 *11-Oct-2021 ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ์ •๋ฆฌํ•˜๊ณ  ์ƒˆ๋กœ์šด ํŒจํ‚ค์ง€์ธ
 com.jrefinery.date๋กœ ์˜ฎ๊ฒผ๋‹ค.
 *05-Nov-2021 getDescription() ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฉฐ
 NotableDate class๋ฅผ ์ œ๊ฑฐํ–ˆ๋‹ค.
 */

 

3. ๋‹ซ๋Š” ๊ด„ํ˜ธ์— ์“ฐ๋Š” ์ฃผ์„

 

๋งŽ์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ซ๋Š” ๊ด„ํ˜ธ์— ํŠน์ˆ˜ํ•œ ์ฃผ์„์„ ๋‹ฌ์•„๋†“๊ณค ํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ซ๋Š” ์ฃผ์„์— ๊ด„ํ˜ธ๋ฅผ ๋‹ค๋Š” ๊ฒƒ ๋Œ€์‹  ์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์ค„์ด๋ ค๋Š” ์‹œ๋„๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

func add(n1:Int,n2:Int,infos:[Int]) {
    for info in infos {
        if  n1 <  n2{
            while infos.isEmpty {
                
            }//while
        }//if
    }//for
}

ํ˜•์‹

 

์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•˜๊ณ , ์ผ๊ด€์ ์ด๊ณ , ๊ผผ๊ผผํ•˜๊ฒŒ ์ „๋ฌธ๊ฐ€๊ฐ€ ์งฐ๋‹ค๋Š” ์ธ์ƒ์„ ์ฃผ๊ธฐ ์œ„ํ•ด์„  

 

์ฝ”๋“œ ํ˜•์‹์„ ๋งž์ถ”๊ธฐ ์œ„ํ•œ ๊ทœ์น™์„ ์ •ํ•˜๊ณ  ๊ทธ ๊ทœ์น™์„ ์ฐฉ์‹คํžˆ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ํƒ„ํƒ„ํžˆ ์žก์•„๋†“์€ ๊ตฌํ˜„ ์Šคํƒ€์ผ๊ณผ ๊ฐ€๋…์„ฑ ์ˆ˜์ค€์€ ์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด์„ฑ๊ณผ ํ™•์žฅ์„ฑ์— ๊ณ„์† ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ฒŒ ๋˜๊ณ  

 

์ด๊ฒƒ์€ ์ฝ”๋“œ์˜ ํ’ˆ์งˆ๊ณผ ์ƒ์‚ฐ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

 

์ ์ ˆํ•œ ํ–‰ ๊ธธ์ด๋ฅผ ์œ ์ง€ํ•˜๋ผ

 

์œ„๋Š” 7๊ฐœ ํ”„๋กœ์ ํŠธ์— ๋Œ€ํ•œ ํŒŒ์ผ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ทธ๋ฆผ์ธ๋ฐ 

 

ํ‰๊ท  ํŒŒ์ผ ๊ธธ์ด๋Š” ์•ฝ 65์ค„์ด๋ฉฐ ๋Œ€๋žต 1/3์ด 40์ค„์—์„œ 100์ค„์„ ๋„˜๋Š” ์ •๋„์ด๋‹ค.

 

์ด๊ฒƒ์€ 500์ค„์„ ๋„˜์ง€ ์•Š๊ณ  ๋Œ€๋ถ€๋ถ„ 200์ค„ ์ •๋„์ธ ํŒŒ์ผ๋กœ๋„ ์ปค๋‹ค๋ž€ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

 

๊ฐœ๋…์€ ๋นˆ ํ–‰์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋ผ

 

ํŒจํ‚ค์ง€ ์„ ์–ธ๋ถ€,import๋ฌธ,๊ฐ ํ•จ์ˆ˜ ์‚ฌ์ด์— ๋นˆ ํ–‰์„ ์ถ”๊ฐ€ํ•ด์„œ ์ƒˆ๋กœ์šด ๊ฐœ๋…์ด ์‹œ์ž‘ํ•œ๋‹ค๋Š” ๋‹จ์„œ๋ฅผ ๋‚จ๊ธฐ๋„๋ก ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

๋‚˜์œ ์˜ˆ โŒ

 

import Foundation
func getManhattanDistance(p1:Seat,p2:Seat) -> Int {
    return abs(p1.0-p2.0) + abs(p1.1-p2.1)
}
func isKeepDistance(p1:Seat,p2:Seat,xMap:inout [[String]]) -> Bool {
    let minX = p1.0 < p2.0 ? p1 : p2
    let minY = p1.1 < p2.1 ? p1 : p2
    if p1.0 == p2.0 {
        return xMap[minY.1+1][p1.0] == "X"
    }else if p1.1 == p2.1 {
        return xMap[p1.1][minX.0+1] == "X"
    }else{
        if p1.0 < p2.0 {
            return xMap[p1.1][p1.0+1] == "X" && xMap[p2.1][p2.0-1] == "X"
        }else {
            return xMap[p2.1][p2.0+1] == "X" && xMap[p1.1][p1.0-1] == "X"
        }
    }
}

 

์ข‹์€ ์˜ˆ ๐Ÿ‘๐Ÿป

 

import Foundation

func getManhattanDistance(p1:Seat,p2:Seat) -> Int {
    return abs(p1.0-p2.0) + abs(p1.1-p2.1)
}

func isKeepDistance(p1:Seat,p2:Seat,xMap:inout [[String]]) -> Bool {
    let minX = p1.0 < p2.0 ? p1 : p2
    let minY = p1.1 < p2.1 ? p1 : p2
    if p1.0 == p2.0 {
        return xMap[minY.1+1][p1.0] == "X"
    }else if p1.1 == p2.1 {
        return xMap[p1.1][minX.0+1] == "X"
    }else{
        if p1.0 < p2.0 {
            return xMap[p1.1][p1.0+1] == "X" && xMap[p2.1][p2.0-1] == "X"
        }else {
            return xMap[p2.1][p2.0+1] == "X" && xMap[p1.1][p1.0-1] == "X"
        }
    }
}

 

์ข…์†์ ์ด๊ฑฐ๋‚˜ ์œ ์‚ฌํ•œ ์ฝ”๋“œ๋Š” ๊ฐ€๊นŒ์ด ๋‘์–ด๋ผ

 

์ค„๋ฐ”๊ฟˆ์ด ๊ฐœ๋…์„ ๋ถ„๋ฆฌํ•œ๋‹ค๋ฉด ์„ธ๋กœ ๋ฐ€์ง‘๋„๋Š” ์—ฐ๊ด€์„ฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, ์„œ๋กœ ๋ฐ€์ ‘ํ•œ ์ฝ”๋“œ ํ–‰์€ ์„ธ๋กœ๋กœ ๊ฐ€๊นŒ์ด ๋†“์—ฌ์•ผ ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

 

ํ•œ ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•œ๋‹ค๋ฉด ๋‘ ํ•จ์ˆ˜๋Š” ์„ธ๋กœ๋กœ ๊ฐ€๊นŒ์ด ๋ฐฐ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋˜ํ•œ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๋ณด๋‹ค ๋จผ์ € ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ผ๊ด€์ ์œผ๋กœ ์ ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ์€ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜๊ฐ€ ์ž ์‹œ ํ›„์— ์ •์˜๋˜๋ฆฌ๋ผ๊ณ  ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

func solution(_ places:[[String]]) -> [Int] {
    var answer:[Int] = []
    for place in places {
        ...
        getSeatLocation(places: place,xMap: &xMap,applicants: &applicants)
        outer:while !applicants.isEmpty {
           
            for applicant in applicants {
                if getManhattanDistance(p1: first, p2: applicant) <= 2 {
                    if !isKeepDistance(p1: first, p2: applicant,xMap: &xMap) {
                        ...
                    }
                }
            }
        }
        ...
    }
    return answer
}

func getSeatLocation(places:[String],xMap:inout [[String]],applicants:inout [Seat]) {
    ...
}

func getManhattanDistance(p1:Seat,p2:Seat) -> Int {
    ...
}

func isKeepDistance(p1:Seat,p2:Seat,xMap:inout [[String]]) -> Bool {
    ...
}

 

์ข…์†์ ์ธ ๊ด€๊ณ„๊ฐ€ ์—†๋”๋ผ๋„ ๋ช…๋ช…๋ฒ•์ด ๊ฐ™๊ฑฐ๋‚˜ ๊ธฐ๋ณธ๊ธฐ๋Šฅ์ด ์œ ์‚ฌํ•œ ํ•จ์ˆ˜๋Š” ์„ธ๋กœ๋กœ ๊ฐ€๊นŒ์ด ๋ฐฐ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

class Assert {
    static func assertTrue(message:String,condition:Bool) {
        
    }
    
    static func assertTrue(condition:Bool) {
        
    }
    
    static func assertFalse(message:String,condition:Bool) {
        
    }
    
    static func assertFalse(condition:Bool) {
        
    }
}

 

ํ–‰ ๊ธธ์ด

 

ํ•œ ํ–‰์€ ๊ฐ€๋กœ๋กœ ์–ผ๋งˆ๋‚˜ ์ ๋‹นํ•ด์•ผ ํ• ๊นŒ ๋˜ํ•œ ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ์—ˆ๋˜ ๊ฒƒ ์ค‘ ํ•˜๋‚˜์ธ๋ฐ์š”.

 

 

์œ„ ๊ทธ๋ž˜ํ”„๋Š” ํฐ 7๊ฐœ ํ”„๋กœ์ ํŠธ์˜ ํ–‰ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

20~60์ž๊ฐ€ 40%์ด๊ณ  10์ž ๋ฏธ๋งŒ์€ 30%์ด๋ฉฐ 80์ž ์ดํ›„๋ถ€ํ„ฐ๋Š” ๊ธ‰๊ฒฉํžˆ ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค.

 

์ €์ž์ธ ๋ฐฅ ์•„์ €์”จ๋Š” 100์ž์—์„œ 120์ž์— ๋‹ฌํ•ด๋„ ๋‚˜์˜์ง€ ์•Š์œผ๋ฉฐ ์ž์‹ ์€ 120์ž๋กœ ์ œํ•œํ•œ๋‹ค๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค.

 

๋“ค์—ฌ์“ฐ๊ธฐ

 

์†Œ์Šค ํŒŒ์ผ์—” ํด๋ž˜์Šค,๋ฉ”์„œ๋“œ,๋ธ”๋ก ๋‚ด์— ์ ์šฉ๋˜๋Š” ์ •๋ณด๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋ฒ”์œ„๋กœ ์ด๋ฃจ์–ด์ง„ ๊ณ„์ธต์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋“ค์—ฌ์“ฐ๊ธฐ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์š”.

 

ํด๋ž˜์Šค ์ •์˜์ฒ˜๋Ÿผ ํŒŒ์ผ ์ˆ˜์ค€์ธ ๋ฌธ์žฅ์€ ๋“ค์—ฌ์“ฐ์ง€ ์•Š๊ณ  ํด๋ž˜์Šค ๋‚ด ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค๋ณด๋‹ค ํ•œ ์ˆ˜์ค€ ๋“ค์—ฌ์”๋‹ˆ๋‹ค.

 

๋ฉ”์„œ๋“œ ์ฝ”๋“œ๋Š” ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ณด๋‹ค ํ•œ ์ˆ˜์ค€ ๋“ค์—ฌ์“ฐ๊ณ  ๋ธ”๋ก ์ฝ”๋“œ๋Š” ๋ธ”๋ก์„ ํฌํ•จํ•˜๋Š” ์ฝ”๋“œ๋ณด๋‹ค ํ•œ ์ˆ˜์ค€ ๋“ค์—ฌ์”๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ์—†๋‹ค๋ฉด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

func solution(_ places:[[String]]) -> [Int] { var answer:[Int] = [] for place in
places { var check:Bool = false var applicants:[Seat] = [] var xMap:[[String]] = [] 
getSeatLocation(places: place,xMap: &xMap,applicants: &applicants) outer:while 
!applicants.isEmpty { let first = applicants.removeFirst() for applicant in applicants 
{ if getManhattanDistance(p1: first, p2: applicant) <= 2 { if !isKeepDistance(p1: first,
p2: applicant,xMap: &xMap) { answer.append(0) check = true break outer } } } } if !check
{ answer.append(1) } } return answer } func getSeatLocation(places:[String],
xMap:inout [[String]],applicants:inout [Seat]) { for (y,place) in places.enumerated() {
xMap.append(place.map{String($0)}) for (x,p) in place.map({String($0)}).enumerated() {
if p == "P" { applicants.append((x,y)) } } } } func getManhattanDistance(p1:Seat,p2:Seat
) -> Int { return abs(p1.0-p2.0) + abs(p1.1-p2.1) } func isKeepDistance(p1:Seat,p2:Seat,
xMap:inout [[String]]) -> Bool { let minX = p1.0 < p2.0 ? p1 : p2 let minY = p1.1 < p2.1
? p1 : p2 if p1.0 == p2.0 { return xMap[minY.1+1][p1.0] == "X" }else if p1.1 == p2.1 {
return xMap[p1.1][minX.0+1] == "X" }else { if p1.0 < p2.0 { return xMap[p1.1][p1.0+1] ==
"X" && xMap[p2.1][p2.0-1] == "X" }else { return xMap[p2.1][p2.0+1] == "X" && xMap[p1.1][
p1.0-1] == "X" } } }

 

๊ฐ€๋” ๊ฐ„๋‹จํ•œ if๋ฌธ,while๋ฌธ ๋“ฑ ์งง์€ ํ•จ์ˆ˜์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์‹ถ์€ ์š•๊ตฌ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. (์ € ๋˜ํ•œ ๋งŽ์ด ๊ทธ๋žฌ์Œ..)

 

ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ์‚ฌ์†Œํ•œ ๊ฒƒ๋„ ๋“ค์—ฌ์“ฐ๊ธฐ ๋ฒ”์œ„๋กœ ์ œ๋Œ€๋กœ ํ‘œํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋‚˜์œ ์˜ˆ โŒ

for i in 0...5 {
    if i == 0 { continue }
}

 

์ข‹์€ ์˜ˆ ๐Ÿ‘๐Ÿป

 

for i in 0...5 {
    if i == 0 {
        continue
    }
}
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€