Problem 457197 · easy · Phase 04 Non-Linear Data Structures

Crate Manifest Total

recursion · nested lists · isinstance

A shipping manifest lists the weights inside a crate, but crates may contain smaller crates, which may contain smaller crates still. The manifest is an arbitrarily nested list whose leaves are integers. Write manifest_total(items) returning the sum of every integer at every depth.

Examples

Input:  items = [1, [2, 3], [[4]], 5]
Output: 15

Input:  items = []
Output: 0

Constraints

  • Leaves are integers in [-10**6, 10**6]; at most 10**4 leaves in total.
  • Nesting depth at most 1500, so a recursive solution stays well under the recursion limit.

Goals

  • Distinguish a leaf value from a sub-list with isinstance
  • Accumulate a recursive result across every element of a list
  • Handle empty lists at any depth
Starting Python…