Given some points (cartesian coordinates), return true if all of them lie on a line.  Treat both an empty set and a single point as a line.

```python
on_line(((1,2), (7,4), (22,9)) == True
on_line(((1,2), (-3,-14), (22,9))) == False
```

Source: codewars | Difficulty: EASY
Tags: Geometry

Fundamentals

Data structures

Starter code: def on_line(points):
	

Solutions:
def on_line(points):
	points = list(set(points))
	cross_product = lambda a, b, c: a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1])
	return all((cross_product(p, *points[:2]) == 0 for p in points[2:]))


from functools import partial

def collinear(p1, p2, p3):
	return (p3[1] - p2[1]) * (p2[0] - p1[0]) == (p2[1] - p1[1]) * (p3[0] - p2[0])

def on_line(points):
	points = list(set(points))
	return all(map(partial(collinear, *points[:2]), points[2:]))


def on_line(li):
	try:
		li = list(set(li))
		(x1, y1) = li.pop()
		return li[0] and len(set([(y2 - y1) / (x2 - x1) for (x2, y2) in li])) == 1
	except:
		return len(li) < 2 or len({i[0] for i in li}) == 1


from fractions import Fraction

def on_line(ps):
	ps = list(set(ps))
	if ps and all((isinstance(x, int) for x in ps)):
		return True
	return len({x1 if x1 == x2 else Fraction(y2 - y1, x2 - x1) for ((x1, y1), (x2, y2)) in zip(ps, ps[1:])}) <= 1


import numpy as np

def determinant(a):
	return round(np.linalg.det(np.matrix(a)))
f = lambda x: x + [1]

def on_line(points=None):
	if points == None:
		return True
	if len(points) <= 2:
		return True
	points = map(list, list(points))
	for i in range(len(points) - 2):
		matrix = [points[i], points[i + 1], points[i + 2]]
		matrix = map(f, matrix)
		if determinant(matrix) != 0:
			return False
	return True


def on_line(p):
	p = list(set(p))
	if len(p) <= 2:
		return True
	((x0, y0), (x1, y1)) = p[:2]
	return all(((x - x0) * (y1 - y0) == (x1 - x0) * (y - y0) for (x, y) in p[2:]))


from fractions import Fraction

def on_line(points):
	return len(set((Fraction(a[1] - b[1], a[0] - b[0]) if a[0] != b[0] else 'Invalid' for (a, b) in zip(points, points[1:]) if a != b))) < 2


def on_line(points):

	def slope_intercept(p1, p2):
		slope = (p2[1] - p1[1]) / (p2[0] - p1[0])
		intercept = p1[1] - slope * p1[0]
		return (slope, intercept)
	if len(points) == 0 or isinstance(points[0], int):
		return True
	for (p1, p2) in zip(points, points[1:]):
		if p1[0] == p2[0] or p1[1] == p2[1]:
			continue
		else:
			(s, i) = slope_intercept(p1, p2)
			try:
				if abs(s - slope) > 1e-08 or abs(i - intercept) > 1e-06:
					return False
			except:
				pass
			(slope, intercept) = (s, i)
	return True


def on_line(points):
	try:
		(x1, y1) = points[0]
		(x2, y2) = points[-1]
	except:
		return True
	for i in points[1:-1]:
		(x3, y3) = i
		if (x1 - x3) * (y2 - y3) != (x2 - x3) * (y1 - y3):
			return False
	return True


def on_line(points):
	y = 1
	x = 0
	if len(points) <= 2:
		return True
	all_x = points[0][x]
	all_y = points[0][y]
	test = True
	for point in points[1:]:
		if point[x] != all_x:
			test = False
			break
	if test:
		return True
	for point in points[1:]:
		if point[y] != all_y:
			test = False
			break
	if test:
		return True
	if points[1][x] - points[0][x] != 0:
		a = (points[1][y] - points[0][y]) / (points[1][x] - points[0][x])
	else:
		a = 0
	b = points[0][y] - a * points[0][x]
	for point in points[2:]:
		if point[y] != a * point[x] + b:
			return False
	return True