Complete the code to calculate Fibonacci numbers using simple recursion.
def fib(n): if n <= 1: return n return fib(n-1) [1] fib(n-2)
The Fibonacci number is the sum of the two previous Fibonacci numbers.
Complete the code to use memoization in Fibonacci calculation.
def fib_memo(n, memo={}): if n in memo: return memo[n] if n <= 1: return n memo[n] = fib_memo(n-1, memo) [1] fib_memo(n-2, memo) return memo[n]
Memoization stores results of fib(n-1) and fib(n-2) summed to avoid repeated work.
Fix the error in the greedy coin change function to return minimum coins.
def min_coins(coins, amount): coins.sort(reverse=True) count = 0 for coin in coins: while amount [1] coin: amount -= coin count += 1 return count
We subtract coin from amount only if amount is greater than or equal to coin.
Fill both blanks to create a dictionary comprehension that filters words longer than 3 letters and maps to their lengths.
words = ['apple', 'bat', 'cat', 'door'] lengths = {word: [1] for word in words if len(word) [2] 3}
We map each word to its length and filter words with length greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 3.
words = ['apple', 'bat', 'cat', 'door'] result = {{ [1]: [2] for word in words if len(word) [3] 3 }}
The dictionary maps uppercase words to their lengths, filtering words longer than 3 letters.
