fibonacci series using lists in python -


i trying print fibonacci series using lists in python. code:

t = int(raw_input()) n = [int(raw_input()) in range(t)] n[:] = [x-2 x in n] l1 = [1, 1] l2 = [] j in n:     k in range(j):         l1.append(l1[-1]+l1[-2])     l2.append(l1) print l2 

here, t denotes number of test cases. n denotes number of elements in fibonacci series.i need print required output of fibonacci series each test case.

input:  2 4 5 

output code above:

[[1, 1, 2, 3, 5, 8, 13], [1, 1, 2, 3, 5, 8, 13]]

however output want print is:

[1, 1, 2, 3], [1, 1, 2, 3, 5]]

please let me know how should proceed on this

your problem don't reset l1. try:

t = int(raw_input()) n = [int(raw_input()) in range(t)] n[:] = [x-2 x in n] l2 = [] j in n:   l1 = [1, 1]   k in range(j):     l1.append(l1[-1]+l1[-2])   l2.append(l1) print l2 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -