
def solution(arr, divisor):
answer = []
for i in range(0,len(arr)):
if arr[i] % divisor == 0:
answer.append(arr[i])
if not answer:
return [-1]
else:
return sorted(answer)
파이썬에서 리스트(배열)이 비어있으면 False, 하나라도 원소가 있으면 True로 취급
if not answer:
answer가 비어 있다면 !
예시
answer = []
if not answer:
print("비어있다!")
answer = [3, 5]
if not answer:
print("비어있다!")
else:
print("무언가 들어있다!")'코딩테스트 > Python' 카테고리의 다른 글
| [프로그래머스/Python] 콜라츠 추측 (0) | 2025.09.24 |
|---|---|
| [프로그래머스/Python] 서울에서 김서방 찾기 (0) | 2025.09.24 |
| [프로그래머스/Python] 없는 숫자 더하기 (0) | 2025.09.24 |
| [프로그래머스/Python] 음양 더하기 (0) | 2025.09.24 |
| [프로그래머스/Python] 하샤드 수 (0) | 2025.09.24 |