1
from fractions import Fraction2
from itertools import pairwise4
from operator import itemgetter10
def minimumLines(self, prices: list[Point]) -> int:14
prices.sort(key=itemgetter(0))16
return self.using_slope(prices)17
# return self.using_cross_product(prices)20
def using_slope(prices: list[Point]) -> int:21
output, slope = 1, Solution.dy_by_dx23
ab = next(pairs := pairwise(prices))26
if slope(ab) != slope(bc):34
def dy_by_dx(ab: tuple[Point, Point]) -> float | Fraction:35
(x1, y1), (x2, y2) = ab37
dx, dy = x2 - x1, y2 - y140
# 1. dx is 0, it means we have a vertical line going from (x1, y1). So whether dy is positive or41
# negative, it does not matter42
# 2. infinity can not be represented by fraction module so returning it directly from math module45
# To avoid floating point error, we use fraction module.47
# (Simple divisions can give same results for example (apparently one of the test cases),48
# 499999998/499999999 and 499999999/500000000 gives same result, and that is where Fraction50
return Fraction(dy, dx)53
def using_cross_product(prices: list[Point]) -> int:54
output, on_line = 1, Solution.lie_on_same_line56
a = next(itr := iter(prices))58
for b, c in pairwise(itr):59
if not on_line(a, b, c):67
def lie_on_same_line(a: Point, b: Point, c: Point) -> bool:68
(x1, y1), (x2, y2), (x3, y3) = a, b, c70
return (y2 - y1) * (x3 - x2) == (x2 - x1) * (y3 - y2)