4
def consume_tail(current, s):5
# Following the definition of the fibonacci sequence6
# we know that the sum of the last two values in our7
# `current` list determines the next value in the sequence.8
# So that value, our "target", is what we're looking for next in10
target = current[-1] + current[-2]16
# If the next value in the fibonacci sequence17
# is found at the beginning of s18
# we can continue to process the remaining19
# portion of the string.20
if s.find(sTarget) == 0:21
current.append(target)26
return consume_tail(current, s[len(sTarget) :])32
def splitIntoFibonacci(self, num: str) -> List[int]:34
# Identify candidate for the first35
# number in fibonacci sequence36
for i in range(len(num)):37
if num[0] == "0" and i > 0:40
first = num[0 : i + 1]42
# If our current candidate for the first number43
# of the sequence is already larger that our44
# maximum value in the spec, don't bother doing anymore work.50
# Identify candidate for the scond51
# number in fibonacci sequence52
for j in range(len(tail)):53
if tail[0] == "0" and j > 0:56
second = tail[0 : j + 1]60
# With our current candidates (first and second),61
# we can consume the remaining portion of the string (tail[j+1:])62
# to determine if it contains the correct values for a fibonacci sequence63
# beginning with [first, second]64
result = consume_tail([int(first), int(second)], tail[j + 1 :])