Skip to main content
edited body
Source Link
17slim
  • 1.2k
  • 1
  • 16
  • 22

l and r are not your indices, but values from your array.

Say you have an array: [21,22,23,23]. l is 21, r is 23; therefore, calling array[21] is out of bounds.

Additionally, you would have a problem with your indx_Dict. You call enumerate on it, which returns [(0,21),...(3,23)]. Calling dict gives you {0:21,1:22,2:23,3:23}. There is no key equivalent to 21 or 23, which will also give you an error.

What you could try is:

def twoSum(num_array, asum): '''1.twoSum Given an array of integers, return indices of the two numbers that add up to a specific target. ''' array = sorted(num_array) l = 0 r = len(array)-1 while (l < len(array)-1) : while (r > l): if (array[l] + array[r]) == asum: return [num_array.index(array[l]),\ num_array.index(array[r])] r -= 1 r = len(array)-1 l += 1 num_array1 = [2, 7, 11, 15,1,0] target1 = 9 twoSum(num_array1, target1) 

This way, your l and r are both indices of the sorted array. It goes through every possible combination of values from the array, and returns when it either has found the sum or gone through everything. It then returns the index of the original num_array that contains the correct values.

Also, as @hiro_protagonist@hiro-protagonist said, sum is a built-in function in Python already, so it should be changed to something else (asum in my example).

l and r are not your indices, but values from your array.

Say you have an array: [21,22,23,23]. l is 21, r is 23; therefore, calling array[21] is out of bounds.

Additionally, you would have a problem with your indx_Dict. You call enumerate on it, which returns [(0,21),...(3,23)]. Calling dict gives you {0:21,1:22,2:23,3:23}. There is no key equivalent to 21 or 23, which will also give you an error.

What you could try is:

def twoSum(num_array, asum): '''1.twoSum Given an array of integers, return indices of the two numbers that add up to a specific target. ''' array = sorted(num_array) l = 0 r = len(array)-1 while (l < len(array)-1) : while (r > l): if (array[l] + array[r]) == asum: return [num_array.index(array[l]),\ num_array.index(array[r])] r -= 1 r = len(array)-1 l += 1 num_array1 = [2, 7, 11, 15,1,0] target1 = 9 twoSum(num_array1, target1) 

This way, your l and r are both indices of the sorted array. It goes through every possible combination of values from the array, and returns when it either has found the sum or gone through everything. It then returns the index of the original num_array that contains the correct values.

Also, as @hiro_protagonist said, sum is a built-in function in Python already, so it should be changed to something else (asum in my example).

l and r are not your indices, but values from your array.

Say you have an array: [21,22,23,23]. l is 21, r is 23; therefore, calling array[21] is out of bounds.

Additionally, you would have a problem with your indx_Dict. You call enumerate on it, which returns [(0,21),...(3,23)]. Calling dict gives you {0:21,1:22,2:23,3:23}. There is no key equivalent to 21 or 23, which will also give you an error.

What you could try is:

def twoSum(num_array, asum): '''1.twoSum Given an array of integers, return indices of the two numbers that add up to a specific target. ''' array = sorted(num_array) l = 0 r = len(array)-1 while (l < len(array)-1) : while (r > l): if (array[l] + array[r]) == asum: return [num_array.index(array[l]),\ num_array.index(array[r])] r -= 1 r = len(array)-1 l += 1 num_array1 = [2, 7, 11, 15,1,0] target1 = 9 twoSum(num_array1, target1) 

This way, your l and r are both indices of the sorted array. It goes through every possible combination of values from the array, and returns when it either has found the sum or gone through everything. It then returns the index of the original num_array that contains the correct values.

Also, as @hiro-protagonist said, sum is a built-in function in Python already, so it should be changed to something else (asum in my example).

Source Link
17slim
  • 1.2k
  • 1
  • 16
  • 22

l and r are not your indices, but values from your array.

Say you have an array: [21,22,23,23]. l is 21, r is 23; therefore, calling array[21] is out of bounds.

Additionally, you would have a problem with your indx_Dict. You call enumerate on it, which returns [(0,21),...(3,23)]. Calling dict gives you {0:21,1:22,2:23,3:23}. There is no key equivalent to 21 or 23, which will also give you an error.

What you could try is:

def twoSum(num_array, asum): '''1.twoSum Given an array of integers, return indices of the two numbers that add up to a specific target. ''' array = sorted(num_array) l = 0 r = len(array)-1 while (l < len(array)-1) : while (r > l): if (array[l] + array[r]) == asum: return [num_array.index(array[l]),\ num_array.index(array[r])] r -= 1 r = len(array)-1 l += 1 num_array1 = [2, 7, 11, 15,1,0] target1 = 9 twoSum(num_array1, target1) 

This way, your l and r are both indices of the sorted array. It goes through every possible combination of values from the array, and returns when it either has found the sum or gone through everything. It then returns the index of the original num_array that contains the correct values.

Also, as @hiro_protagonist said, sum is a built-in function in Python already, so it should be changed to something else (asum in my example).