Hi everyone 👋
At the end of Day 12 (Day 12 post), Muthu Sir gave us two new stories as homework. He said: "I want your attempt, not the output."
So this post is my attempt. Sir will discuss these in the next class, so my answers may change after that. 🙂
I will follow Sir's method for both:
- Understand the story first.
- Micro: solve it by hand, step by step.
- Find the 100% repeated lines.
-
Macro: keep one copy, put it in a
whileloop. - Dry run to check.
Homework 1: Tenali Raman and the Palace Guards 👑
Tenali Raman wishes to meet the King. Before reaching the royal court, he has to pass through many palace gates. A guard is posted at each gate.
Every guard demands a share of whatever reward Tenali Raman receives from the King. Tenali Raman agrees and promises: "I will give you half of whatever reward I receive from the King."
All the guards allow him to enter. After meeting the King, Tenali Raman receives a reward of 1024 lashes. 😄
Keeping his promise, Tenali Raman shares his reward with the guards. At each gate, he gives half of the lashes he still has to receive to the guard at that gate, and proceeds to the next gate.
- How many lashes does each guard receive?
- How many guards are there if Tenali Raman gets only one lash?
Step 1: Understand the story
Clever Tenali! The "reward" is a punishment, and the greedy guards get most of it. 😂
Sir said this is the reverse of the Saint's Flower Basket:
| Saint's story | Tenali's story |
|---|---|
| We know the end (1 flower) | We know the start (1024 lashes) |
| Go backwards, double each time | Go forwards, halve each time |
Rule 2: Known to Unknown. Here we know the start, so we can go forward.
Step 2: Micro (by hand)
| Gate | Lashes left before the gate | Guard gets (half) | Lashes left after |
|---|---|---|---|
| 1 | 1024 | 512 | 512 |
| 2 | 512 | 256 | 256 |
| 3 | 256 | 128 | 128 |
| 4 | 128 | 64 | 64 |
| 5 | 64 | 32 | 32 |
| 6 | 32 | 16 | 16 |
| 7 | 16 | 8 | 8 |
| 8 | 8 | 4 | 4 |
| 9 | 4 | 2 | 2 |
| 10 | 2 | 1 | 1 ✅ |
After 10 gates, Tenali has only 1 lash left for himself!
In code, the first few gates look like this:
lashes = 1024
guards = 0
half = lashes // 2 # 512
print(half)
lashes = lashes - half # 512 left
guards = guards + 1 # 1 guard
half = lashes // 2 # 256
print(half)
lashes = lashes - half # 256 left
guards = guards + 1 # 2 guards
💡 // divides and keeps only the whole number (1024 // 2 is 512, not 512.0). Lashes can't be half! 😅
Step 3: Is it 100% repeated?
Yes! These 4 lines repeat exactly the same at every gate:
half = lashes // 2
print(half)
lashes = lashes - half
guards = guards + 1
Step 4: When do we stop?
We don't know how many gates there are. But the story tells us when to stop: when Tenali has only 1 lash left. So we keep going while lashes is more than 1.
Step 5: Macro
lashes = 1024
guards = 0
while lashes > 1:
half = lashes // 2
print(half, end=' ')
lashes = lashes - half
guards = guards + 1
print()
print("Guards:", guards)
print("Tenali gets:", lashes)
Output:
512 256 128 64 32 16 8 4 2 1
Guards: 10
Tenali gets: 1
Answers
- The guards receive 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 lashes.
- There are 10 guards.
✅ Check it backwards (like the saint!)
If Tenali ends with 1 lash, how many gates until we reach 1024? Double backwards, just like the saint's flowers:
count = 1
guards = 0
while count < 1024:
count = count * 2
guards = guards + 1
print(guards)
Output: 10 ✅ (1024 is 2 to the power of 10)
Both ways give the same answer. 🎉
Homework 2: Two Trains 🚆🚆
Two trains stop at regular intervals:
- Train 1 stops at every 3rd station.
- Train 2 stops at every 5th station.
Find:
- The first common station where both trains stop.
- The last common station where both trains stop.
- All common stations where both trains stop.
Note: Total number of stations is unknown.
Step 1: Micro (Known to Unknown)
I don't know the answer. But I know where each train stops. Let me just write them:
- 🚆 Train 1 (every 3rd): 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45 ...
- 🚆 Train 2 (every 5th): 5, 10, 15, 20, 25, 30, 35, 40, 45 ...
Common stations: 15, 30, 45, ... 🤩
Step 2: Find the pattern
15, 30, 45... they go up by 15 every time! (3 × 5 = 15.)
So I don't need to check every station. Rule 5: introduce a variable only when it simplifies the solution. One variable common is enough.
Micro:
common = 15
print(common)
common = common + 15 # 30
print(common)
common = common + 15 # 45
print(common)
100% repeated! ✅
Step 3: The tricky part: "Total stations are unknown"
The first common station is always 15. That doesn't need the total.
But the last common station depends on how many stations the line has. If there are 50 stations, the last common is 45. If there are 100, it's 90. We can't know it without the total.
So I keep the total as one variable at the top. Change only that number, and the program works for any railway line:
Step 4: Macro
total_stations = 50 # change this number for any line
common = 15
print("First common station:", common)
print("All common stations:", end=' ')
while common <= total_stations:
print(common, end=' ')
common = common + 15
print()
print("Last common station:", common - 15)
Output (for 50 stations):
First common station: 15
All common stations: 15 30 45
Last common station: 45
🧠 Why common - 15 at the end?
Remember the lift button question from Day 11? After the loop ends, the variable has already gone one step too far.
Dry run:
common |
common <= 50? |
Prints |
|---|---|---|
| 15 | ✅ | 15 |
| 30 | ✅ | 30 |
| 45 | ✅ | 45 |
| 60 | ❌ | stop |
After the loop, common is 60, not 45. So the last common station is common - 15 = 45. Day 11's dry run lesson helped me here! 🙌
(If there are fewer than 15 stations, the trains never stop at the same station!)
What I Learned From This Homework
- Tenali's story is the reverse of the saint's story: halve forwards instead of doubling backwards.
- When we don't know how many times, the story tells us when to stop (
lashes > 1). - Writing the stops by hand showed me the pattern (every 15) before writing any loop.
- If something is truly unknown (total stations), keep it as a variable you can change.
- After a loop, the variable is one step past the last value. Dry run always! 🔍
Sir will discuss these in the next class. I will update this post if my answers change. See you on Day 13! 🐍
Thank you, Muthu Sir! 🙏
Top comments (0)