The problem
A timetable lists departure times as whole numbers, such as 905 for 9:05. Return True if the times are in order: each time is at most the time after it. Otherwise return False.
is_sorted([905, 1010, 1130]) # True
is_sorted([905, 1130, 1010]) # False
is_sorted([905, 905, 1010]) # True: equal times are fine
is_sorted([]) # True
Before you code, write the spec for yourself, as in lesson 1. What should happen for an empty list, or a list with one time? Both are in order, because no pair of times is out of order.
Limits
- Up to 500,000 times.
- Do not change
times.
Builds on CORR-1 What “correct” means.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | Sorted | Not run | |
| 2 | One pair out of order | Not run | |
| 3 | Out of order only at the end | Not run | |
| 4 | Equal times are allowed | Not run | |
| 5 | Empty timetable | Not run | |
| 6 | One train | Not run | |
| 7 | First and last fine, middle wrong | Not run | |
| 8 | Descending | Not run | |
| 9 | 500,000 times, one swap at the end | Not run |