
Fill in
any, all & sum
Three built-ins answer common questions about a whole collection:
sum(items)adds everything up.any(items)isTrueif at least one item is truthy.all(items)isTrueif 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 ___:
totalis the sum of allreadings.has_zeroisTrueif any reading equals 0.all_litisTrueonly if every island's value isTrue.big_countcounts readings greater than 10.
Replace each ___:
totalis the sum of allreadings.has_zeroisTrueif any reading equals 0.all_litisTrueonly if every island's value isTrue.big_countcounts readings greater than 10.
main.py Python sleeping
Press Run to try your code, then Check to test it.