-
Notifications
You must be signed in to change notification settings - Fork 0
/
sums.py
executable file
·48 lines (32 loc) · 1.33 KB
/
sums.py
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
#! /usr/bin/env python3
'''Daily Coding Problem
This was a problem recently asked by Google during an interview:
Accept a list of values and a target value. Return True if any two numbers
in the list of values add up to the target value, otherwise return False.
For example, given [10,15,3,7] and a target of 17, return True,
since 10 + 7 = 17
'''
def checkList(target,valueList):
''' checkList(Target value, list of values)
Solution presented here:
Given a target and a list of values return true if the first value
plus any of the remaining values in the list add up to the target.
Otherwise, remove the first value from the list, and call checkList()
again to check the sums of the remaining values, until the list
is empty
'''
if len(valueList) == 1:
return False
addend = valueList[0]
for i in valueList[1:]:
if (addend + i) == target:
return True
return checkList(target, valueList[1:])
listtext = input("Enter a list of numbers, separated by commas: ")
target = input("Enter a target sum: ")
values = listtext.split(",")
for i in range(len(values)):
values[i] = int(values[i])
target = int(target)
print("list = {}, target = {}".format(values, target))
print(" result is {}".format( checkList(target, values) ))