Suppose we have a list coordinates in a Cartesian plane, we have to check whether the coordinates form a straight line segment or not.
So, if the input is like coordinates = [(5, 5),(8, 8),(9, 9)], then the output will be True, as these points are forming a line segment with a slope 1.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, coordinates): (x0, y0), (x1, y1) = coordinates, coordinates for i in range(2, len(coordinates)): x, y = coordinates[i] if (x0 - x1) * (y1 - y) != (x1 - x) * (y0 - y1): return False return True ob = Solution() coordinates = [[5, 5],[8, 8],[9, 9]] print(ob.solve(coordinates))
[[5, 5],[8, 8],[9, 9]]