ECX 30 Days of Code and Design
Day 6
Man in the Well
Task:
A man is stuck at the bottom of a well. Each day, he climbs up 8 metres, and then at night, he slips downwards by 3 metres. Using loops (any loop of your choice), write a function to determine (and print) how many days it takes for him to climb out of a well of any given depth, where the depth of the well is taken as input.
E.g.; f(17) => the well is 17 metres deep.
Day 1: climbs 8m, height=8m; slips 3m, height=8-3=5m;
Day 2: climbs 8m, height=5+8=13m; slips 3m, height= 13-3=10m
Day 3: climbs 8m, height=10+8=18m.
But 18>17; STOP (height climbed has exceeded well depth).
Therefore, f(17) = 3 days.
My Approach
This is the code. I will explain it in bits throughout this article.
def day_in_well(well_height):
"""This determines the number of days, it takes for the man to climb out of the well"""
still_in_well = True
day = 1
climb_height = 0
while still_in_well is True:
if well_height <= 0:
print('He was never in a well.')
break
# The climbing height during the day.
climb_height = climb_height + 8
if climb_height < well_height:
# The falling at night.
climb_height = climb_height - 3
day = day + 1
else:
print('He took about %d day(s) to get out of the well' % day)
still_in_well = False
# Function call
day_in_well(17)
First, we define the function, still_in_well()
, which has an integer (the well height) as its parameter. Next, we initialize the variables we would be using; we assign still_in_well the Boolean value of True, which we would use in a while loop for counting the days it takes the man in the well to climb out of the well. Next, we assign the variable, day, the integer value of one, and climb_height, the value of zero.
def day_in_well(well_height):
"""This determines the number of days, it takes for the man to climb out of the well"""
still_in_well = True
day = 1
climb_height = 0
We then create a while loop which continues while the man is still in the well (i.e., the well height is having a higher value than the climb height). Next, we check if the well height is less than or equal to zero; if it is, we display “He was never in a well” on the screen; else we increase the climb height by 8.
while still_in_well is True:
if well_height <= 0:
print('He was never in a well.')
break
# The climbing height during the day.
climb_height = climb_height + 8
After increasing the climb height, we check if it is greater than the well height; if it is not, we decrease the climb height by 3 as stated in the question, and we increase the day value by 1 (to indicate the start of another day). The while loop continues until the climb height is greater than the well height. When the climb height is greater than the well height, still_in_well is given the value False, the number of days it takes the man to climb out is printed, and the loop breaks.
if climb_height < well_height:
# The falling at night.
climb_height = climb_height - 3
day = day + 1
else:
print('He took about %d day(s) to get out of the well.' % day)
still_in_well = False
Finally, we call the function.
day_in_well(17)
Output
He took about 3 day(s) to get out of the well
Run the code on Replit