Program to count number of points that lie on a line in Python

Suppose we have a list of coordinates. Each coordinate has two values x and y, representing a point on the Cartesian plane. Now find the maximum number of points that lie on some line.
So, if the input is like coordinates = [[6, 2],[8, 3],[10, 4],[1, 1],[2, 2],[6, 6],[7, 7]], then the output will be 4, as the points are [1, 1], [2, 2], [6, 6], [7, 7]] that lies on a line.
To solve this, we will follow these steps −
res := 0
res := 0
for i in range 0 to size of points list, do(x1, y1) := points[i]slopes := a new mapsame := 1for j in range i + 1 to size of points, do(x2, y2) := points[j]if x2 is same as x1, thenslopes[inf] := 1 + (slopes[inf] if exits otherwise 0)otherwise when x1 is same as x2 and y1 is same as y2, thensame := same + 1otherwise,slope :=(y2 - y1) /(x2 - x1)slopes[slope] := 1 + (slopes[slope] if exits otherwise 0)if slopes is not empty, thenres := maximum of res and (same + maximum of list of all values of slopes)
for i in range 0 to size of points list, do
(x1, y1) := points[i]
(x1, y1) := points[i]
slopes := a new map
slopes := a new map
same := 1
same := 1
for j in range i + 1 to size of points, do(x2, y2) := points[j]if x2 is same as x1, thenslopes[inf] := 1 + (slopes[inf] if exits otherwise 0)otherwise when x1 is same as x2 and y1 is same as y2, thensame := same + 1otherwise,slope :=(y2 - y1) /(x2 - x1)slopes[slope] := 1 + (slopes[slope] if exits otherwise 0)
for j in range i + 1 to size of points, do
(x2, y2) := points[j]
(x2, y2) := points[j]
if x2 is same as x1, thenslopes[inf] := 1 + (slopes[inf] if exits otherwise 0)
if x2 is same as x1, then
slopes[inf] := 1 + (slopes[inf] if exits otherwise 0)
slopes[inf] := 1 + (slopes[inf] if exits otherwise 0)
otherwise when x1 is same as x2 and y1 is same as y2, thensame := same + 1
otherwise when x1 is same as x2 and y1 is same as y2, then
same := same + 1
same := same + 1
otherwise,slope :=(y2 - y1) /(x2 - x1)slopes[slope] := 1 + (slopes[slope] if exits otherwise 0)
otherwise,
slope :=(y2 - y1) /(x2 - x1)
slope :=(y2 - y1) /(x2 - x1)
slopes[slope] := 1 + (slopes[slope] if exits otherwise 0)
slopes[slope] := 1 + (slopes[slope] if exits otherwise 0)
if slopes is not empty, thenres := maximum of res and (same + maximum of list of all values of slopes)
if slopes is not empty, then
res := maximum of res and (same + maximum of list of all values of slopes)
res := maximum of res and (same + maximum of list of all values of slopes)
return res
return res
Let us see the following implementation to get better understanding −
 Live Demo
class Solution:
   def solve(self, points):
      res = 0
      for i in range(len(points)):
         x1, y1 = points[i][0], points[i][1]
         slopes = {}
         same = 1
         for j in range(i + 1, len(points)):
            x2, y2 = points[j][0], points[j][1]
            if x2 == x1:
               slopes[float("inf")] = slopes.get(float("inf"), 0) + 1
            elif x1 == x2 and y1 == y2:
               same += 1
            else:
               slope = (y2 - y1) / (x2 - x1)
               slopes[slope] = slopes.get(slope, 0) + 1
         if slopes:
            res = max(res, same + max(slopes.values()))
      return res
ob = Solution()
coordinates = [[6, 2],[8, 3],[10, 4],[1, 1],[2, 2],[6, 6],[7, 7]]
print(ob.solve(coordinates))
[[6, 2],[8, 3],[10, 4],[1, 1],[2, 2],[6, 6],[7, 7]]
4