Question: Using python 3.0 or greater. I need help writing this recursive function. Question: Write a recursive function (not using loops, global varibles, or other ways
Using python 3.0 or greater. I need help writing this recursive function.
Question: Write a recursive function (not using loops, global varibles, or other ways to go around using a simple recursion) to calculate the time it takes to travel a certain distance.
The conditions are:
If the distance is 1m or less, then it takes 1 min to travel.
If the distance is greater than 1m, then every step is half the distance until it is equal or less than 1m for 1 min each step. Then the last step would take 1min to travel.
Ex. distance= 10m
10/2= 5m (+1min), 5/2 =2.5m (+1min), 2.5/2= 1.25 (+1min), 1.25/2= 0.625 (+1min), 0.625 to the end (+1min).
total time= 1+1+1+1+1= 5min to travel.
To see if the recursion is correct here is another example for the correct output (537m will take 11minutes).
This is the work I completed so far. I am having trouble writing the recursive case.
def time_travelled(distance): """ :param distance: distance to travel :return: Total time it takes to travel the distance entered. """ if distance<=1: return 1 else: return time_travelled(distance-1)/2
print(time_travelled(5))
0.0625
print(spaceTime(28))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
