Alternate solution - Two sum
def two_sum(list_value,target):
'''create a dictionary with list values as keys'''
dict_list = {list_value[i]:i for i in range(len(list_value))}
'''iterate through dict'''
for k,v in dict_list.items():
required_val = target - k
'''if required val is in dict and is not the current key we iterating on'''
if required_val in dict_list and k != required_val:
print(v,dict_list[required_val])
return(v,dict_list[required_val])
return -1
1 answers ( 0 marked as helpful)
i get an error when trying to run this