Loading...
Before choosing an algorithm, establish what is supplied, what must be produced and which conditions the solution may rely on. Caching and reuse then need contracts as well as performance claims.
What you'll be able to do
An input can be user-entered data, a file, a sensor reading or another routine’s result. An output can be displayed text, a stored record, a returned value or an action signal. For a class-average calculation, specify which scores are included, their valid range and how missing results are represented before deciding how to divide the total.
A precondition is something assumed to hold before an operation, such as a sorted input array for binary search or a non-empty collection for a simple mean. Validation can enforce a condition at a boundary, but the condition itself is part of the contract. Decide what happens when it does not hold: reject, transform or use a different operation under a stated rule.
Caching retains a result or data copy so a later request can avoid repeated work or slower access. A useful cache key includes everything that determines the result. If price depends on item and catalogue version, caching by item alone can return a stale price after the catalogue changes. A higher hit rate is useful only if returned answers still meet the requirements.
The demonstration deliberately caches a lookup into mutable data. After the source price changes, the cached result stays old until cleared. Real policies can use versioned keys, expiry or explicit invalidation. A cache consumes space, has lookup and maintenance costs and provides no benefit for a result never reused. Explain whether stale data are acceptable in the scenario.
Reusable components can reduce duplicate work and concentrate testing around a stable interface. Specify parameter types, allowed ranges, outputs, failure responses and side effects. A component tested for positive integers is not automatically suitable for arbitrary signed decimals without further design and tests.
Reuse introduces dependencies and compatibility obligations. A library update can change behaviour or supported platforms, so retain suitable version and test evidence. Do not equate reuse with copying an unexplained block into a project. Understanding and adapting an interface is necessary for maintenance, and independently assessed work still needs proper acknowledgement of external material and the learner’s own contribution.
from functools import lru_cache
prices = {"A": 12}
@lru_cache(maxsize=16)
def lookup_price(item):
return prices[item]
print(lookup_price("A"))
prices["A"] = 15
print(lookup_price("A"))
lookup_price.cache_clear()
print(lookup_price("A"))Scroll horizontally to see the full code or table.Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.