DEV Community

Discussion on: Daily Challenge #141 - Two Sum

Collapse
 
gagandureja profile image
Gagan • Edited

python

from itertools import combinations
def idx_sum(lst,num):
    com=  list(combinations(lst,2)) # [(1, 5), (1, 2), (1, 4), (1, 6), (5, 2), (5, 4), (5, 6), (2, 4), (2, 6), (4, 6)]
    ans = [x for x in com if sum(x)==num]  # [(1, 5), (2, 4)]
    return [(lst.index(x),lst.index(y)) for x,y in ans]  # [(0, 1), (2, 3)]

print(idx_sum([1,5,2,4,6],6))
#[(0, 1), (2, 3)]

# defines result of the code