Print 1..n without loops
Challenge · Write code · about 12 min
Return the list [1, 2, …, n] with no loop of any kind. countUp(0) returns the empty list.
The recursive call must still count down from n. Decide whether n goes before or after the result of the call.
Example 1
Input:
[3]
Output:
[1,2,3]
countUp(2), then 3 at the end.
Example 2
Input:
[1]
Output:
[1]
Constraints
- 0 ≤ n ≤ 500
- No for, while or array helper that loops for you.
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
- Factorial trace Warm-up
- Sum of digits Warm-up
- Missing base case Standard
- Countdown Standard
- Follow the frames of fact(4) Standard