Ted Idzikowski - 1 year ago 145

Python Question

In python 3, how to do you implement the function pairSum() that takes as input a list of distinct integers 1st and then an integer n, and prints the indexes of all pairs of values in 1st that sum up to n?

Expected result:

`>>> pairSum([7,8,5,3,4,6],11)`

0 4

1 3

2 5

Here's what I did:

`def pairSum(l,s):`

i = 0

j = len(l)-1

for x in range(len(l)**len(l)):

if float(i) < float(len(l)/2):

if (l[i] + l[j] == s):

print(i,end=' ')

print(j)

i += 1

elif (l[i] + l[j] < s):

j += 1

elif (l[i] + l[j] > s):

j -= 1

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You just have to iterate through all the values and check for the remaining values:

```
def pairSum(l, total):
for i, x in enumerate(l):
for j, y in enumerate(l[i+1:], start=i+1):
if x+y == total:
print(i, j)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**