Three Irish fare tables show the same prices. Will one rule work for every ticket?
Here are three made-up fare tables for Galway, Cork and Limerick. You spot the same prices in each one. Could you replace all three tables with one rule? Before you decide, see what happens when a passenger gets a concession fare.
You’ve already used abstraction to leave out details that don’t help answer a question. To compare fares, you can leave out the bus colour and stop names. Keep the distance, the price and the ticket type. That last detail might matter even if the first few prices match.
Put the three standard fare tables side by side and line up journeys by distance. The highlighted fares for two kilometres match, and Limerick matches too. Scan across the four-kilometre row, then the six-kilometre row. Each distance group has the same fare in all three tables.
Look at the four-kilometre standard journeys in the three tables. If you group those journeys together, which fare appears for Galway, Cork and Limerick? Pick the answer that matches that row.
Put distance along the bottom and fare up the side. At each distance, the three tables share one point. From left to right, each step of two kilometres takes you up one euro. A straight line fits all three points, but it does not verify fares at distances between them.
The fare rises one euro over two kilometres, so the rate is fifty cent per kilometre. Subtract the distance charge from the two-kilometre fare to get one euro fifty. Add that fixed amount to the distance charge. It fits at six kilometres and predicts three euro fifty at four, above the two-euro concession fare.
Now try a six-kilometre concession ticket that actually costs three euro. Use the rule to predict the fare at that distance. Does your prediction match what the passenger pays? Pick the option that gives both the prediction and the result of the test.
Suppose you only looked at the four-kilometre standard fare. A flat fare of three euro fifty would match that row perfectly. But it misses the two-kilometre fare and the six-kilometre fare. Check a proposed rule against the other groups before you decide where it applies.
Give each function one job. The first calculates the fare in cent. The second checks the city, ticket type and distance before calling it. Try the two Cork requests: the standard ticket returns three hundred and fifty cent, while the concession returns None, meaning this rule gives no quote for it.