Editor

Two Sum

Advanced · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Write two_sum(nums, target) that returns the indexes (i, j) with i < j of the two numbers that add up to target, or None if there are none. Do it in one pass (O(n)), not with two nested loops.

two_sum([2, 7, 11, 15], 9)  ->  (0, 1)
def two_sum(nums, target):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
two_sum([2, 7, 11, 15], 9)(0, 1)
two_sum([3, 2, 4], 6)(1, 2)

More practice problems

All Python exercises · 50 practice problems