DEV Community

Gowtham
Gowtham

Posted on

Python - Scenario based problem solving programs

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?

Solution:

i) Problem solving using pen and paper

ii) Code

money = 100
chocolate_cost = 5
wrapper_exchange = 3

#Buy chocolates initially
chocolates = money // chocolate_cost

#Initially, every chocolate gives one wrapper
wrappers = chocolates

total_chocolates = chocolates

#Exchange wrappers for additional chocolates
while wrappers >= wrapper_exchange:
    new_chocolates = wrappers // wrapper_exchange

    total_chocolates += new_chocolates

    #Calculate remaining wrappers
    wrappers = (wrappers % wrapper_exchange) + new_chocolates

print("Total Chocolates:", total_chocolates)
Enter fullscreen mode Exit fullscreen mode

Output:

Total Chocolates: 29

2) Chapathi Story

There is one person called Viyan. He is a Software Engineer. He employs a maid for cooking. One day, the maid prepared n number of chapathis and kept in a hot box. Viyan ate those chapathis and the count is as follows:
* For Morning breakfast, he completed 1/3 number of chapathis in the hot box.
* For Lunch, He had 1/3 of what was there in the hot box.
* For Dinner, again he took and ate 1/3 number of chapathis present in the box.

The next day morning, When the maid checked the number of chapathis remaining, it was 8. What was the number of chapathis, She made - in total?

Solution:

i) Problem solving using pen and paper

ii) Code

remaining = 8
n = 1

while True:
    chapathis = n

    # Morning breakfast
    chapathis = chapathis - (chapathis // 3)

    # Lunch
    chapathis = chapathis - (chapathis // 3)

    # Dinner
    chapathis = chapathis - (chapathis // 3)

    if chapathis == remaining:
        print("Total Chapathis:", n)
        break

    n += 1
Enter fullscreen mode Exit fullscreen mode

Output:

Total Chapathis: 27

Top comments (0)