๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] 2019 KAKAO BLIND RECRUITMENT ๋งค์นญ ์ ์ˆ˜

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

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋งค์นญ ์ ์ˆ˜

๋งค์นญ ์ ์ˆ˜ ํ”„๋ Œ์ฆˆ ๋Œ€ํ•™๊ต ์กฐ๊ต์˜€๋˜ ์ œ์ด์ง€๋Š” ํ—ˆ๋“œ๋ ›์ผ๋งŒ ์‹œํ‚ค๋Š” ๋„ค์˜ค ํ•™๊ณผ์žฅ๋‹˜์˜ ๋งˆ์ˆ˜์—์„œ ๋ฒ—์–ด๋‚˜, ์นด์นด์˜ค์— ์ž…์‚ฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ํ‰์†Œ์— ๊ด€์‹ฌ์žˆ์–ดํ•˜๋˜ ๊ฒ€์ƒ‰์— ๋งˆ์นจ ๊ฒฐ์›์ด ๋ฐœ์ƒํ•˜์—ฌ, ๊ฒ€์ƒ‰๊ฐœ๋ฐœํŒ€

programmers.co.kr


Solution

 

1. ํŽ˜์ด์ง€์— ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

๋ช‡๋ฒˆ์งธ ์ธ๋ฑ์Šค์ธ์ง€,์ž์‹ ์˜ url,์ด ์ ์ˆ˜,์™ธ๋ถ€ ๋งํฌ๋ฅผ ํ”„๋กœํผํ‹ฐ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

struct Page {
    var index:Int, url:String,score:Double,externalLinks:[String]
}

 

2. "<" ๊ธฐ์ค€์œผ๋กœ page์˜ ๋ฌธ์ž์—ด์„ ์ž๋ฅธ๋‹ค.

 

let tags = page.split(separator: "<")

 

3. ๋‚˜๋ˆ ์ง„ ํƒœ๊ทธ๋ณ„๋กœ ์ •๋ณด๋ฅผ ์•Œ์•„๋‚ธ๋‹ค.

 

๋งŒ์•ฝ ์•ž ๊ธ€์ž๊ฐ€ meta p(meta property)์ผ ๊ฒฝ์šฐ ์ž์‹ ์˜ url์„ ๋œปํ•˜๋ฏ€๋กœ url์„ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค. (์†Œ์Šค ์ฝ”๋“œ์—์„œ getWebpageURL๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!)

 

/body์ผ ๊ฒฝ์šฐ body๊ฐ€ ๋๋‚œ ๊ฒƒ์ด๋ฏ€๋กœ ์ ์ˆ˜๋ฅผ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

a href์ผ ๊ฒฝ์šฐ ์™ธ๋ถ€ ๋งํฌ๋ฅผ ๋œปํ•˜๋ฏ€๋กœ ์™ธ๋ถ€๋งํฌ๋ฅผ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

body์ผ ๊ฒฝ์šฐ score์— +1์„ ํ•ด์ฃผ์–ด 0์œผ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

๊ทธ ์™ธ ์ผ ๊ฒฝ์šฐ์— body์•ˆ์— word๋ฅผ ์ฐพ์•„๋‚ด์–ด ์ ์ˆ˜์— ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. (์†Œ์Šค ์ฝ”๋“œ์—์„œ findWordInBody๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!)

 

var pageInfos:[Page] = []
    for (i,page) in pages.enumerated() {
        var pageInfo = Page(index:i,url: "", score: 0, externalLinks: [])
        let tags = page.split(separator: "<")
        var score:Double = -1
        
        for tag in tags {
            if String(tag.prefix(6)) == "meta p" {
                pageInfo.url = getWebpageURL(tag: String(tag))
            }
            if String(tag.prefix(5)) == "/body" {
                pageInfo.score = score
                break
            }
            if String(tag.prefix(6)) == "a href" {
                let url = tag.split(separator: "\"")[1]
                pageInfo.externalLinks.append(String(url))
            }
            if String(tag.prefix(4)) == "body" {
                score += 1
            }
            if score > -1 {
                findWordInBody(tag: String(tag), word: word.lowercased(), score: &score)
            }
        }
        pageInfos.append(pageInfo)
    }

 

3. ๋งค์นญ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„๋‚ธ๋‹ค.

 

func getMaxMatchingScoreIndex(pageInfos:inout [Page]) -> Int {
    for pageInfo in pageInfos {
        for link in pageInfo.externalLinks {
            if let index = pageInfos.firstIndex(where: {$0.url == link}) {
                pageInfos[index].score += pageInfo.score/Double(pageInfo.externalLinks.count)
            }
        }
    }
    return pageInfos.sorted{($0.score) > ($1.score)}.first?.index ?? 0
}

 

4. ๋งค์นญ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

return getMaxMatchingScoreIndex(pageInfos: &pageInfos)

Source Code

 


P.S

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 8๋ฒˆ๋งŒ ํ‹€๋ ค์„œ ์ฐพ์•„๋ดค๋”๋‹ˆ ์ด ์ ์ˆ˜๋ฅผ Double๋กœ ํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค...

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€