problem 35 - Savings Calculator (for Rodion Gorkovenko)

Back to Problem Solutions forum

BukChoiWarrior     2019-12-22 21:31:25

Hi,

I recently completed the Savings Calculator problem and had much trouble rounding the yearly total DOWN to the nearest penny/cent every year. After completing the task, I go to see others' solutions to compare and realise that many "correct" solutions do not deal with the rounding down. I believe that it is due to the rounding down being irrelevent until you reach very high year count so for most test-cases it will not make a difference.

One example of the problem is when using the input values as "25 75 1". - Without rounding down every year, the goal will be reached in 111 years, but WITH rounding, the goal will take 112 years.

Please could you either fix the answer checker to include one such result to force users to round down as stated in the problem, or alternatively remove the limitation.

I hope I have helped and please let me know if I was mistaken - I am only new to programming after all :)

Much appreiated, BCW

BukChoiWarrior     2019-12-22 21:38:24

It seems I have found another test case for your interest: "25 425 3" - small initial values paired with small interest rates seem to produce intended result more often!

Sorry I did not know that I could edit posts :)

Rodion (admin)     2020-01-03 11:56:48
User avatar

Hi, thanks for this research!

or alternatively remove the limitation

Well, you see, it is not "limitation" really, but a kind of clarification. Without it someone may decide that calculation are performed without rounding (though no banks work in such way), others may think values are rounded "to nearest", then some people may calculate in integer cents (effectively rounding down).

fix the answer checker to include one such result

he-he, for this I need first to find out how to do this easier :) I'll try, thanks!

UPD tried to update the checker so it creates certain number of "long period" test-cases, similar to your hint. Hopefully this works!

BukChoiWarrior     2020-01-03 16:34:43

Thanks for your response and I hope this will help newer CA members in the future :)

Warm regards,

BCW

Philosophical-Zombie     2020-03-16 21:39:02

According to the website the input '50 950 1' should yeild '297'. However I tried number of functions that use loops or math.log and I always get '296'. Have tried with rounding down to two decimal places and without. I ran a variation of the function that printed bank balance each year and I can not see where the problem is arising.

import math
def saings_calculator1(s, r, p ):
    total = s
    n = 0
    while total < r:
        total = ( total *((1+(p/100))**1))
        n += 1 
        print(total,n)
    return n
saings_calculator1(50, 950, 1)
Rodion (admin)     2020-03-16 21:56:43
User avatar

Hi Friend!

Have tried with rounding down to two decimal places and without.

Well, don't panic! :)

The problem statement says you should round down to cents. If I add rounding down to whole cents to your code, it works properly and gives 297.

So your code is mainly correct, you just should do rounding carefully. Judging by your code you was not in most careful mood - otherwise what this **1 may mean :)

If you find it difficult to round down to decimal digits after point, you can instead do calculations directly in cents rather than in dollars and round down simply with int(...).

Cheer up, you almost did it!
Feel free to ask more if more hints are needed...

UPD I see you have already passed this problem successfully, though judging by your code it doesn't seem to me pretty correct. Probably you used wrong functions for rounding. I see ceil in one place and round in other, though you really want floor to round down. It could be replaced with round(x - 0.005, 2) I think, if you want digits after the point. But calculations in cent would be easier. Just multiply input by 100, right.

e.g. fix for your code could be like this

total = int(total*(1+(p/100)) * 100) / 100

(here int and floor would be the same)

Philosophical-Zombie     2020-03-16 22:49:20

Thank you for the critique. It is the first review I have ever got. I think I must have missed that part of the spefications about round down to whole cents. I can see you point about working in cents. Thank you for the tips.

Please login and solve 5 problems to be able to post at forum