$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #255

Which fix removes the bug?

from functools import lru_cache

@lru_cache(maxsize=None)
def total(items):
    return sum(items)

total([1, 2, 3])  # TypeError
  1. Use @lru_cache(maxsize=128)
  2. Add typed=True to the decorator
  3. Pass a tuple: total((1, 2, 3))
  4. Return sum(list(items)) instead
lru_cache hashes the arguments to build its cache key, and a list is unhashable. A tuple is hashable and sums the same way.
Play Problem Solving ▸