AlgoFrames

Countdown

Standard · Write code · about 8 min

Return the list [n, n - 1, …, 1] using recursion and no loop. countdown(0) returns the empty list.

Example 1

Input:

[3]

Output:

[3,2,1]

3 in front of countdown(2).

Example 2

Input:

[1]

Output:

[1]

Constraints

  • 0 ≤ n ≤ 500

Learn the idea first: Recursion: base case, recursive case, the call stack, a visual lesson.

More Recursion: base case, recursive case, the call stack problems