0
 def fibo(m):
  dp = []
  for i in range(m+1):
    dp.append(0)
  dp[0] = 1
  dp[1] = 1
  if(m>1):
    for i in range(2,m+1):
      dp[i] = dp[i-1] + dp[i -1]
  return dp[m]

I am getting an error that says that the list index is out of bound can any resolve it and can post the correct code . Thanks.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-92-ea67e751dd0e> in <module>()
     12   return dp[m]
     13 
---> 14 fibo(0)

<ipython-input-92-ea67e751dd0e> in fibo(m)
      6     dp.append(0)
      7   dp[0] = 1
----> 8   dp[1] = 1
      9   if(m>1):
     10     for i in range(2,m+1):

IndexError: list assignment index out of range
2
  • Your code shows range(m+1), but the traceback range(m). Commented Mar 18, 2021 at 8:13
  • range(0+1) is just 0. So you don't create dp[1] when you call fibo(0) Commented Mar 18, 2021 at 8:19

4 Answers 4

1

When m == 0, the initial for loop only appends dp[0], so assigning to dp[1] gets an error.

Instead of assigning after the loop, append 1 in the loop. The elements after dp[1] will then be overwritten by the second loop, so it doesn't matter what their original values are.

def fibo(m):
    dp = []
    for i in range(m+1):
        dp.append(1)
    if(m>1):
        for i in range(2,m+1):
            dp[i] = dp[i-1] + dp[i-2]
    return dp[m]

Another way to do it is to initialize dp with the first two elements, and then append to it in the loop that calculates each element of the series.

def fibo(m):
    dp = [1, 1]
    if(m>1):
        for i in range(2,m+1):
            dp.append(dp[i-1] + dp[i-2])
    return dp[m]
Sign up to request clarification or add additional context in comments.

Comments

0

Why adding zeros to your list before assigning values ? When you pass m = 0, you will have an error when trying to access dp[1].

Also, there is a typo in dp[i] = dp[i-1] + dp[i -1]. Fibonacci is the sum of the (n-1)th and (n-2)th elements like this dp[i] = dp[i-1] + dp[i -2]

I would do something like :

def fibo(m):
  dp = []
  dp.append(0)
  dp.append(1)
  if(m>1):
    for i in range(2,m+1):
      dp.append(dp[i-1] + dp[i -2])
  return dp[m]

Comments

0

You can also initialize the dp list in the beginning to the length of the number and then just store the answer in the respective index.

def fib(n):
    if n<=1:
        return n
    dp=[0]*(n+1)
    dp[1]=1
    for i in range(2,n+1):
        dp[i]=dp[i-2]+dp[i-1]
    return dp[n]

>>> print(fib(5))
5
>>> print(fib(9))
34

Comments

0

The IndexError you are seeing occurs when you pass in m as 0. In this case the list contains just a single element( dp = [0] ), but your code tries to access the second element by index: i.e. dp[1] = 1 (hence the index error)

An easy way to get around this is to not initialize the list at all, and directly start appending your fibonacci series numbers to it.

def fibo(m):
   dp = []
   dp.append(1)
   dp.append(1)
   # Use a loop for the remainder of the numbers. Will only execute if m > 1
   for i in range(2,m+1):
       dp.append(dp[i-1] + dp[i-2])
   return dp[m]

An alternative solution would be to not use a list at all (since you are only interested in the nth element of the series). The solution is quite concise:

def fibo(m):
    a, b = 1, 1
    while (m > 0):
         a, b = b, a+b   # At every iteration, "a" takes the value of the next element in the fibo series.
         m -= 1
    return a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.