AlgoViz

DRY

easy 40 min

Don't repeat yourself: every piece of knowledge has one home, and everything else asks that home.

Watch lesson4:18

Problem

Find the fact that lives in several places.

The point: before you extract anything, write down the fact and every place that knows it.

The village hall has a booking program. The task: *emails like abc@ are accepted, and the tax changes from 8 to 8.5 percent on 1 July 2026.* It sounds like two small edits.

Inventory

Who knows what

FactWhere it livesIn which form
A valid emailregister, updateEmail, sendResetTwo copies and one rewrite
Tax of 8 percentOrder, subscription, service fee0.08 three times
How to round centsThe same threeNot at all: floats are printed with two digits
Password length 8register onlyOne home already
Voucher length 8VoucherAnother fact that is also 8
The last row stays alone. It only looks like the one above it.
Python
def order_total(items):    subtotal = sum(i["price"] for i in items)    return subtotal + subtotal * 0.08def subscription_total(monthly, months):    subtotal = monthly * months    return subtotal + subtotal * 0.085        # changed here, not in the othersdef service_fee(hours, rate):    subtotal = hours * rate    return subtotal + subtotal * 0.08print(order_total([{"price": 100}]), subscription_total(50, 2), service_fee(4, 25))
The three totals today. The second one was already changed by somebody in a hurry.
Edit and run it in your browser

Three ways to spend 100, and two different totals. No test fails, because each function is tested against its own number.