-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.py
More file actions
51 lines (41 loc) · 1.43 KB
/
Copy pathTwoSum.py
File metadata and controls
51 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 2 14:31:08 2020
@author: Daniel
"""
'''
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = 2, 7, 11, 15, target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return 0, 1.
'''
#defining function sums
def sums(nums, target):
"""
:type nums: List[int]
:type target: int
:return type: List [int]
"""
#checking invalid inputs
if len(nums)==0: #empty list input
return "List is empty."
if len(nums)==1: #list input without two or more items
return "Input another number to the list"
remainder = 0 #initializing remainder
seen = {} #empty set for checking if the sum of two numbers equals target
#One Pass Solution
for i, number in enumerate(nums):
remainder = target - number #finding needed value to sum to target
if remainder in seen: #if needed value exists in seen set
return [seen[remainder], i]
else:
seen[number] = i #if needed value does not yet exist
'''Test case'''
nums = [2, 7, 11, 15]
target = 9
print ("The indices of the respective numbers are:\n", sums(nums, target))
"""
Output:
The indices of the respective numbers are:
[0, 1]
"""