View difference between Paste ID: kDNtBkAb and PRzAGyTw
SHOW: | | - or go back to the newest paste.
1-
"""
1+
"""
2-
Problem #1
2+
Problem #1
3-
"""
3+
"""
4-
4+
5-
balance = 42
5+
balance = 42
6-
annualInterestRate = 0.2
6+
annualInterestRate = 0.2
7-
monthlyPaymentRate = 0.04
7+
monthlyPaymentRate = 0.04
8-
8+
9-
def cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate):
9+
def cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate):
10-
    """
10+
    """
11-
    
11+
    
12-
    Write a program to calculate the credit card balance after
12+
    Write a program to calculate the credit card balance after
13-
    one year if a person only pays the minimum monthly
13+
    one year if a person only pays the minimum monthly
14-
    payment required by the credit card company each month.
14+
    payment required by the credit card company each month.
15-
    
15+
    
16-
    """
16+
    """
17-
    Monthly_Int_Rate = annualInterestRate / 12
17+
    Monthly_Int_Rate = annualInterestRate / 12
18-
    Min_Monthly_Pmt = monthlyPaymentRate * balance
18+
    Min_Monthly_Pmt = monthlyPaymentRate * balance
19-
    Monthly_Unpaid_Bal = balance - Min_Monthly_Pmt
19+
    Monthly_Unpaid_Bal = balance - Min_Monthly_Pmt
20-
    Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
20+
    Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
21-
    if hasattr(cc_12mo_bal, "count"):
21+
    if hasattr(cc_12mo_bal, "count"):
22-
        cc_12mo_bal.count += 1
22+
        cc_12mo_bal.count += 1
23-
    else:
23+
    else:
24-
        cc_12mo_bal.count = 1
24+
        cc_12mo_bal.count = 1
25-
    if cc_12mo_bal.count == 12:
25+
    if cc_12mo_bal.count == 12:
26-
        return print("Remaining balance: " + str(round(Updated_bal, 2)))
26+
        return print("Remaining balance: " + str(round(Updated_bal, 2)))
27-
    else:
27+
    else:
28-
        return cc_12mo_bal(Updated_bal, annualInterestRate, monthlyPaymentRate)
28+
        return cc_12mo_bal(Updated_bal, annualInterestRate, monthlyPaymentRate)
29-
29+
30-
cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate)
30+
cc_12mo_bal(balance, annualInterestRate, monthlyPaymentRate)
31-
31+
32-
"""
32+
"""
33-
Problem #2
33+
Problem #2
34-
"""
34+
"""
35-
35+
36-
def cc_12mo_payoff(balance, annualInterestRate, \
36+
def cc_12mo_payoff(balance, annualInterestRate, \
37-
                   increment = 10, epsilon = 10, \
37+
                   increment = 10, epsilon = 10, \
38-
                   guess = 10, \
38+
                   guess = 10, \
39-
                   num_guesses = 12):
39+
                   num_guesses = 12):
40-
    """
40+
    """
41-
    
41+
    
42-
    Write a program that calculates the minimum fixed monthly payment 
42+
    Write a program that calculates the minimum fixed monthly payment 
43-
    needed in order pay off a credit card balance within 12 months. 
43+
    needed in order pay off a credit card balance within 12 months. 
44-
    By a fixed monthly payment, we mean a single number which does 
44+
    By a fixed monthly payment, we mean a single number which does 
45-
    not change each month, but instead is a constant amount that will 
45+
    not change each month, but instead is a constant amount that will 
46-
    be paid each month.
46+
    be paid each month.
47-
    
47+
    
48-
    """
48+
    """
49-
49+
50-
    def pmt_trial(balance, annualInterestRate, num_guesses, \
50+
    def pmt_trial(balance, annualInterestRate, num_guesses, \
51-
                  guess = round((balance*(1+annualInterestRate/12)**12)*.0833333,-1)):
51+
                  guess = round((balance*(1+annualInterestRate/12)**12)*.0833333,-1)):
52-
        Monthly_Int_Rate = annualInterestRate / 12
52+
        Monthly_Int_Rate = annualInterestRate / 12
53-
        Monthly_Unpaid_Bal = balance - guess
53+
        Monthly_Unpaid_Bal = balance - guess
54-
        Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
54+
        Updated_bal = Monthly_Unpaid_Bal + (Monthly_Int_Rate * Monthly_Unpaid_Bal)
55-
        if num_guesses == 1:
55+
        if num_guesses == 1:
56-
            return Updated_bal
56+
            return Updated_bal
57-
        else:
57+
        else:
58-
            return pmt_trial(Updated_bal, annualInterestRate, num_guesses - 1, guess)
58+
            return pmt_trial(Updated_bal, annualInterestRate, num_guesses - 1, guess)
59-
    
59+
    
60-
    Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
60+
    Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
61-
    
61+
    
62-
    if Updated_bal <= 0:
62+
    if Updated_bal <= 0:
63-
        print("Lowest Payment: " + str(guess))
