Halloween Sale
--
Problem:-
Solution:-
A straight forward hackerrank problem where we have to calculate the number of gifts one can buy with given amount.
Lets move toward the code,we will have to declare two variables n and count with intially value n=p and count =0.
def howManyGames(p, d, m, s):
n=p
count=0
while n<=s:
p=p-d
if p<=m:
n+=m
count+=1
else:
n+=p
count+=1
return count
We will use a while loop which will run a long as the value of n is less than or equal to s. ”n” will hold the cost of each gift after the discount has been deducted.After purchasing the first gift without any discount ,we will deduct d from p and store it again in p. Then we will check whether the value of p is less than or equal to m,as per the condition give if this condition is statisfy we will add m to n and increment the counter by.In the else-part we will add p to n and increment the counter by 1.As the loop runs out we will return count which will hold the number of gifts we have purchased in the given pocket money.
Conclusion:-
“ If you learn self-control, you can master anything.”
quote which sums up todays time where exercising self control has become so difficult.Keep learning and Keep hustling…(Cheers:-PN)