Two posts with a target sum
Write code · Basic · §7.1 Trying them all
views holds the number of views for each post. Complete the function solution, which returns the number of ways to pick two different posts whose views add up to target.
The order of picking does not matter. Even if two posts have the same number of views, they count as different posts.
Function
solution(views, target)
Constraints
- The length of
viewsis from 2 to 300. - The number of views for each post is an integer from 0 to 10,000, and
targetis an integer from 0 to 20,000.
Examples
| views | target | result |
|---|---|---|
[1, 2, 3, 4] | 5 | 2 |
[5, 5, 5] | 10 | 3 |
Starter code
def solution(views, target):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.