코딩테스트
프로그래머스 코딩테스트> 연습문제>추억 점수
DTV
2024. 6. 21. 12:21
https://school.programmers.co.kr/learn/courses/30/lessons/176963
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
name리스트는 이차원 리스트다 name[i]가 ["may", "kein", "kain"]이고 그리움 점수 yearning가 [5점, 10점, 1점]일 때 해당 사진의 추억 점수는 16(5 + 10 + 1)점이 됩니다. 전체 name 리스트의 점수를 photo에 주어진 순서대로 배열에 담아 return하는 solution 함수를 완성해주세요.
index를 찾으려는 값이 없을경우 ValueError가 나기 대문에 try: 를 이용해봤다.
def solution(name, yearning, photo):
answer = []
for arr in photo:
point = 0
for arr_name in arr:
try:
idx = name.index(arr_name)
point += yearning[idx]
except ValueError:
point = point
answer.append(point)
return answer
아래는 다른분 풀이이다. dictionary를 미리 만들고 검색을 해서 점수를 더하는 방식이다.
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