-
Notifications
You must be signed in to change notification settings - Fork 0
/
PS2-3v1.py
86 lines (63 loc) · 2.21 KB
/
PS2-3v1.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Test Case 1:
# balance = 3329
# annualInterestRate = 0.2
#
# Result Your Code Should Generate:
# -------------------
# Lowest Payment: 310
#balance = 3329
# lowest payment = 310
#annualInterestRate = 0.2
#balance = 4773
# lowest payment = 440
#balance = 3926
# lowest payment = 360
# 0.15 ir test
balance = 320000
annualInterestRate = 0.2
# Lowest Payment: 29157.0999983
# ub - unpaid balance
# uub - updated unpaid balance
# mp = monthly payment, incremented by 10
# flag - set to true until balance is <= 0 after 12 months of payments
ub = balance
flag = True
minPay = round(balance/12,2)
maxPay = round((balance * (1+annualInterestRate/12)**12)/12,2)
# start in the middle
guessPay = (minPay+maxPay)/2
def balance_after_one_year(balance1,annualInterestRate,payment):
for _ in xrange(12):
balance1=(balance1-payment) * (1+annualInterestRate/12.00)
return balance1
leastNeg = balance_after_one_year(balance, annualInterestRate,maxPay)
leastPos = balance_after_one_year(balance, annualInterestRate, minPay)
while flag:
ub = balance_after_one_year(balance, annualInterestRate, guessPay)
# print('test ub is: ' + str(ub)+ ' and guessPay is ' + str(guessPay))
# if ub is lower than 0 and still lower than the least negative
if ub < 0 and ub > leastNeg:
leastNeg = ub
minPay = guessPay
guessPay = (minPay+maxPay)/2.00
print 'in 1st if'
# if ub is higher than 0 and still less than the least positive unpaid balance
elif ub > 0 and ub < leastPos:
leastPos = ub
maxPay = guessPay
guessPay = (minPay+maxPay)/2.00
print 'in 2nd if'
# when under 0.01 we can exit
elif (maxPay-guessPay <= 0.01) or (guessPay-minPay <= 0.01):
print('Found an answer')
flag = False
# in case I messed up
else:
print 'maxPay-guessPay = ', maxPay-guessPay
print 'minPay-guessPay = ', minPay-guessPay
print 'maxPay= ', maxPay, 'minPay= ',minPay, 'guessPay= ',guessPay
print 'ub= ', ub
print 'leastNeg= ', leastNeg
print 'leastPos= ', leastPos
flag = False
print('Lowest Payment: ' + str(guessPay))