-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfinancialtools.py
89 lines (79 loc) · 3.46 KB
/
financialtools.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
87
88
89
'''
PROBLEM 1: PAYING THE MINIMUM
Write a program to calculate the credit card balance after one year if a person only pays the minimum monthly payment required by the credit card company each month.
'''
#balance - the outstanding balance on the credit card
#annualInterestRate - annual interest rate as a decimal
#monthlyPaymentRate - minimum monthly payment rate as a decimal
def minpayment(balance, annualInterestRate, monthlyPaymentRate):
totalPaid = 0
month = 1
while month <= 12:
minPayment = monthlyPaymentRate * balance #Compute the monthly payment, based on the previous month's balance.
balance -= minPayment #Update the outstanding balance by removing the payment,
balance += (annualInterestRate/12.0)*balance #then charging interest on the result.
print 'Month:',month #Output the month,
print 'Minimum monthly payment:',round(minPayment,2) #the minimum monthly payment
print 'Remaining balance:',round(balance,2) #and the remaining balance.
totalPaid += minPayment #Keep track of the total amount of paid over all the past months so far.
month += 1
print 'Total paid:', round(totalPaid,2) #Print out the result statement with the total amount paid
print 'Remaining balance:', round(balance,2) #and the remaining balance.
'''
PROBLEM 2: PAYING DEBT OFF IN A YEAR
Now write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months.
By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.
'''
fixedPayment1 = 0
searching = True
def adequatePayment(balance,annualInterestRate,fixedPayment):
'''
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
'''
month = 1
while month <= 12:
balance -= fixedPayment1 #Update the outstanding balance by removing the payment,
balance += (annualInterestRate/12.0)*balance #then charging interest on the result.
month += 1
if balance <= 0:
return fixedPayment1
else:
return False
while searching == True:
if adequatePayment(balance,annualInterestRate,fixedPayment1):
searching = False
print 'Lowest Payment:',adequatePayment(balance,annualInterestRate,fixedPayment1)
else:
fixedPayment1 += 10
adequatePayment(balance,annualInterestRate,fixedPayment1)
'''
PROBLEM 3: USING BISECTION SEARCH TO MAKE THE PROGRAM FASTER
'''
monthlyInterestRate = annualInterestRate/12.0
high = (balance * (1 + monthlyInterestRate)**12)/12.0
low = balance/12
fixedPayment2 = (high + low)/2
searching = True
def adequatePayment(balance,monthlyInterestRate,fixedPayment2):
'''
balance - the outstanding balance on the credit card
monthlyInterestRate - monthly interest rate as a decimal
'''
month = 1
while month <= 12:
balance -= fixedPayment2 #Update the outstanding balance by removing the payment,
balance += monthlyInterestRate*balance #then charging interest on the result.
month += 1
return balance
while searching:
z = adequatePayment(balance,monthlyInterestRate,fixedPayment2)
if round(z) < 0:
high = fixedPayment2
fixedPayment2 = (high + low)/2
elif round(z) > 0:
low = fixedPayment2
fixedPayment2 = (high + low)/2
elif round(z) == 0:
searching = False
print 'Lowest Payment:',round(fixedPayment2,2)