CodeBlanks Logo Icon CodeBlanks

Summary - Tabulation

Compute Fibonacci with tabulation and print it in a mini task.

Summary - Tabulation

Tabulation is the loop-based DP method that fills a table bottom-up. No stack overflow risk.

In this mini task you will compute Fibonacci with tabulation and print it; combine all steps.

Tabulation is the bottom-up form of DP; it suits large problems.

Practice Print Fibonacci

Write fib_tab_print(n). Compute Fibonacci with tabulation and print with print(). Output of fib_tab_print(20): 6765.

Starter code

solution.py
def fib_tab_print(n):
    # Print Fibonacci

Sample solution

solution.py
def fib_tab_print(n):
    # Print Fibonacci
    if n <= 1:
        print(n)
        return
    dp = [0] * (n + 1)
    dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
    print(dp[n])
fib_tab_print(20)

Expected output

6765

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Summary - Tabulation: match the terms

Term Meaning
tabulation Bottom-up
table Table
loop Loop
dp DP

2 Loop start

Question
for i in range(____, n + 1):
Answer
for i in range(2, n + 1):

Explanation: 2 is right.

3 What is tabulation's biggest advantage?

  • No recursion needed
  • Less memory
  • Faster
  • No stack overflow Correct answer

Explanation: No stack overflow.

All lessons in Advanced