Two Sum on LeetCode
You want something along these lines:
#! python3
def two_sum(arr,targ):
look_for = {}
for n,x in enumerate(arr,1):
try:
return look_for[x], n
except KeyError:
look_for.setdefault(targ - x,n)
a = (2,7,1,15)
t = 9
print(two_sum(a,t)) # (1,2)
a = (-3,4,3,90)
t = 0
print(two_sum(a,t)) # (1,3)
Here you build the dictionary of values on an as-needed basis. The dictionary is keyed by the values you are seeking, and for each value you track the index of its first appearance. As soon as you come to a value that satisfies the problem, you're done. There is only one for loop.
The only other detail is to add 1 to each index to satisfy the ridiculous requirement that the indices be 1-based. Like that's going to teach you about Python programming.
Keys are added to the dictionary using the setdefault function, since if the key is already present you want to keep its value (the lowest index).
def twosum(nums=(6, 7, 11, 15, 3, 6, 5, 3), target=6):
lookup = dict(((v, i) for i, v in enumerate(nums)))
return next(( (i+1, lookup.get(target-v)+1)
for i, v in enumerate(nums)
if lookup.get(target-v, i) != i), None)
I have not tested this extensively but the basic logic should be sound. This algorithm can be broken up into two stages:
Create a dictionary of value->index for all index, value pairs in nums. Note that you can have multiple values with different indices. In this case, the highest index will be stored in the dictionary and lower indexes will be overwritten. This behavior can be modified, of course, but I don't believe it needs to be for this problem because part of the problem statement is this: "You may assume that each input would have exactly one solution." Thus, each input has a single unique output so we never have to worry about returning a "wrong-pair" of indices.
Loop through the enumeration of nums, getting
i
as index, andv
as value. Check iftarget-v
is a key in the dictionary we created, and simultaneously assert that the value pointed to by that key is noti
. If this is ever true, return the tuplei+1, lookup.get(target-v)+1
.