Photo Credit: Unsplash (Fotis Fotopoulos)

Problem 1: TwoSum

Problem Statement

def twoSum(nums, target):    res = []
for i in range(len(nums)):
for j in range(1, len(nums)-1):
if nums[i] + nums[j] == target and i != j:
res.append(i)
res.append(j)
return res
return [-1, -1]
# Testing
s = [10, 15, 3, 7]
k = 17
print(twoSum(s, k))
# Output: [0, 3]
Check http://pythontutor.com/ for your code-visualization (in steps)
def twoSum(nums, target):
dictionary = {}
for i in range(len(nums)):
if nums[i] not in dictionary:
dictionary[target - nums[i]] = i
else:
return [dictionary[nums[i]], i]
return [-1, -1]
# Testing:
nums = [7, 2, 15, 11]
target = 17
print(twoSum(nums, target))
# Output
[1,2]

Big Data Engineer / Data Modeler @ APS | M.S. in Information Sciences with Big Data Analytics major from University of Pittsburgh.