쉽고 깔끔하게
[프로그래머스/python3] Level 1 자연수 뒤집어 배열로 만들기 문제풀이 본문
728x90
반응형

문제
programmers.co.kr/learn/courses/30/lessons/12932
코딩테스트 연습 - 자연수 뒤집어 배열로 만들기
자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다. 제한 조건 n은 10,000,000,000이하인 자연수입니다. 입출력 예 n return 12345
programmers.co.kr
자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요.
<정답>
This file contains hidden or 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
def solution(n): | |
return list(map(int, reversed(str(n)))) |
※ 내림차순 정렬이 아니라 말 그대로 역순으로 뒤집기이다. 따라서, sorted(map(int, str(n)), reverse = True)는 틀린 답이다. 함정에 빠지지 말자!
728x90
반응형
'Algorithm > Programmers' 카테고리의 다른 글
[프로그래머스/python3] Level 1 핸드폰 번호 가리기 문제풀이 (0) | 2021.01.27 |
---|---|
[프로그래머스/python3] Level 1 예산 문제풀이 (0) | 2021.01.27 |
[프로그래머스/python3] Level 1 직사각형 별찍기 문제풀이 (0) | 2021.01.26 |
[프로그래머스/python3] Level 1 x만큼 간격이 있는 n개의 숫자 문제풀이 (0) | 2021.01.25 |
[프로그래머스/python3] Level 1 문자열 내 p와 y의 개수 문제풀이 (0) | 2021.01.25 |