Last answered:

02 Aug 2024

Posted on:

01 Aug 2024

0

solution for two sum , is this soultion correct or lack in certain aspects ??

l=[2,3,4,5,7]
tgt=10
def f(l):
    d = {}
    for i in range(len(l)):
        d[l[i]]=i
 return d
for x,m in f(l).items():
     for i in range(len(l)): # Reset the iterator i for each x, m pair
 if l[i] != x :
     if l[i] + x==tgt:
 print(i,m)

2 answers ( 0 marked as helpful)
Instructor
Posted on:

02 Aug 2024

1

Hi,

Good to hear from you! I can see two issues with the solution posted:

1) You don't need a separate function to create the dictionary.
2) Use a single pass to check for pairs instead of nested loops, which improves efficiency.

Best,

Ned

Posted on:

02 Aug 2024

0

THANK YOU 

Submit an answer