human
What is a code to probability of collision between two trucks and the other steps required given the following:
Probability of collision between two trucks | Function to calculate total number of accidents.
Function to calculate count of all possible collision.
Size of string.
Stores the count of collisions.
Total number of truck in lane b.
Count total number of collisions while traversing the string a.
Function to calculate the probability of collisions.
Evaluate total outcome that is all the possible accident.
Evaluate favourable outcome i . e . , count of collision of trucks.
Print desired probability.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to probability of collision between two trucks and the other steps required.
 def count_of_accident ( a , b ) :
	n = len ( a )
	m = len ( b )
	if ( n > m ) :
		return ( m * ( m + 1 ) ) / 2
	else :
		return ( ( n * ( n + 1 ) ) / 2 + ( m - n ) * n )
def count_of_collision ( a , b ) :
	n = len ( a )
	m = len ( b )
	answer = 0
	count_of_truck_in_lane_b = 0
	for i in range ( 0 , m ) :
		if ( b [ i ] == ' T ' ) :
			count_of_truck_in_lane_b += 1
	i = 0
	while ( i < m and i < n ) :
		if ( a [ i ] == ' T ' ) :
			answer += count_of_truck_in_lane_b
		if ( b [ i ] == ' T ' ) :
			count_of_truck_in_lane_b -= 1
		i += 1
	return answer
def findProbability ( a , b ) :
	total_outcome = count_of_accident ( a , b ) ;
	favourable_outcome = count_of_collision ( a , b ) ;
	print ( favourable_outcome / total_outcome )
if __name__ == " _ _ main _ _ " :
	S = " TCCBCTTB "
	T = " BTCCBBTT "
	findProbability ( S , T )