- 2
About below describes, how can I write the code?
The snail climbs up 7 feet each day and slips back 2 feet each night. How many days will it take the snail to get out of a well with the given depth? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will reach 32 feet and get out of the well at day, without slipping back that night.
1 Respuesta
0
Each day the snail climbs 7 feet, then slips back 2 feet at night — so it gains 5 feet per full day. But on the last day, it climbs out and doesn’t slip back.
So if the well depth is d, the formula is:
import math
def days_to_escape(depth):
if depth <= 7:
return 1
return math.ceil((depth - 7) / 5) + 1
# Example:
print(days_to_escape(31)) # Output:6