Herbert attempts to climb the slope (Heffalumps are heavy), so each of his rushes gains him only 95% as much distance as his previous rush. This means that if his first rush gains him rush_height_gain metres, then his second rush will only gain him 0.95 * rush_height_gain metres, his third rush 0.95 * 0.95 * rush_height_gain metres, and so on. Fortunately, his back sliding reduces in the same way.
The traditional way to write the code is:
def num_rushes(slope_height, rush_height_gain, back_sliding):
''' Calculate how many rushes '''
import math
current_height = 0
rushes = 0
i = 0
while current_height < slope_height:
current_height += rush_height_gain * (0.95 ** i)
if current_height < slope_height:
current_height -= back_sliding * (0.95 ** i)
rushes += 1
i += 1
return rushes
- ans = num_rushes(10, 10, 9) print(ans) #result: 1
- ans = num_rushes(100, 15, 7) print(ans) #result: 19
- ans = num_rushes(150,20,9) print(ans) #result: 22
Now we need to use recursion to improve the efficiency. The recursive code I wrote below didn't get the desired result as above. Please point out how to revise it.
def num_rushes(slope_height, rush_height_gain, back_sliding, current_height=0):
if (slope_height-current_height) < rush_height_gain:
return 0
else:
current_height = current_height + 0.95*(rush_height_gain-back_sliding)
return num_rushes(slope_height, 0.95*rush_height_gain, 0.95*back_sliding, current_height) + 1
- ans = num_rushes(10, 10, 9) print(ans)
- ans = num_rushes(100, 15, 7) print(ans)
- ans = num_rushes(150,20,9) print(ans) #wrong result - get 23