def num_rushes_first_question(slope_height, rush_height_gain, back_sliding): current_height = 0 rushes = 0 while current_height < slope_height: current_height += rush_height_gain rushes += 1 if (current_height < slope_height): current_height -= back_sliding return rushes def num_rushes_second_question(slope_height, rush_height_gain, back_sliding): current_height = 0 rushes = 0 factor = 1.0 while current_height < slope_height: current_height += factor * rush_height_gain if (current_height < slope_height): current_height -= factor * back_sliding elif (factor == 0.0 and current_height < slope_height): # Never reach the top. return "inf" rushes += 1 factor -= 0.05 return rushes print(num_rushes_first_question(10, 2, 0.5)) print(num_rushes_second_question(10, 2, 0.5))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question