DEV Community

vishwa v
vishwa v

Posted on Edited on

Python-8

Police , Thief question

police=0
thief=40
step=0
while thief>police:
  police=police+5
  thief=thief+2
  step+=1
print("police feet : ", police)
print("police step : ",step)

Enter fullscreen mode Exit fullscreen mode

output:
police feet : 70
police step : 14

**

A king announces a unique reward for Hard Workers. On each day, the reward is multiplied by the day's number. The reward starts at 1 gold coin. Given the number of days n, write a program to calculate the final reward. hint: factorial

**

reward = 1
n = 5
i = 1

while i <= n:
    reward *= i
    i += 1

print(f"{n} days reward = {reward}")
Enter fullscreen mode Exit fullscreen mode

Output: 5 days reward = 120

Suppose a password consists of the letters: A, B, C, D. Each letter must be used exactly once. How many different passwords are possible? hint: factorial

letters = 4
result = 1
i = 1

while i <= letters:
    result *= i
    i += 1

print(result)
Enter fullscreen mode Exit fullscreen mode

output: 24

**

A frog accidentally falls into a 60-foot-deep well.

Every day:

During the day, the frog climbs 2 feet.
During the night, it slips down 0.5 feet.
Enter fullscreen mode Exit fullscreen mode

This pattern continues every day until the frog reaches the top of the well and escapes.

Write a program to determine:

How many days it takes for the frog to get out of the well.
On which day the frog escapes.
Enter fullscreen mode Exit fullscreen mode

Note: Once the frog reaches or crosses the top of the well during the daytime, it escapes immediately and does not slip back that night.

up = 0
day = 0
n = 60

while up <= 60:
    up = up + 2
    day = day + 1

    if up >= n:
        break

    up = up - 0.5

print("Number of days:", day)
print("Frog escapes on day:", day)
Enter fullscreen mode Exit fullscreen mode

Output: Number of days: 40
Frog escapes on day: 40

**

The Saint's Flower Basket

A saint goes for a morning walk every day. During his walk, he plucks flowers and keeps them in a basket.

On his way back home, he visits 7 temples. At each temple, he offers half of the flowers currently in his basket.

After visiting all 7 temples, he reaches home with exactly one flower remaining in his basket.

Write a program to determine how many flowers the saint had in his basket before he visited the first temple.

Hint: Think carefully about whether it is easier to solve the problem from the beginning or by working backwards from the final flower.

flower = 1
temple = 0

while temple < 7:
    flower = flower * 2
    temple += 1

print("Total Flowers =", flower)

Enter fullscreen mode Exit fullscreen mode

Output: Total Flowers = 128

Top comments (0)