-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.py
More file actions
31 lines (27 loc) · 1011 Bytes
/
TwoSum.py
File metadata and controls
31 lines (27 loc) · 1011 Bytes
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
def main():
"""
This program allows to identify position of two numbers in a given
list that when added together fulfills the target sum
e.g. input numbers 2, 7, 8, 9 and
for target sum 15 expected result is 1 , 2
for target sum 17 expected result is 2 , 3
T(n) = O(n)
"""
#accept user input for the list of numbers
input_nums = input("Enter comma separated list of numbers: ")
num = eval(input_nums)
#accept user input for the target sum
input_target = input("Enter target sum: ")
target = int(input_target)
#define empty dictionary object
dic = {}
#loop through each number, subtract target with indexed num
# to identify other num to complete the target sum
for i in range(0, len(num)):
if dic.get(target-num[i]) == None:
dic.update({num[i] : i})
else:
print(dic.get(target-num[i]), ',', i)
break
if __name__ == "__main__":
main()