Editor

How Many Routes?

Beginner · 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 lesson

Write count_routes(stops) that returns how many different orders there are to visit stops places. The first place can be any of the stops, the second any of the remaining stops - 1, and so on, so the answer is stops x (stops - 1) x ... x 1. Use a loop.

count_routes(3)  ->  6
count_routes(5)  ->  120
def count_routes(stops):
# Multiply stops x (stops - 1) x ... x 1
pass

What Check runs

Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

RunsExpected output
count_routes(3)6
count_routes(5)120
count_routes(1)1

More Algorithmic Efficiency exercises

All Python exercises · 50 practice problems