DEV Community

Subash
Subash

Posted on

Python | Looping Problems

1. Chocolate Wrapper Problem

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?

Flowchart

See the first flowchart below.

Algorithm

  1. Set the total money as 100.
  2. Set the chocolate price as 5.
  3. Calculate the number of chocolates we can buy.
  4. Store the number of chocolates in a variable called total_chocolates.
  5. Store the same number in wrappers, because each chocolate gives one wrapper.
  6. Use a while loop to check whether we have at least 3 wrappers.
  7. If the condition is true:
    • Exchange 3 wrappers for 1 chocolate.
    • Add 1 to the total chocolates.
    • Add the new chocolate's wrapper.
  8. Repeat the process until fewer than 3 wrappers remain.
  9. Print the total number of chocolates.

Python Program

total_money = 100
chocolate_price = 5

total_chocolates = total_money // chocolate_price
wrappers = total_chocolates

while wrappers >= 3:
    wrappers -= 3
    total_chocolates += 1
    wrappers += 1

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

Output

Total chocolates: 29
Enter fullscreen mode Exit fullscreen mode

Explanation

First, we use ₹100 to buy 20 chocolates because each chocolate costs ₹5.

Now we have 20 wrappers. We can exchange 3 wrappers for 1 extra chocolate.

Whenever we exchange wrappers, we receive another wrapper from the new chocolate. So, the loop continues as long as we have at least 3 wrappers.

Finally, when we have fewer than 3 wrappers, the loop stops.

Therefore, the total number of chocolates we get is 29.


2. Chapathi Story

Problem

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?

Flowchart

See the second flowchart below.

Algorithm

  1. Initialize remaining_chapathi = 8.
  2. Initialize count = 0.
  3. Use a while loop to repeat the calculation 3 times.
  4. Since Viyan eats one-third and leaves two-thirds, we need to reverse the calculation.
  5. Calculate the previous quantity by multiplying the remaining chapathis by 3 and dividing by 2.
  6. Increase the count by 1.
  7. Repeat until the count reaches 3.
  8. Print the original number of chapathis.

Python Program

remaining_chapathi = 8
count = 0

while count < 3:
    remaining_chapathi = remaining_chapathi * 3 // 2
    count += 1

print("Total chapathis:", remaining_chapathi)

Enter fullscreen mode Exit fullscreen mode

Output

Total chapathis: 27
Enter fullscreen mode Exit fullscreen mode

Explanation

Here, we know that 8 chapathis are left after breakfast, lunch and dinner.

Instead of calculating forward, we calculate backwards.

After each meal, Viyan leaves two-thirds of the chapathis. So, to find the previous quantity, we multiply the remaining chapathis by 3 and divide by 2.

We repeat this calculation three times because there were three meals.

  • Before dinner: 8 × 3 / 2 = 12
  • Before lunch: 12 × 3 / 2 = 18
  • Before breakfast: 18 × 3 / 2 = 27

Therefore, the maid originally prepared 27 chapathis.


Conclusion

In the chocolate problem, we use a while loop to repeat the wrapper exchange until the condition becomes false.

In the chapathi problem, we use a loop to calculate the original quantity by working backwards.

Top comments (0)