728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/42893?language=python3
코딩테스트 연습 - 매칭 점수
매칭 점수 프렌즈 대학교 조교였던 제이지는 허드렛일만 시키는 네오 학과장님의 마수에서 벗어나, 카카오에 입사하게 되었다. 평소에 관심있어하던 검색에 마침 결원이 발생하여, 검색개발팀
programmers.co.kr
최종 코드
GitHub - hwayeon351/Programmers-Algorithms: 프로그래머스 알고리즘 소스 코드 모음
프로그래머스 알고리즘 소스 코드 모음. Contribute to hwayeon351/Programmers-Algorithms development by creating an account on GitHub.
github.com
from collections import defaultdict
def solution(word, pages):
#[url, link, 기본점수, idx, 매칭 점수]
web = []
word = word.lower()
link_dic = dict()
connect_dic = defaultdict(list)
for i in range(len(pages)):
html = pages[i].split('\n')
#head 태그 처리
#url 구하기
head = html[html.index('<head>')+1:html.index('</head> ')]
for meta in head:
if '<meta property="og:url" content="' in meta:
url = meta[meta.index('https://'):meta.index('"/>')]
break
#body 태그 처리
#외부 링크와 기본 점수 구하기
body = html[html.index('<body>')+1:html.index('</body>')]
link = []
basic_score = 0
for line in body:
line = line.lower()
w = ''
while '<a href="' in line:
text = line[:line.index('<a href="')]
# text에 word 있는지 탐색
for c in text:
if c.isalpha(): w += c
else:
if w == word:
basic_score += 1
w = ''
if w == word:
basic_score += 1
w = ''
#외부 링크 구하고, 남은 text 부분을 line에 담기
line = line[line.index('<a href="')+9:]
new_link = line[:line.index('"')]
line = line[line.index('"'):]
link.append(new_link)
connect_dic[new_link].append(url)
#line에 담긴 텍스트에서 word 단어 개수 구하기
for c in line:
if c.isalpha(): w += c
else:
if w == word: basic_score += 1
w = ''
if w == word: basic_score += 1
link_dic[url] = i
web.append([url, link, basic_score, i])
for i in range(len(web)):
link_score = 0
for link in connect_dic[web[i][0]]:
if link in link_dic:
link_score += (web[link_dic[link]][2] / len(web[link_dic[link]][1]))
web[i].append(link_score + web[i][2])
web.sort(key = lambda x:(-x[4], x[3]))
return web[0][3]
풀이 과정
문자열을 적절히 파싱해서 url과 외부 링크를 찾고, 해당 html 파일에서 출력되는 text에서 word 단어를 몇개 포함시키는지를 찾아줘야 하는 문제이다.
어렵지는 않았지만 굉장히 정확하게 구현해야 하기에 까다로웠다.
from collections import defaultdict
def solution(word, pages):
#[url, link, 기본점수, idx, 매칭 점수]
web = []
word = word.lower()
link_dic = dict()
connect_dic = defaultdict(list)
for i in range(len(pages)):
html = pages[i].split('\n')
#head 태그 처리
#url 구하기
head = html[html.index('<head>')+1:html.index('</head> ')]
for meta in head:
if '<meta property="og:url" content="' in meta:
url = meta[meta.index('https://'):meta.index('"/>')]
break
#body 태그 처리
#외부 링크와 기본 점수 구하기
body = html[html.index('<body>')+1:html.index('</body>')]
link = []
basic_score = 0
for line in body:
line = line.lower()
w = ''
while '<a href="' in line:
text = line[:line.index('<a href="')]
# text에 word 있는지 탐색
for c in text:
if c.isalpha(): w += c
else:
if w == word:
basic_score += 1
w = ''
if w == word:
basic_score += 1
w = ''
#외부 링크 구하고, 남은 text 부분을 line에 담기
line = line[line.index('<a href="')+9:]
new_link = line[:line.index('"')]
line = line[line.index('"'):]
link.append(new_link)
connect_dic[new_link].append(url)
#line에 담긴 텍스트에서 word 단어 개수 구하기
for c in line:
if c.isalpha(): w += c
else:
if w == word: basic_score += 1
w = ''
if w == word: basic_score += 1
link_dic[url] = i
web.append([url, link, basic_score, i])
for i in range(len(web)):
link_score = 0
for link in connect_dic[web[i][0]]:
if link in link_dic:
link_score += (web[link_dic[link]][2] / len(web[link_dic[link]][1]))
web[i].append(link_score + web[i][2])
web.sort(key = lambda x:(-x[4], x[3]))
return web[0][3]
참고
- 4. 무지의 먹방 라이브 -> 2021.09.02 - [코테 노트/프로그래머스] - Level 4 무지의 먹방 라이브 <2019 KAKAO BLIND RECRUITMENT> Python 3
728x90
반응형
'코테 노트 > 프로그래머스' 카테고리의 다른 글
Level 3 외벽 점검 <2020 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2022.04.07 |
---|---|
Level 3 기둥과 보 설치 <2020 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2022.04.02 |
Level 2 [3차] n진수 게임 <2018 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2022.03.30 |
Level 2 [3차] 파일명 정렬 <2018 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2022.03.29 |
Level 2 [3차] 압축 <2018 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2022.03.28 |