DEV Community

sridhar n
sridhar n

Posted on

Python Day 15 – My Homework Attempts: Chocolate Wrappers and the Chapathi Story

On Day 15 our trainer gave us two problems for homework, with these instructions:

  1. Try a dry run with pen and paper
  2. Think about how to solve the problem alone. Don't think about converting it into a program.
  3. If you feel you can, convert it into a flow chart first
  4. Once the flow chart is perfect, give the Python program a try
  5. Write your attempts in a blog post

So here are my attempts.


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?

Attempt 1

First I worked out how many chocolates the money buys, then did one round of exchanging wrappers:

money = 100

chocolate = money // 5   # ---> 20
wrapper = chocolate      # ---> 20

new = wrapper // 3
remaining = wrapper % 3
chocolate = chocolate + new
wrapper = new + remaining
print(chocolate)

new = wrapper // 3
remaining = wrapper % 3
chocolate = chocolate + new
wrapper = new + remaining
print(chocolate)
Enter fullscreen mode Exit fullscreen mode

Attempt 2

Then I kept repeating the same block to see where it stops:

money = 100

chocolate = money // 5   # ---> 20
wrapper = chocolate      # ---> 20

new = wrapper // 3
remaining = wrapper % 3
chocolate = chocolate + new
wrapper = new + remaining
print(chocolate)

new = wrapper // 3
remaining = wrapper % 3
chocolate = chocolate + new
wrapper = new + remaining
print(chocolate)

new = wrapper // 3
remaining = wrapper % 3
chocolate = chocolate + new
wrapper = new + remaining
print(chocolate)

new = wrapper // 3
remaining = wrapper % 3
chocolate = chocolate + new
wrapper = new + remaining
print(chocolate)

new = wrapper // 3
remaining = wrapper % 3
chocolate = chocolate + new
wrapper = new + remaining
print(chocolate)
Enter fullscreen mode Exit fullscreen mode

Answer

The same four lines kept repeating, so I put them in a loop that runs while I still have 3 or more wrappers:

money = 100

chocolate = money // 5   # ---> 20
wrapper = chocolate      # ---> 20

while wrapper >= 3:
    new = wrapper // 3
    remaining = wrapper % 3

    chocolate = chocolate + new

    wrapper = new + remaining

print(chocolate)
Enter fullscreen mode Exit fullscreen mode

Output: 29


Problem 2: Chapathi Story

Viyan is a Software Engineer. He employs a maid for cooking. One day, the maid prepared n chapathis and kept them in a hot box.

  • For breakfast, he ate 1/3 of the chapathis in the hot box.
  • For lunch, he had 1/3 of what was there in the hot box.
  • For dinner, again he ate 1/3 of the chapathis present in the box.

The next morning, the maid found 8 chapathis remaining. How many chapathis did she make in total?

Attempt 1

I started from the 8 remaining chapathis and worked backwards, one meal at a time:

chappathis = 8
print('Chappathies', chappathis)

meal3 = chappathis // 2
print('Meal 3 ', meal3)

chappathis = chappathis + meal3
print(chappathis)

meal2 = chappathis // 2
print('Meal 2 ', meal2)

chappathis = chappathis + meal2
print(chappathis)

meal1 = chappathis // 2
print('Meal 1 ', meal1)

chappathis = chappathis + meal1
print(chappathis)
Enter fullscreen mode Exit fullscreen mode

Attempt 2

Then I turned it into a loop that runs 3 times, once for each meal:

chappathis = 8
time = 3
while time != 0:
    meal = chappathis // 2
    print('Meal - ', time, 'he had ', meal, 'chappathies')
    chappathis = chappathis + meal
    print('There were', chappathis)
    time = time - 1
Enter fullscreen mode Exit fullscreen mode

Answer

chappathis = 8
time = 3
while time != 0:
    meal = chappathis // 2
    chappathis = chappathis + meal
    time = time - 1

print('Total Chappathies -', chappathis)
Enter fullscreen mode Exit fullscreen mode

Output: Total Chappathies - 27

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam •

Very good attempt!