DEV Community

Keerti Sadana S
Keerti Sadana S

Posted on Edited on

PYTHON TASKS - LOOPS4

12)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.

Write a program to determine: சவுக்கடி

  1. How many lashes each guard receives.
  2. How many Guards are there if Tenali Raman gets only one lash.
lashes = 1024
guard = 0
while lashes > 1:
    lashes//=2
    guard += 1
    print('Guard',guard,'=',lashes)

print('Total number of guards:',guard)
Enter fullscreen mode Exit fullscreen mode

Output:
Guard 1 = 512
Guard 2 = 256
Guard 3 = 128
Guard 4 = 64
Guard 5 = 32
Guard 6 = 16
Guard 7 = 8
Guard 8 = 4
Guard 9 = 2
Guard 10 = 1
Total number of guards: 10

13)Thief, Police Running

police = 0
step = 0
thief = 40
while police<thief:
    police += 5
    thief += 2
    step += 1
print(police)
print('Total number of steps to catch the thief: ',step)
Enter fullscreen mode Exit fullscreen mode

Output:
70
Total number of steps to catch the thief: 14

Top comments (0)