CORR-P1

Is the timetable in order?

Practice 1 of 5Correctness9 tests

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

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1SortedNot run
2One pair out of orderNot run
3Out of order only at the endNot run
4Equal times are allowedNot run
5Empty timetableNot run
6One trainNot run
7First and last fine, middle wrongNot run
8DescendingNot run
9500,000 times, one swap at the endNot run