BSRC-P1

Find the platform

Practice 1 of 5Binary search12 tests

The problem

A station lists its platform numbers from smallest to largest, with no repeats. You are given this list, platforms, and a platform number, target. Return the index where target is in the list. If it is not in the list, return -1.

search([3, 7, 11, 14, 19], 14)   # 3
search([3, 7, 11, 14, 19], 12)   # -1
search([], 5)                    # -1

In the large tests, the list counts every value you read from it. A binary search stays well under the limit. Anything that goes through the whole list goes over it. This includes in, .index(), list(…), sorted(…) and slicing.

Limits

  • Up to 1,000,000 platforms. At most 25 reads on the large tests.
  • The list may be empty.

Builds on BSRC-1 Halving the search space and BSRC-2 The invariant.

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.

12 tests, not run yet
Test results
#TestResultReads · time
1Target in the middleNot run
2Target at index 0Not run
3Target at the last indexNot run
4Not in the list, smaller than all valuesNot run
5Not in the list, bigger than all valuesNot run
6Not in the list, between two valuesNot run
7Empty listNot run
8One platform, presentNot run
9One platform, not in the listNot run
10Two platformsNot run
11A million platforms, at most 25 readsNot run
12A million platforms, target not in the listNot run