-
우박수열 정적분코딩 테스트/Level 2 2022. 11. 9. 21:04반응형
https://school.programmers.co.kr/learn/courses/30/lessons/134239
def solution(k, ranges): points = [k] while k != 1: if k % 2 == 0: k //= 2 else: k = 3 * k + 1 points.append(k) return [-1 if a > (c := len(points) + b - 1) else sum((points[i] + points[i + 1]) / 2 for i in range(a, c)) for a, b in ranges]
보기 좋게 두 개의 함수로..
def collatz(k): points = [k] while k != 1: if k % 2 == 0: k //= 2 else: k = 3 * k + 1 points.append(k) return points def solution(k, ranges): points = collatz(k) answer = [] for a, b in ranges: c = len(points) + b - 1 if a > c: answer.append(-1) else: temp = 0 for i in range(a, c): temp += (points[i] + points[i + 1]) / 2 answer.append(temp) return answer
반응형