Help for the VSCode editor.
-
What is the most appropriate definition for recursion?
- A function that calls another function.
- A function that calls another execution instance of the same function.
- An in-built method for solving base cases related problems.
- A variadic function that calls another variadic function.
Reveal
B
Also known as "a function that calls itself"
-
Complete the missing line (to calculate the factorial of a number)
func fact(n int) int { if n == 0 { return 1 } return ______ }
Reveal
n * fact(n-1)
Calculating a factorial is a classic use case for recursion.
n! = n * (n-1) * (n-2) * (n-3) * ... * 3 * 2 * 1 = n * (n-1)!
In mathematics, the factorial of a non-negative integer
n
, denoted byn!
, is the product of all positive integers less than or equal ton
. The factorial ofn
also equals the product ofn
with the next smaller factorial. This fact is what makes the calculation suitable for a recursive function. -
What would be the output of the following program:
func print(n int) { if n == 0 { return } fmt.Print(n) print(n - 1) } func main() { print(5) }
- 5 4 3 2 1
- 54321
- 12345
- 1 2 3 4 5
Reveal
54321
main()
begins by callingprint()
with5
.- In the
print()
function,n
starts as5
, so that is printed first. This eliminates two possible answers. - Then
print
calls itself, withn-1
, i.e. 4. Here is the recursion. print
will keep doing this untiln == 0
, which will be after1
has been printed. Then it will return all the way up and the program will end.fmt.Print
will not emit a space after printing an integer, so that leaves only the given answer. `
-
What would be the output of the following program:
func print(n int) { if n == 0 { return } print(n - 1) fmt.Print(n) } func main() { print(5) }
- 5 4 3 2 1
- 54321
- 12345
- 1 2 3 4 5
Reveal
12345
This is subtly different from the previous question! Firstly for the same reason ias the previous question, we can rule out the answers that have spaces between the numbers.
What's different? This time round it recurses all the way down until the function bottoms out (
n == 0
), then as it returns up the stack it prints the output. At maximum recursion it makes the fisrt return, then it starts printing wheren == 1
and returns and so on, so the numbers are printed in ascending order as we return up the stack. -
Select the correct base case for function printSquares -
for input n -> prints squares for n, n-1, n-2, ... -5
Example: Input: n=2 Output: 4, 1, 0, 1, 4, 9, 16, 25
func print(n int) { // base case fmt.Printf("%d ", n*n) print(n - 1) } func main() { print(2) }
Replace
// base case
with which of the following:-
if n == 5 { return }
-
if n == -5 { return }
-
if n == 0 { return }
-
if n == -6 { return }
Reveal
if n == -6 { return }
- The specification requires to print the squares of numbers from the starting value down to and including
-5
- The
print()
function prints the square first, then calls itself with the next smaller number - Therefore to get -52 printed, it will then call itself with -6, so this is what must be tested for to bottom out the recursion and return.
-