Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 19, 2017 16:52
Show Gist options
  • Save tyler-austin/bb1f56f38b603dd292015426ca800b9c to your computer and use it in GitHub Desktop.
Save tyler-austin/bb1f56f38b603dd292015426ca800b9c to your computer and use it in GitHub Desktop.
Code Fights - depositProfit

You have deposited a specific amount of dollars into your bank account. Each year your balance increases at the same growth rate. Find out how long it would take for your balance to pass a specific threshold with the assumption that you don't make any additional deposits.

Example

For deposit = 100, rate = 20 and threshold = 170, the output should be
depositProfit(deposit, rate, threshold) = 3.

Each year the amount of money on your account increases by 20%. It means that throughout the years your balance would be:

  • year 0: 100;
  • year 1: 120;
  • year 2: 144;
  • year 3: 172,8.

Thus, it will take 3 years for your balance to pass the threshold, which is the answer.

Input/Output

  • [time limit] 4000ms (py3)

  • [input] integer deposit

    The initial deposit as a positive integer.

    Guaranteed constraints:

    1 ≤ deposit ≤ 100.
    
  • [input] integer rate

    The rate of increase. Each year the balance increases by the rate percent of the current sum.

    Guaranteed constraints:

    1 ≤ rate ≤ 100.
    
  • [input] integer threshold

    The target balance.

    Guaranteed constraints:

    deposit < threshold ≤ 200.
    
  • [output] integer

    The number of years it would take to hit the threshold.

import math
def deposit_profit(deposit, rate, threshold):
return math.ceil((math.log(threshold) - math.log(deposit))/(math.log(1 + (rate / 100))))
import unittest
from deposit_profit import deposit_profit
class TestDepositProfit(unittest.TestCase):
def test_1(self):
deposit = 100
rate = 20
threshold = 170
solution = 3
result = deposit_profit(deposit, rate, threshold)
self.assertEqual(result, solution)
def test_2(self):
deposit = 100
rate = 1
threshold = 101
solution = 1
result = deposit_profit(deposit, rate, threshold)
self.assertEqual(result, solution)
def test_3(self):
deposit = 1
rate = 100
threshold = 64
solution = 6
result = deposit_profit(deposit, rate, threshold)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment