PS/프로그래머스

[Python] 추억 점수

s_omi 2024. 10. 26. 10:25
728x90
반응형
SMALL

✏️ 문제

 

문제 파악

name 배열의 값이 photo 배열에 있으면 

name의 이름에 해당하는 yearning 배열의 값을 더하면 되므로 .index() 메소드를 알면 쉽게 풀 수 있다! 

 

 

코드

def solution(name, yearning, photo):
    answer = []
    
    for i in range(len(photo)):
        temp = 0
        for j in range(len(photo[i])):
            for n in name:
                if n == photo[i][j]:
                    temp += yearning[name.index(n)]
        answer.append(temp)
    return answer
  • 다른 사람이 딕셔너리 사용해서 푼 코드 
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

 

728x90
반응형
LIST