forked from matsci/python-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq2.py
More file actions
40 lines (39 loc) · 1.24 KB
/
Copy pathq2.py
File metadata and controls
40 lines (39 loc) · 1.24 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
# ONLY EDIT FUNCTIONS MARKED CLEARLY FOR EDITING
#compute all combinations for two portfolios
def question02(cashFlowIn, cashFlowOut):
# modify and then return the variable below
answer = 1000
if (len(cashFlowIn) > 200 and len(cashFlowOut) > 200):
return 0
else:
cashFlowIn = sorted(cashFlowIn, reverse=True)
cashFlowOut = sorted(cashFlowOut, reverse=True)
maxList = cashFlowIn if (len(cashFlowIn) > len(cashFlowOut)) else cashFlowOut
minList = cashFlowIn if (maxList == cashFlowOut) else cashFlowOut
for i in range(len(minList)):
tmp1 = minList[i:]
tmp2 = maxList
j = i
sumList1, sumList2 = [tmp1[0]], [tmp2[0]]
tmp1 = tmp1[1:]
tmp2 = tmp2[1:]
t = True
while(tmp1 != [] and tmp2 != []):
s1,s2 = sum(sumList1), sum(sumList2)
if (s1 > s2):
if (s1-s2 in tmp2):
return 0
else:
sumList2 += [tmp2[0]]
answer = min(answer, abs(s1-s2-tmp2[0]))
tmp2 = tmp2[1:]
elif (s1 < s2):
if (s2-s1 in tmp1):
return 0
else:
sumList1 += [tmp1[0]]
answer = min(answer, abs(s2-s1-tmp1[0]))
tmp1 = tmp1[1:]
else:
return 0
return answer