BIGO-P1

Two loads that add up

Practice 1 of 5Cost: Big-O and friends8 tests

The problem

A goods yard has a list a of wagon loads. Return True if two loads at different positions add up to exactly target, and False otherwise.

has_pair_sum([40, 25, 60, 15], 75)
# True: 60 + 15
has_pair_sum([40, 25, 60, 15], 50)
# False: there is only one 25
has_pair_sum([5, 3, 5], 10)
# True: two different 5s

Checking every pair is correct, but it takes about n × n steps: Θ(n²). Read the limits and work out, as in lesson 3, whether that is fast enough.

Limits

  • Up to 100,000 loads. Loads and the target are whole numbers, and may be negative.

Builds on BIGO-3 Growth rates and BIGO-4 Space cost.

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.

8 tests, not run yet
Test results
#TestResultReads · time
1A pair existsNot run
2No pairNot run
3The same value twiceNot run
4One value cannot pair with itselfNot run
5Empty and singleNot run
6Negative valuesNot run
7100,000 values, no pairNot run
8100,000 values, pair at the endNot run