Fill in

any, all & sum

Three built-ins answer common questions about a whole collection:

  • sum(items) adds everything up.
  • any(items) is True if at least one item is truthy.
  • all(items) is True if every item is truthy.

They pair beautifully with a generator expression: a comprehension without the square brackets, written right inside the call. It produces values one at a time instead of building a list first.

fuel = [3, 0, 8]
sum(f * 2 for f in fuel)          # 22
any(f == 0 for f in fuel)         # True: some lantern is empty
all(f > 0 for f in fuel)          # False
sum(1 for f in fuel if f > 2)     # 2: count matching items

any stops at the first True, and all stops at the first False. Edge cases worth knowing: any([]) is False and all([]) is True ("nothing failed").

Assignment

Replace each ___:

  1. total is the sum of all readings.
  2. has_zero is True if any reading equals 0.
  3. all_lit is True only if every island's value is True.
  4. big_count counts readings greater than 10.