DEV Community

Sajin Joe J
Sajin Joe J

Posted on Edited on

Python : Chocolate Wrapper Problem

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?

*# step 1 : *

amount = 100
chocolate_amount = 5  #chocolate cost
chocolate = amount // chocolate_amount  

print (chocolate) #20
Enter fullscreen mode Exit fullscreen mode

*# step 2 : *

amount = 100
chocolate_amount = 5          #chocolate cost
chocolate = amount // chocolate_amount
wrapper = chocolate        #one chocolate -> one wrapper

extra_chocolate = wrapper // 3     #3 wrapper -> one chocolate
wrapper = wrapper % extra_chocolate    #remainder (remaining wrapper)
chocolate = chocolate + extra_chocolate       #total


print (chocolate)  #Dispaly total
Enter fullscreen mode Exit fullscreen mode

*# step 3 : *

amount = 100
chocolate_amount = 5
chocolate = amount // chocolate_amount
wrapper = chocolate 

while wrapper >= 3 : 
    extra_chocolate = wrapper // 3
    wrapper = wrapper % 3 + extra_chocolate
    chocolate = chocolate + extra_chocolate


print ("Total Chocolate", chocolate)
Enter fullscreen mode Exit fullscreen mode

Output:

Total Chocolate 29
Enter fullscreen mode Exit fullscreen mode

Top comments (0)