-
나누어 떨어지는 숫자배열프로그래밍/알고리즘 2021. 4. 2. 13:18
나의 풀이
def solution(arr, divisor): answer = [] for number in arr: if number % divisor == 0: answer.append(number) if len(answer) == 0: answer.append(-1) return sorted(answer)
다른 사람 풀이
def solution(arr, divisor): return sorted([n for n in arr if n%divisor == 0]) or [-1]
or로 -1을 반환한게 아주 마음에 든다. 난 괜히 길이 조건문을 썼는데....
728x90반응형LIST'프로그래밍 > 알고리즘' 카테고리의 다른 글
이상한 문자 만들기 (0) 2021.04.02 문자열 내 마음대로 정렬하기 (0) 2021.04.02 체육복 (0) 2021.03.31 124 나라의 숫자 (0) 2021.03.30 프린터 (0) 2021.03.30