python - How can I fix my function? -
i being told.. comments fix function make "cleaner". i've tried alot.. don't know how use llambda accomplish i'm trying do. code works.. isn't being asked of me.
here code suggestions on how fix it.
def immutable_fibonacci(position): #define lambda instead of def here def compute_fib (previousseries, ignore): newlist = previousseries if len(newlist) < 2: # outside , keep function focused on returning new list last element being sum of previous 2 elements newlist.append(1) else: first = newlist[-1] second = newlist[-2] newlist.append(first+second) return newlist range=[none]*position return reduce(compute_fib, range, []) #above code. how this: #next_series = lambda series,_ : (use these instead of above line) #return reduce(next_series, range(position - 2), [1, 1])
anything helps.. confused on how can implement these suggestions.
here attempted.
def immutable_fibonacci(position): range=[none]*position next_series = lambda series, _ : series.append(series[-1] + series[-2]) return reduce(next_series, range(position - 2), [1, 1])
the append
function returns none. need return renewed array
next_series = lambda series: series + [series[-1] + series[-2]]
also renaming range
isn't serving purpose.
def immutable_fibonacci(position): next_series = lambda series: series + [series[-1] + series[-2]] return reduce(next_series, range(position - 2), [1, 1])
this assuming call function positions >= 2. conventionally fib(0) 0 , fib(1) 1.
Comments
Post a Comment