DEV Community

Mukesh
Mukesh

Posted on Edited on

Simple logic problem

1) Chocolate Wrapper Problem
You have Rs. 100 /- One chocolate costs Rs. 5. Every time you buy a chocolate, you get one wrapper. You can exchange 3 wrappers for 1 additional chocolate. What is the maximum number of chocolates you can have with Rs. 100?

ANS:

rs=100
chocolate=rs//5
wraper=3
subwrap=chocolate
while subwrap>=wraper:
newchoco=subwrap//wraper
chocolate+=newchoco
subwrap=newchoco+(subwrap%wraper)

print(chocolate)

Explanation :

Line 1 : You have a total rs of 100
Line 2 : Per chocolate cost rs=5 so that you can buy 20 chocolate by dividing 100 by 5 and uses // to get a whole number without a decimal value.
Line 3 : In exchange of 3 Wrapper you can get a chocolate
Line 4 : so if we buy some amount like 20 chocolate we assign that to subwrap variable to further processing
Line 5: while subwrap>=wraper: starts a loop that keeps running as long as you have at least 3 wrappers available to exchange.
Line 6: newchoco=subwrap//wraper calculates how many new whole chocolates you can get by dividing your current wrappers by 3.
Line 7: chocolate+=newchoco adds those newly earned chocolates to your total chocolate count.
Line 8: subwrap=newchoco+(subwrap%wraper) updates your wrapper count by adding the new wrappers from the fresh chocolates to any leftover wrappers that could not be traded.
Line 9: print(chocolate) displays the final number of chocolates you collected once you have fewer than 3 wrappers left.

Top comments (0)