문제) 프로그래머스 - 구현 - 같은 숫자는 싫어
https://school.programmers.co.kr/learn/courses/30/lessons/12906
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이)
리스트 내에 연속적으로 중복되는 숫자들을 제거하는 문제였습니다. 파이썬 리스트의 마지막 인덱스([-1])을 이용하여 비교해가면 해결할 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin.readline | |
def solution(arr): | |
answer = [] | |
answer.append(arr[0]) | |
for i in range(1, len(arr)): | |
if answer[-1] != arr[i]: | |
answer.append(arr[i]) | |
else: | |
continue | |
return answer |
반응형
'PS(Problem Solving) > 프로그래머스_Programmers' 카테고리의 다른 글
[프로그래머스] 2020 카카오 인턴십 - 키패드 누르기 (파이썬) 문제 및 풀이 (0) | 2022.08.06 |
---|---|
[프로그래머스] 2022 카카오 공채 - 신고 결과 받기 (파이썬) 문제 및 풀이 (0) | 2022.08.04 |
[프로그래머스] 코딩테스트 고득점 Kit - 단어 변환 (Python) 문제 및 풀이 (0) | 2021.12.07 |
[프로그래머스] 코딩테스트 고득점 Kit - 네트워크 (C++) 문제 및 풀이 (0) | 2021.12.07 |
[프로그래머스] 코딩테스트 고득점 Kit - 주식 가격 (C++) 문제 및 풀이 (0) | 2021.12.04 |
댓글