63+
        print("Lowest Payment: " + str(guess))
64-
    else:
64+
    else:
65-
        return cc_12mo_payoff(balance, annualInterestRate, guess = guess + increment)
65+
        return cc_12mo_payoff(balance, annualInterestRate, guess = guess + increment)
66-
66+
67-
cc_12mo_payoff(balance, annualInterestRate)
67+
cc_12mo_payoff(balance, annualInterestRate)
68-
68+
69-
69+
70-
"""
70+
"""
71-
Problem #3
71+
Problem #3
72-
"""
72+
"""
73-
73+
74-
def cc_12mo_payoff(balance, annualInterestRate, lower_bound = 0, \
74+
def cc_12mo_payoff(balance, annualInterestRate, lower_bound = 0, \
75-
                   upper_bound = 0, guess = 10, num_guesses = 12, epsilon = .01):
75+
                   upper_bound = 0, guess = 10, num_guesses = 12, epsilon = .01):
76-
    """
76+
    """
77-
    
77+
    
78-
    Write a program that uses these bounds and bisection search (for more info
78+
    Write a program that uses these bounds and bisection search (for more info
79-
    check out the Wikipedia page on bisection search) to find the smallest 
79+
    check out the Wikipedia page on bisection search) to find the smallest 
80-
    monthly payment to the cent (no more multiples of $10) such that we can 
80+
    monthly payment to the cent (no more multiples of $10) such that we can 
81-
    pay off the debt within a year. Try it out with large inputs, and notice 
81+
    pay off the debt within a year. Try it out with large inputs, and notice 
82-
    how fast it is (try the same large inputs in your solution to Problem 2 to
82+
    how fast it is (try the same large inputs in your solution to Problem 2 to
83-
    compare!). Produce the same return value as you did in Problem 2.
83+
    compare!). Produce the same return value as you did in Problem 2.
84-
84+
85-
    """
85+
    """
86-
    Monthly_Int_Rate = annualInterestRate / 12
86+
    Monthly_Int_Rate = annualInterestRate / 12
87-
    
87+
    
88-
    if lower_bound == 0 and upper_bound == 0:
88+
    if lower_bound == 0 and upper_bound == 0:
89-
        lower_bound = balance / 12
89+
        lower_bound = balance / 12
90-
        upper_bound = balance * ((1+Monthly_Int_Rate)**12)/12.0
90+
        upper_bound = balance * ((1+Monthly_Int_Rate)**12)/12.0
91-
        guess = (upper_bound + lower_bound) / 2
91+
        guess = (upper_bound + lower_bound) / 2
92-
    else:
92+
    else:
93-
        guess = (upper_bound + lower_bound) / 2
93+
        guess = (upper_bound + lower_bound) / 2
94-
        
94+
        
95-
    def pmt_trial(balance, annualInterestRate, num_guesses, guess):
95+
    def pmt_trial(balance, annualInterestRate, num_guesses, guess):
96-
        Monthly_Unpaid_Bal = balance - guess
96+
        Monthly_Unpaid_Bal = balance - guess
97-
        Updated_bal = Monthly_Unpaid_Bal + \
97+
        Updated_bal = Monthly_Unpaid_Bal + \
98-
        (Monthly_Int_Rate * Monthly_Unpaid_Bal)
98+
        (Monthly_Int_Rate * Monthly_Unpaid_Bal)
99-
        if num_guesses == 1:
99+
        if num_guesses == 1:
100-
            return Updated_bal
100+
            return Updated_bal
101-
        else:
101+
        else:
102-
            return pmt_trial(Updated_bal, annualInterestRate, \
102+
            return pmt_trial(Updated_bal, annualInterestRate, \
103-
                             num_guesses - 1, guess)
103+
                             num_guesses - 1, guess)
104-
    
104+
    
105-
    Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
105+
    Updated_bal = pmt_trial(balance, annualInterestRate, num_guesses, guess)
106-
    
106+
    
107-
    if Updated_bal <= 0 and Updated_bal >= -0.01:
107+
    if Updated_bal <= 0 and Updated_bal >= -0.01:
108-
        print("Lowest Payment: " + str(round(guess,2)))
108+
        print("Lowest Payment: " + str(round(guess,2)))
109-
    elif Updated_bal > 0:
109+
    elif Updated_bal > 0:
110-
        return cc_12mo_payoff(balance, annualInterestRate, 
110+
        return cc_12mo_payoff(balance, annualInterestRate, 
111-
                              lower_bound = guess, upper_bound = upper_bound)
111+
                              lower_bound = guess, upper_bound = upper_bound)
112-
    else:
112+
    else:
113-
        return cc_12mo_payoff(balance, annualInterestRate, \
113+
        return cc_12mo_payoff(balance, annualInterestRate, \
114-
                              lower_bound = lower_bound, upper_bound = guess)
114+
                              lower_bound = lower_bound, upper_bound = guess)
115-
115+
116
cc_12mo_payoff(999999, 0.18)