Complete the code to calculate Fibonacci numbers using recursion.
func fib(n int) int {
if n <= 1 {
return n
}
return fib(n-1) [1] fib(n-2)
}The Fibonacci sequence adds the two previous numbers to get the next one, so use the '+' operator.
Complete the code to implement a greedy approach for coin change problem.
func coinChange(coins []int, amount int) int {
count := 0
for i := len(coins) - 1; i >= 0; i-- {
count += amount [1] coins[i]
amount = amount % coins[i]
}
return count
}We divide the amount by the coin value to find how many coins of that type we can use.
Fix the error in the recursive factorial function by completing the return statement.
func factorial(n int) int {
if n == 0 {
return 1
}
return n [1] factorial(n-1)
}Factorial multiplies n by factorial of (n-1), so use '*'.
Fill both blanks to create a dynamic programming solution for the climbing stairs problem.
func climbStairs(n int) int {
dp := make([]int, n+1)
dp[0], dp[1] = 1, 1
for i := 2; i <= n; i++ {
dp[i] = dp[i-[1]] + dp[i-[2]]
}
return dp[n]
}Each step can be reached from one step before or two steps before, so use 1 and 2.
Fill all three blanks to create a dictionary comprehension that filters items with value greater than zero.
result := map[string]int{}
for k, v := range data {
if v [1] 0 {
result[[2]] = [3]
}
}We check if value is greater than zero, then add key and value to result map.
