the code:
def check(balls):
r = False
for i in balls:
if(i == "R"):
r = True
if(r and i == "L"):
return False
return True
N = 5
Balls_direction = ["L", "R", "R", "L", "R"]
collisionCount = 0
while(not check(Balls_direction)):
for i in range(N-1):
if(Balls_direction[i] == "R" and Balls_direction[i+1] == "L"):
Balls_direction[i] = "L"
Balls_direction[i+1] = "R"
collisionCount += 1
print(collisionCount)
Question: N balls are arranged in a row. Each ball is moving either left (L) or right (R) with the same speed. When two balls collide, they each move to the opposite direction with the same speed as before. How many collisions will happen?