Suppose we have some parameters P,O,E,T. If we have P dollars in principal that we want to invest the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars.
So, if the input is like P = 200, O = 10, E = 25, T = 300, then the output will be 3 as in the first year we will get interest 25%, so end up with 200+50 = 250, then next year we will get 10%, so end up with 250+25 = 275, then again get 10% in the next year, so it will be 275+27.5 = 302.5, this is greater than 300, so 3 years required.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, P, O, E, T): ans=0 while P < T: P *= 1+(E/100) ans += 1 if P < T: P *= 1+(O/100) ans += 1 return ans ob = Solution() P = 200 O = 10 E = 25 T = 300 print(ob.solve(P,O,E,T))
P = 200, O = 10, E = 25, T = 300