39.4k views
3 votes
Implement the function pairSum that takes as parameters a list of distinct integers and a target value n and prints the indices of all pairs of values in the list that sum up to n. If there are no pairs that sum up to n, the function should not print anything. Note that the function does not duplicate pairs of indices

1 Answer

3 votes

Answer:

Following are the code to this question:

def pairSum(a,x): #find size of list

s=len(a) # use length function to calculte length of list and store in s variable

for x1 in range(0, s): #outer loop to count all list value

for x2 in range(x1+1, s): #inner loop

if a[x1] + a[x2] == x: #condition check

print(x1," ",x2) #print value of loop x1 and x2

pairSum([12,7,8,6,1,13],13) #calling pairSum method

Output:

0 4

1 3

Step-by-step explanation:

Description of the above python can be described as follows:

  • In the above code, a method pairSum is declared, which accepts two-parameter, which is "a and x". Inside the method "s" variable is declared that uses the "len" method, which stores the length of "a" in the "s" variable.
  • In the next line, two for loop is declared, in the first loop, it counts all variable position, inside the loop another loop is used that calculates the next value and inside a condition is defined, that matches list and x variable value. if the condition is true it will print loop value.
User John Livermore
by
5.8k points