https://school.programmers.co.kr/learn/courses/30/lessons/155652
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 접근
<처음 접근>
- 알파벳을 담아놓을 리스트(alpabets_list)를 만든다.
- skip의 길이만큼 반복문을 돌면서 해당 알파벳을 제거한다.
- 만약 해당 알파벳을 index만큼 건너 뛴 값이 마지막 알파벳을 넘으면 다시 첫 알파벳으로 복귀
=> 런타임 에러로 78.9 ..
<수정>
리스트 범위에서 넘어갈 경우 빼기 연산이 아니라 나머지 연산으로 jump를 수정해야 한다.
=> 통과!!
코드
<처음 시도>
def solution(s, skip, index):
answer = []
alphabets = 'abcdefghijklmnopqrstuvwxyz'
alphabets_list = list(alphabets)
for i in range(len(skip)):
if skip[i] in alphabets_list:
alphabets_list.remove(skip[i])
for j in range(len(s)):
k = alphabets_list.index(s[j])
jump = k+index
if jump >= len(alphabets_list):
jump = (k+index) - len(alphabets_list)
answer.append(alphabets_list[jump])
answer = ''.join(answer)
return answer
<오답>
def solution(s, skip, index):
answer = []
alphabets = 'abcdefghijklmnopqrstuvwxyz'
alphabets_list = list(alphabets)
for i in range(len(skip)):
if skip[i] in alphabets_list:
alphabets_list.remove(skip[i])
for j in range(len(s)):
k = alphabets_list.index(s[j])
jump = k+index
if jump >= len(alphabets_list):
jump = (k+index) % len(alphabets_list)
answer.append(alphabets_list[jump])
answer = ''.join(answer)
return answer
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스]Lv2. 주차 요금 계산(Python) (1) | 2024.03.07 |
---|---|
[프로그래머스]Lv2. k진수에서 소수 개수 구하기(Python) (0) | 2024.03.07 |
[프로그래머스]Lv2. 자동차 평균 대여 기간 구하기(SQL) (0) | 2024.03.07 |
[프로그래머스]Lv3. 다단계 칫솔 판매(Python) (0) | 2024.03.07 |
[프로그래머스]Lv2. 양궁대회(Python) (0) | 2024.02.29 |