DEV Community

vidhya murali
vidhya murali

Posted on

Python task : looping problems

1,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.
guards=0;
lashes=1024;

while lashes>1:
    lashes//=2;
    guards+=1;
    print("Guard",guards,": lashes = ",lashes);

print("total guards ",guards);   
Enter fullscreen mode Exit fullscreen mode

2,A police officer is chasing a thief. The police officer starts at 0 meters, while the thief is 40 meters ahead. Every jump, the police officer moves 5 meters and the thief moves 2 meters ,How many jumps the police officer needs to catch the thief ?

police=0
thief=40;
jumpCount=0;

while police<thief:
    police+=5;
    thief+=2;
    jumpCount+=1;

print("total jump count",jumpCount)
print(police,  thief)
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
karthick_07 profile image
Karthick (k)

Good

Collapse
 
vidhya_murali_5aabe7784bd profile image
vidhya murali

Thank you