DEV Community

Raj
Raj

Posted on

Python looping task

1.Tenali Raman promises to share his reward with the guards at each gate. At every gate, he gives half of the remaining lashes to the guard and proceeds to the next gate.

The process continues until only one lash remains. Tenali Raman receives that final one lash.

We need to write a Python program to find:

  • How many lashes each guard receives.

  • How many guards are there in total.

Get the Input

lashes = int(input("Enter total number of lashes: "))
The program asks the user to enter the total number of lashes. The entered value is stored in the lashes variable.

Initialize the Guard Count

guard = 0
Initially, the guard count is zero because no guard has received any lashes yet.

Check the While Loop

while lashes > 1:
The while loop checks whether the remaining lashes are greater than If the condition is true, the loop continues.

Calculate the Lashes

received = lashes // 2
The `// operator performs floor division. It divides the remaining lashes by 2 and gives the whole-number result.
**
Increase the Guard Count**

python
guard += 1

Every time a guard receives lashes the guard count increases by

Display the Guard's Lashes

python
print("Guard", guard, "receives", received, "lashes")
This statement displays the guard number and the number of lashes received.
**
Update the Remaining Lashes**

python
lashes = lashes - received
After giving half of the lashes to the current guard, the program subtracts the received lashes from the total remaining lashes.
**
Display the Final Result**

python
print("Total number of guards:", guard)
print("Tenali Raman receives:", lashes, "lash")

When only one lash remains, the loop stops. The program displays the total number of guards and the final lash received by Tenali Raman.

Program Execution – Example

Input:

text
Enter total number of lashes: 16

Slashes = int(input("Enter total number of lashes: "))

guard = 0

`python
while lashes > 1:
received = lashes // 2

guard += 1

print("Guard", guard, "receives", received, "lashes")

lashes = lashes - received
Enter fullscreen mode Exit fullscreen mode

print("Total number of guards:", guard)
print("Tenali Raman receives:", lashes, "lash")

`
Final Output

plaintext
text
Guard 1 receives 8 lashes
Guard 2 receives 4 lashes
Guard 3 receives 2 lashes
Guard 4 receives 1 lashes

Total number of guards: 4
Tenali Raman receives: 1 lash

**2.Two trains stop at regular intervals. Train 1 stops at every 3rd station, and Train 2 stops at every 5th station. We need to find the first common station, the last common station, and all common stations where both trains stop.

The total number of stations is unknown, so we get the total number of stations as input from the user.**

`

total_stations = int(input("Enter total number of stations: "))

train1 = 3
train2 = 5

common_stations = []

for station in range(1, total_stations + 1):

    if station % train1 == 0 and station % train2 == 0:
        common_stations.append(station)

if len(common_stations) > 0:

    print("First common station:", common_stations[0])
    print("Last common station:", common_stations[-1])
    print("All common stations:", common_stations)

else:
    print("No common stations found.")
Enter fullscreen mode Exit fullscreen mode

`

How the Program Works

Get the Total Number of Stations

The input() function gets the total number of stations from the user. The int() function converts the input into an integer and stores it in the total_stations variable.

Define Train Intervals

We assign 3 to train1 and 5 to train2. These values represent the stopping intervals of the two trains.

Create an Empty List

We create an empty list called common_stations. This list stores all the station numbers where both trains stop.

Check Every Station

The for loop checks every station from 1 to the total number of stations. The range() function generates the station numbers.

Check Common Stations

The % operator checks whether a station is divisible by another number without leaving a remainder.

`plaintext

  • station % 3 == 0 checks whether Train 1 stops at that station.

  • station % 5 == 0 checks whether Train 2 stops at that station.

  • The and operator checks whether both conditions are true.
    `

Store Common Stations

The append() method adds the station number to the common_stations list whenever both trains stop at the same station.

Find the First and Last Common Stations

The len() function checks whether the list contains any common stations.

  • common_stations[0] returns the first common station.

  • common_stations[-1] returns the last common station.

Step 8: Display All Common Stations

Finally, the print() function displays the first common station, last common station, and all common stations. If no common stations exist, the program displays a message.

Execution – Example

Input:

Enter total number of stations: 100
Calculation:

The first common station is 15 because 15 is divisible by both 3 and 5.

The common stations are obtained by adding 15 repeatedly until reaching the total number of stations.

15
30
45
60
75
90
Final Output

plaintext
First common station: 15
Last common station: 90

Top comments (0)