추억 점수

Kylie·2023년 4월 14일

프로그래머스 Lv.1

목록 보기
64/69

문제
그리워하는 사람의 이름을 담은 문자열 배열 name, 각 사람별 그리움 점수를 담은 정수 배열 yearning, 각 사진에 찍힌 인물의 이름을 담은 이차원 문자열 배열 photo가 매개변수로 주어질 때, 사진들의 추억 점수를 photo에 주어진 순서대로 배열에 담아 return

내 코드

def solution(name, yearning, photo):
    answer = []
    #이름과 점수 dict 매칭
    D = []
    for i in range(len(name)):
        D.append([name[i],yearning[i]])
    Dict = dict(D)
    #photo에 있는 key 값 유무에 따라 value sum 저장 t
    for i in photo:
        t = 0
        for key, value in Dict.items():
            if key in i:
                t += value 
    #새로운 리스트 생성 answer
        answer.append(t)
    return answer

+1

다른 풀이

def solution(name, yearning, photo):
    dictionary = dict(zip(name,yearning))
    scores = []
    for pt in photo:
        score = 0
        for p in pt:
            if p in dictionary:
                score += dictionary[p]
        scores.append(score)
    return scores

dict 와 zip 을 깜빡했다...

profile
딥린이

0개의 댓글