19Two Sum

    Mediumdictionarieslists

    Write two_sum(nums, target) that returns the indexes [i, j] of the two numbers in nums that add up to target, with i < j.

    Every test has exactly one answer, and you may not use the same element twice.

    Examples

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

    Good to know

    • Try to do it in one pass through the list, without checking every pair.

    Run examples checks the 2 examples above. Submit runs all 6 tests, including hidden edge cases, and marks the challenge solved.

    solution.py
    Loading Python…

    Test results show up here

    Write two_sum in the editor, then run the examples. Use print() freely: its output is shown for each test.