Sum a List Recursively

Easy ⏱ 8 min 85% acceptance ★★★★★ 4.9
Implement list_sum(nums) that recursively sums the numbers in a list, without using the built-in sum() or any loop.

Examples

Example 1
Input
list_sum([1, 2, 3, 4])
Output
10
Explanation

1 + (2 + (3 + (4 + 0))).

Example 2
Input
list_sum([])
Output
0
Explanation

Empty list is the base case, sums to 0.

Constraints

  • nums is a list of ints or floats (possibly empty).

Topics

RecursionLists

Companies

AdobeOracle

Hints

Hint 1

Base case: empty list returns 0.

Hint 2

Recursive case: nums[0] + list_sum(nums[1:]).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.