Skip to content

Instantly share code, notes, and snippets.

@caryfitzhugh
Created May 11, 2012 01:00
Show Gist options
  • Save caryfitzhugh/2656860 to your computer and use it in GitHub Desktop.
Save caryfitzhugh/2656860 to your computer and use it in GitHub Desktop.
problem 3
balance = float (raw_input ('What is your current balance? '))
interest = float (raw_input ('What is your interest rate? '))
monthly_interest_rate = interest / 12.0
# Monthly payment lower bound = Balance / 12.0
# Monthly payment upper bound = (Balance * (1 + (Annual interest rate / 12.0)) ** 12.0) / 12.0
lower_bound = balance / 12.0
upper_bound = ( balance * ( 1 + (monthly_interest_rate)) ** 12.0 ) / 12.0
print upper_bound, lower_bound
exit
# Here you want to have two loops.
# One loop on the outside which keeps trying until it finds a good value
amount_paid_off_over_12_months = 0
# While we're still looking for the answer
still_looking_for_answer = True
while still_looking_for_answer:
# Now we'll keep looping and trying different calculations
# Set this to a 0 to 'start over' for this try
amount_paid_off_over_12_months = 0
# This is the monthly payment we'll aim for, lower_bound + our delta.
# Here is how much more than the minimum payment we'll try for on this run
# Start with 1/2 the range (pick the number in the middle)
monthly_payment = ((upper_bound - lower_bound) / 2.0) + lower_bound
# Make a copy of balance so we don't stomp on it and change it
# as we calculate how much we'd pay off with this monthly payment
this_runs_balance = balance
print "Guessing ", monthly_payment, " this time"
# THis is the same code as problem1 !!
# Calculates how much $$ was paid off in a year at this monthly_payment amount
for month in range(0,12):
interest_paid = monthly_interest_rate * this_runs_balance
principal_paid = monthly_payment - interest_paid
this_runs_balance = this_runs_balance - principal_paid
# Now save this value -- it's how much we'd pay off with this monthly payment
amount_paid_off_over_12_months = balance - this_runs_balance
# So now we finished our calculation.
# If everything went ok, we won't repeat this (the while loop above will not continue)
# If everything didn't go ok, we'll repeat.
#1 / 0
# Cool stuff -- keep making this number smaller (even 0, and you'll just get closer and closer
# And it'll take longer and longer!
if abs( amount_paid_off_over_12_months - balance) < 1:
# If we paid off the right amount.
still_looking_for_answer = False
# Print out the monthly payment value here
# And the amount_paid_off_over_12_months
print ('Monthly payment for pay off in 1 year '), (round(monthly_payment, 2))
print ('Remaining balance:'), (balance - amount_paid_off_over_12_months)
else:
# Things didn't go so well.
# So we'll adjust the range and lower_bound.
#
if amount_paid_off_over_12_months > balance:
# means we paid too much.
# make the upper bound now our current guess
upper_bound = monthly_payment
print "We guessed too much, trying less -- upper is now", upper_bound
else:
# We paid too little
# Bring up the lower bound, squeezing the right number in the middle
lower_bound = monthly_payment
print "We guessed too little, trying more -- lower is now" , lower_bound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment