Count the Pair Checks
Intermediate · Write the code · from Algorithmic Efficiency and Heuristics: Reasonable and Unreasonable Time
From the lesson: Algorithmic Efficiency and Heuristics: Reasonable and Unreasonable Time
Efficiency is how much computing work an algorithm needs, and it decides whether a program answers in the blink of an eye or would need longer than the age of the universe. Two correct algorithms for the same job can sit at opposite ends of that range.
Read the lessonThe "compare every pair" duplicate check from the lesson runs this loop over a list of n items:
for i in range(n):
for j in range(i + 1, n):
checks += 1Write pair_checks(n) that returns how many times checks += 1 runs. You can run the loop and count, or find a pattern.
pair_checks(4) -> 6
pair_checks(10) -> 45def pair_checks(n):
# Return how many times checks += 1 runs for n items
pass
What Check runs
Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| pair_checks(4) | 6 |
| pair_checks(10) | 45 |
| pair_checks(1) | 0 |
More Algorithmic Efficiency exercises
- How Many Routes?Beginner
- Nearest Neighbor RouteAdvanced