115k views
4 votes
A man is standing on an infinite ladder. In one move, he can either go up (with probability 0.5), or stay where he is (with probability 0.5). Please simulate one trip and print out the number of steps the man takes to move 100 steps up from his initial position. Hint: consider using inline if statement

1 Answer

3 votes

Answer:

Pseudo Code:

//Start

//position = 0 #Initial position on the ladder

//steps = 0 #initial moves of the man

//while position <= 100: #the man keeps moving until he moves 100 steps

// if randomint(1, 2) = 1: #this simulates the probability of 0.5

// position = position + 1 #the man only moves up with 0.5 probability

// end if

// steps =steps + 1 #this counts the total moves a man takes

//end while

//output steps #print the number of moves taken

User Zeynep
by
4.8k points