코딩테스트

프로그래머스 코테 155652 둘만의 암호 Lv1

DTV 2024. 6. 24. 11:15

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

 

프로그래머스

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

programmers.co.kr

 

s에 담긴 스트링을 index만큼 shift하는데 skip에 담긴 글자는 넘어간다.

def solution(s, skip, index):
    answer = ''
    alpha = list('abcdefghijklmnopqrstuvwxyz')
    alphadic = {v:i for i,v in enumerate(alpha)}
    for ch in s:
        idx = alphadic[ch]
        cnt = 0
        while cnt < index:
            idx += 1
            if idx > 25:
                idx = 0
            if alpha[idx] not in skip:
                cnt += 1
        answer += alpha[idx]
            
    return answer


a="aukks"	
b="wbqd"	
c=5
a = "z" 
b = "abcdefghij"
c = 20
solution(a,b,c)

검색할 리스트에서 이미 skip을 제거한후 검색할 리스트에서 index를 사용 shift 하였다.

def solution(s, skip, index):
    alphas = [chr(a) for a in range(ord("a"), ord("z")+1) if chr(a) not in skip]
    return "".join([alphas[(alphas.index(a) + index) % len(alphas)] for a in s])

이 코드를 보고 내 풀이를 좀더 다듬었다.

def solution(s, skip, index):
    answer = ''
    alpha = [ch for ch in 'abcdefghijklmnopqrstuvwxyz' if ch not in skip]
    alphadic = {v:i for i,v in enumerate(alpha) }
    for ch in s:
        answer += alpha[(alphadic[ch] + index) % len(alpha)]      
    return answer