October 2025
Intermediate to advanced
485 pages
13h 2m
English
The following exercises provide you with the opportunity to practice with benchmarking Python code. The solutions to these exercises are found in the section Chapter 2.
Following are two different Python functions that accept an unsorted array of integers and return the smallest number from the array. Here’s the first version:
| | def minimum(array): |
| | smallest_item_so_far = float('inf') |
| | |
| | for item in array: |
| | if item < smallest_item_so_far: |
| | smallest_item_so_far = item |
| | |
| | return smallest_item_so_far |
This first version performs a linear search on the array while keeping track of the smallest number throughout the search.
Here’s the second version:
| | def minimum(array): |
| | array.sort() |
| | return array[0] |
This second ...
Read now
Unlock full access