Input/Output Analysis
Concepts covered: Functions
Before a function has a body, it has a shape: what comes in, what goes out, and what type each is. Writing 3 or 4 input and output pairs by hand pins that shape down. Each pair is a small fact about the problem that the finished code will be held to, and the pairs usually expose the questions the sentence left open.
Write the pairs first
Problem: a fitness app records daily step counts, and we want the days that met the 10000 step goal. Before deciding anything about loops, fill a table of inputs and the outputs they should produce:
| Input | Output | Why |
|---|---|---|
| [] | [] | No days recorded, so no days met the goal. |
| [12000] | [12000] | One day, and it met the goal. |
| [8000, 12000, 10000] | [12000, 10000] | 10000 meets the goal because met means reached. |
| [9999, 500] | [] | Both fell short, so the answer is an empty list; the number 0 would be the wrong type. |
The table settled 3 things the sentence left unsaid. The output is a list, because 'the days that met the goal' is a collection even when it holds 1 or 0 items. The boundary 10000 is included. And an input with no qualifying days returns an empty list rather than the number 0, which a caller that appends the result or asks for its length would otherwise break on.
def goal_days(steps: list) -> list:
met = []
for count in steps:
if count >= 10000:
met.append(count)
return met
print(goal_days([]))
print(goal_days([12000]))
print(goal_days([8000, 12000, 10000]))
print(goal_days([9999, 500]))Output:
[] [12000] [12000, 10000] []
Each printed line matches its row in the table, and that's the point of having written the table: the table decided what correct meant, and the code followed it.
Input type, output type
The second half of the analysis is types. The same sentence can hide very different functions depending on what the caller holds and what they need back. Here are 3 readings of 'find the most expensive item' with the same cart:
| Input | Output | Function returns |
|---|---|---|
| list of prices [4.5, 12.0, 7.25] | 12.0 | a float, the price |
| list of (name, price) tuples | "headphones" | a string, the name |
| list of (name, price) tuples | ("headphones", 12.0) | a tuple, the whole item |
All 3 are reasonable, and only the caller knows which one they want. Writing the type hint on the signature forces that question before the body exists: -> float, -> str and -> tuple describe 3 different functions. The float version compares each item directly. The 2 tuple versions share 1 loop that compares item[1] and keeps the whole tuple. They differ only in the return line: best[0] for the name, best for the item.
def priciest_name(items: list) -> str:
best = items[0]
for item in items:
if item[1] > best[1]:
best = item
return best[0]
cart = [("cable", 4.5), ("headphones", 12.0), ("case", 7.25)]
print(priciest_name(cart))Output:
headphones
Each item is a tuple, so item[1] is the price being compared and best[0] is the name being returned. The input row said tuples and the output row said string, and those 2 index positions follow from the rows.
Practice
Problem: given a list of exam scores, return how many students scored under 40. A teammate writes these input and output pairs. Which pair is wrong?
- Input [] gives output 0
- Input [40] gives output 0
- Input [39, 12] gives output [39, 12]
- Input [55, 39] gives output 1
A function that returns the values that passed a test has the same shape as goal_days, and its pairs table predicts its code. The result starts as [], because an input where nothing passes must return an empty list. The comparison carries the boundary the sentence names, and .append adds each kept value in input order. When no value passes, the function returns the [] it started with, unchanged. That is the empty row of the table.
About this lesson
This section is part of the Problem Solving: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.