Programmin-in-Python

Returning the Indices of numbers in an array that sums to a particular number

Feb 3rd, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.21 KB | None | 0 0
  1. from itertools import permutations as perm
  2.  
  3. def TwoSum(arr, target):
  4.     L1 = perm(arr, 2)
  5.  
  6.     for i in L1:
  7.         if sum(i) == target:
  8.             return [arr.index(i[0]), arr.index(i[1])]
  9.  
  10. var = TwoSum([3,2,4], 6)
  11. print(var)
Advertisement
Add Comment
Please, Sign In to add comment