https://school.programmers.co.kr/learn/courses/30/lessons/155652
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
나의 풀이
a(97) ~ z(122) 까지의 숫자로 표현하기 위해 atoz 함수를 생성하였다.
문자 a에서 5만큼 뒤에 있는 알파벳 [b, c, d, e, f] 들을 큐에 저장하도록 하였고,
큐에 저장된 알파벳이 skip 문자열에 있으면 pop후 새로운 문자열을 append 하도록 구성하였다.
from collections import deque
def atoz(i: int):
return (i - 97) % 26 + 97
def solution(s, skip, index):
answer = ''
for s_ in s:
next_alpha = deque([ord(s_) + i for i in range(1, index+1)])
count = index
while True:
if count == 0: break
p = next_alpha.popleft()
if chr(atoz(p)) in skip:
next_alpha.append(p + count)
elif chr(atoz(p)) not in skip:
count -= 1
answer += chr(atoz(p))
return answer
다른 사람 풀이
제한 사항 중 'skip에 포함되는 알파벳은 s에 포함되지 않습니다.'를 활용하여 해결하였다.
알파벳 리스트에서 skip에 해당하는 알파벳을 제거 후 s에 해당하는 문자의 + index에 해당하는 문자열을 반환한다.
def solution(s, skip, index):
answer = ''
lower = [chr(i) for i in range(97, 123) if chr(i) not in skip]
for s_ in s:
answer += lower[(lower.index(s_) + index) % len(lower)]
return answer
'Coding Test > 프로그래머스' 카테고리의 다른 글
[프로그래머스] / [Level 1] / [Python] 햄버거 만들기 (0) | 2024.01.19 |
---|---|
[프로그래머스] / [Level 1] / [Python] [PCCE 기출문제] 9번 / 이웃한 칸 (0) | 2024.01.16 |
[프로그래머스] / [Level 1] / [Python] 대충 만든 자판 (0) | 2024.01.15 |
[프로그래머스] / [Level 1] / [Python] 문자열 나누기 (0) | 2024.01.15 |
[프로그래머스] / [Level 1] / [Python] 숫자 짝꿍 (0) | 2024.01.12 |