👩‍💻 코테 공부/코테 공부

[코테 - py] 대충 만든 자판

수댕ʕت̫͡ʔ 2024. 8. 11. 18:01

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

 

프로그래머스

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

programmers.co.kr

오늘은 해시가 주요 포인트였던 것같다. 
어렵지 않은 문제이지만, 알아가는게 많은 문제였다. 내가 특정한 부분 때문에 비효율적이라고 생각하는 코드가 있었는데 여기서 이걸 고칠 수 있었다.

def solution(keymap, targets):
    dic = {}
    
    for i in range(len(keymap)):
        temp = keymap[i]
        for j in range(len(temp)):
            if temp[j] not in dic:
                dic[temp[j]] = j+1
            elif temp[j] in dic:
                dic[temp[j]] = min(dic[temp[j]], j+1) # 최솟값 비교해서 넣어주기
                
    answer = []
    
    for i in range(len(targets)):
        temp = targets[i]
        final = 0
        che = 0
        for j in range(len(temp)):
            if temp[j] not in dic:
                final = -1 # 만약 딕셔너리에 없다면 final을 -1 로 설정해주고 break!
                break
            elif temp[j] in dic:
                final += dic[temp[j]]
        if che == 0:
            answer.append(final)
                
    
    return answer