forked from benjamin-korobkin/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_zero_sum.py
More file actions
55 lines (47 loc) · 2.12 KB
/
find_zero_sum.py
File metadata and controls
55 lines (47 loc) · 2.12 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
52
53
54
55
# Check the list to find numbers whose sum is 0
def find0(listA):
if 0 in listA:
return("We have a sum! It's just 0!")
for num in listA:
if (num * -1) in listA:
return("We have a sum! " + str(num) + " + " + str((num * -1)) + " = 0")
listB = []
for num in listA:
listB.append(listA[0])
listA = listA[1:] ## listA loses its first element.
if sum(listB) == 0 or -(sum(listB)) in listA:
print("Sum found!", end = ' ')
for num in listB:
print(str(num) + " +", end=' ')
return ("" + str(-sum(listB)) + " = 0")
return ("No sum found :(")
## Omg, find sum? NP!
nums = int(input("How many ints? "))
myList = []
while nums > 0:
userInt = int(input("Int? "))
myList.append(userInt)
nums -= 1
## We now have all the user's input in myList
print (find0(myList))
'''
Place all the user's input into a list called listA.
Strategy: First check if the list contains a 0. If so, just return the zero
and we're done.
Then, check if the list contains two oppostie values.
For example, 1 and -1, 2 and -2, 30 and -30. These are pairs of opposite
numbers which, when added together, sum up to 0.
If neither of these conditions are met, create a new empty list, called listB.
Start by adding the first element from listA to listB. (Be sure to remove
that element from listA.) Then, create a loop to check if listA contains the
opposite value of the sum of what's in listB. At the end of each loop
iteration, take out the first number from listA and append it to listB.
Keep going, all the while checking if listA contains a value which is the
opposite of the sum of what's inside listB. The loop will end when:
1) the sum of listB adds up to 0
2) listA contains an opposite value of the sum of listB
3) We've exhausted the contents of listA
If one of the first two conditions are met, we have successfully found a sum
in listA that adds up to 0. Otherwise, condition 3) tells us that there is no
sum that adds up to 0.
'''