Find the probability of reaching all points after N moves from point N | Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; When the person moves from ith index in right direction when i moves has been done ; When the person moves from ith index in left direction when i moves has been done ; Print the arr ; Driver code
def printProbabilities ( n , left ) : NEW_LINE INDENT right = 1 - left ; NEW_LINE arr = [ [ 0 for j in range ( 2 * n + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE arr [ 1 ] [ n + 1 ] = right ; NEW_LINE arr [ 1 ] [ n - 1 ] = left ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n + 1 ) : NEW_LINE INDENT arr [ i ] [ j ] += ( arr [ i - 1 ] [ j - 1 ] * right ) ; NEW_LINE DEDENT for j in range ( 2 * n - 1 , - 1 , - 1 ) : NEW_LINE INDENT arr [ i ] [ j ] += ( arr [ i - 1 ] [ j + 1 ] * left ) ; NEW_LINE DEDENT DEDENT for i in range ( 2 * n + 1 ) : NEW_LINE INDENT print ( " { :5.4f } ▁ " . format ( arr [ n ] [ i ] ) , end = ' ▁ ' ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE left = 0.5 ; NEW_LINE printProbabilities ( n , left ) ; NEW_LINE DEDENT