티스토리 뷰

Codesignal

code signal - 31. depositProfit

터보건 2020. 3. 1. 22:46

Question

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


Example

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

    Each year the amount of money in your account increases by 20%. So 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, so the answer is 3

Input/Output

  • [execution time limit] 4 seconds (py3)

  • [input] integer deposit

    The initial deposit, guaranteed to be 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.

MY_ANSWER

def depositProfit(deposit, rate, threshold):
    i = 0
    while threshold > deposit:
        deposit = deposit + deposit*(rate/100)
        i += 1
    return i
  • deposit이 rate만큼 이자율을 가질때 복리로 threshold까지 얼마나 걸릴지를 반환하는 문제
  • while 구문을 이용하여 deposit이 threshold보다 커지기 전까지 반복

Best_ANSWER

def depositProfit(deposit, rate, threshold):

    return math.ceil(math.log(threshold/deposit, 1+rate/100))
  • threshold < deposit(1+rate/100)n 의 수식에서 n에 대한 값을 구하도록 return 값을 구현하였다.
  • threshold/deposit < (1+rate/100)n -> log(threshold/deposit) / log(1+rate/100) < n

'Codesignal' 카테고리의 다른 글

code signal - 32. absoluteValuesSumMinimization  (0) 2020.03.01
code signal - 29. chessBoardCellColor  (0) 2020.02.23
code signal - 28. alphabeticShift  (0) 2020.02.23
code signal - 27. variableName  (0) 2020.02.23
code signal - 26. evenDigitsOnly  (0) 2020.02.23
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함