1. 문제
https://programmers.co.kr/learn/courses/30/lessons/84325
2. 풀이
2.1. 나의 풀이
def solution(table, languages, preference):
jobs = []
dict = {}
for job in table:
job = job.split()
jobs.append(job[0])
dict[job[0]] = {}
for i in range(5, 0, -1):
dict[job[0]][job[6 - i]] = i
jobs_score = [0] * len(jobs)
for i, job in enumerate(jobs):
for lan, pre in zip(languages, preference):
jobs_score[i] = jobs_score[i] + (dict[job].get(lan, 0) * pre)
max_score = max(jobs_score)
anslist = [(-score, job) for job, score in zip(jobs, jobs_score) if score == max_score]
return sorted(anslist)[0][1]
2.2. 숏코딩
아래와 같이 table 을 한 번 돌면서 문자열이 있는지 찾은 후, 점수를 갱신할 수 있습니다.
# JinYs 님의 소스코드
def solution(table, languages, preference):
score = {}
for t in table:
for lang, pref in zip(languages, preference):
if lang in t.split():
score[t.split()[0]] = score.get(t.split()[0], 0) + (6 - t.split().index(lang)) * pref
return sorted(score.items(), key = lambda item: [-item[1], item[0]])[0][0]