Lv2 프로그래머스(Programmers)[Python][파이썬] 우박 수열 정적분

"""
출처:프로그래머스,
https://school.programmers.co.kr/learn/courses/30/lessons/134239
"""

# 풀이 과정
def solution(k, ranges):
count = 0
fun = [[0, k]]

while True:
if k == 1:
break
count += 1
if k % 2 == 0:
k = k / 2
fun.append([count, k])
else:
k = k * 3 + 1
fun.append([count, k])
result = []

for a, b in ranges:
s = 0

if count + b < a:
result.append(-1)
continue

for c in range(a, count + b):
if a > count + b:
result.append(-1)
break
if c >= len(fun) - 1 or c < 0:
result.append(-1)
break
elif c <= len(fun) - 2:
if c < 0:
s = -1
result.append(-1)
break
k = ((abs(fun[c + 1][1] + fun[c][1])) / 2)
s += k
if s != -1:
result.append(s)

return result