October 2025
Intermediate to advanced
485 pages
13h 2m
English
These are the solutions to the Exercises.
Personally, I would have guessed that the first version is faster. This is because the first version relies on linear search, which is O(N), while the second version relies on sorting, which is O(N log N). Indeed, my benchmarking bears out this hypothesis. Here is my benchmarking code:
| | import timeit |
| | |
| | setup_code = ''' |
| | import random |
| | import exercise_1a |
| | |
| | array = [] |
| | for i in range(1000000): |
| | n = random.randint(1, 1000000) |
| | array.append(n) |
| | ''' |
| | |
| | test_code = ''' |
| | exercise_1a.minimum(array) |
| | ''' |
| | |
| | print(timeit.repeat(stmt=test_code, setup=setup_code, repeat=5, number=1)) |
This code benchmarks the first version, which I saved in a file called exercise_1a.py
Read now
Unlock full access