Development

[프로그래머스] 위클리 챌린지 4주차 직업군 추천하기 본문

코딩테스트/Python

[프로그래머스] 위클리 챌린지 4주차 직업군 추천하기

yo~og 2021. 9. 6. 00:53
반응형

내 코드

def solution(table, languages, preference):
    score = []

    for tbl in table:
        s=0
        a = tbl.split()
        for lng,p in zip(languages,preference):
            if lng in a:
                s += (len(a) - a.index(lng))*p
        score.append([a[0],s])
    
    return sorted(score,key=lambda x:(-x[1],x[0]))[0][0]

위클리 챌린지 ~~~!! table에 있는 문자열을 나눠 a에 담는다. zip으로 언어와 선호도를 가져오면서 언어가 a에 있으면 순위를 계산하고 선호도를 곱해 합을 구한다. 이것을 table 길이만큼 반복한다.

계산한 점수들은 직업군들과 score에 담기고 정렬을 해서 답을 구한다.

 

다른사람 코드

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]

ㅎㅎ 내꺼랑 비슷한듯? 근데 중간에 점수를 구하는 식은 내가 더 간단한거같다! 뿌듯뿌듯

반응형
Comments