Skip to content

Instantly share code, notes, and snippets.

@david-sosa-valdes
Created April 1, 2016 08:18
Show Gist options
  • Save david-sosa-valdes/a2df05cc3009824dd0ff2b6382f81f86 to your computer and use it in GitHub Desktop.
Save david-sosa-valdes/a2df05cc3009824dd0ff2b6382f81f86 to your computer and use it in GitHub Desktop.
Problema de finanza #1
import unittest
"""
Implement a class Finances, which contains a function timeLeft. timeLeft is
passed three int values: initialValue, interest, monthly.
* The initial value of the bank account is initialValue.
* Interest is compounded monthly at a rate of interest percent.
* Every month monthly dollars is taken out from the account (immediately after
the Interest has been added).
* timeLeft must return an int that is the number of months the account can
support the full monthly withdrawal.
* If the account will last longer than 1200 months, the function should return 1200.
"""
class Finances(object):
def __init__(self, debug = False):
self.debug = debug
def _validate(self, value, minLim, maxLim):
return minLim if value < minLim else maxLim if value > maxLim else value
def timeLeft(self, initialValue, interest, monthly):
initialValue = self._validate(initialValue, 0, 100000)
interest = self._validate(interest, 0, 1000)
monthly = self._validate(monthly, 0, 100000)
months = 0
while initialValue > 0:
if self.debug != False:
print months, initialValue, interest, monthly
initialValue -= monthly + (monthly * (float(interest)/100))
if initialValue < 0:
break
months+=1
return months if months < 1200 else 1200
class TestFinancialCases(unittest.TestCase):
def test_first(self):
f = Finances()
self.assertEqual(1, f.timeLeft(100,0,100))
def test_second(self):
f = Finances()
self.assertEqual(0, f.timeLeft(200,1,300))
def test_third(self):
f = Finances()
self.assertEqual(1200, f.timeLeft(3452, 8, 200))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment