코딩테스트/PYTHON

[프로그래머스][PYTHON] Lv. 1 추억 점수

_알파카 2024. 4. 12. 20:05
728x90

문제 설명

https://school.programmers.co.kr/learn/courses/30/lessons/176963

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내 풀이

def solution(name, yearning, photo):
    # photo의 길이만큼 기본 answer 배열 생성
    answer = [0] * len(photo)

    # 인물과 추억 점수로 딕셔너리 생성
    score_dic = dict(zip(name, yearning))
    
    for p in range(len(photo)):
        # 각 사진 중 정보가 있는 인물에 대해 추억점수를 더함
        for i in photo[p]:
            if i in name:
                answer[p] += score_dic[i]
    return answer

 

먼저, photo의 길이만큼의 answer 배열을 생성한다. 

인물(name)과 추억점수(yearning)을 바탕으로 딕셔너리를 생성하고, 

 

photo 배열을 순회하며, 각 사진 중 정보가 있는 인물에 대해 추억점수를 더하여

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

 

내가 한 풀이와 비슷하지만 약간 다른 풀이이다. 

 

def solution(name, yearning, photo):
    answer = []

    for i in photo:
        score=0
        for j in range(len(name)):
            if name[j] in i:
                score+=yearning[j]
        answer.append(score)
    return answer

 

이건 딕셔너리를 생성하지 않고 푼 풀이이다. 

728x90