Distance Traveled by Two Trains together in the same Direction | Function to find the distance traveled together ; Stores distance travelled by A ; Stpres distance travelled by B ; Stores the total distance travelled together ; Sum of distance travelled ; Condition for traveling together ; Driver Code
def calc_distance ( A , B , n ) : NEW_LINE INDENT distance_traveled_A = 0 NEW_LINE distance_traveled_B = 0 NEW_LINE answer = 0 NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT distance_traveled_A += A [ i ] NEW_LINE distance_traveled_B += B [ i ] NEW_LINE if ( ( distance_traveled_A == distance_traveled_B ) and ( A [ i ] == B [ i ] ) ) : NEW_LINE INDENT answer += A [ i ] NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT A = [ 1 , 2 , 3 , 2 , 4 ] NEW_LINE B = [ 2 , 1 , 3 , 1 , 4 ] NEW_LINE N = len ( A ) NEW_LINE print ( calc_distance ( A , B , N ) ) NEW_LINE