Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- typeORM
- mongoose
- MySQL
- JavaScript
- flask
- Sequelize
- MongoDB
- nodejs
- Python
- react
- class
- dfs
- nestjs
- 공룡게임
- OCR
- TypeScript
- Nest.js
- game
- Queue
- jest
- Dinosaur
- 자료구조
- AWS
- cookie
- Express
- 게임
- GIT
- 정렬
- Bull
Archives
- Today
- Total
포시코딩
[프로그래머스][Lv.1] 나누어 떨어지는 숫자 배열 본문
728x90
문제
https://school.programmers.co.kr/learn/courses/30/lessons/12910
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
내 풀이
def solution(arr, divisor):
result = [x for x in arr if x % divisor == 0]
if len(result) == 0:
result = [-1]
result.sort()
return result
다른 풀이
def solution(arr, divisor):
return sorted([n for n in arr if n%divisor == 0]) or [-1]
다른 풀이를 통해 알 수 있는건
파이썬에서 [] 같이 빈 리스트는 False를 반환한다는 것과 그걸 통해
return [] or [-1]
이렇게 리턴시켜 코드를 줄일 수 있다는 것이다.
또, 지금까지 sort()만 써왔는데 sorted()에 대해서도 활용해야겠다.
다른 풀이 보고나서 내 방식대로 새로 푼 풀이
def solution(arr, divisor):
result = sorted([ x for x in arr if x % divisor == 0 ])
return result or [-1]
728x90
'자료구조알고리즘 > 문제풀이' 카테고리의 다른 글
[프로그래머스][Lv.1][정렬] K번째수 (0) | 2022.12.25 |
---|---|
[프로그래머스][Lv.1] 행렬의 덧셈 (0) | 2022.12.25 |
[프로그래머스][Lv.1] 하샤드 수 (0) | 2022.12.24 |
[프로그래머스][Lv.1] x만큼 간격이 있는 n개의 숫자 (0) | 2022.12.23 |
[프로그래머스][Lv.1] 없는 숫자 더하기 (0) | 2022.12.23 |