Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Itertools combinations, axis intersection and slope. solution in Clear category for The Rows of Cakes by Phil15
from math import inf
from fractions import Fraction
from itertools import combinations
def slope(A, B):
"""Slope of the line (AB)."""
xA, yA, xB, yB = *A, *B
return inf if xA==xB else Fraction(yB-yA, xB-xA)
def aligned(A, B, C) -> bool:
"""Say if A, B and C are aligned, or not."""
return slope(A, C) == slope(A, B)
def axis_intersection(point, my_slope):
"""Line going through the point with my_slope slope
have an intersection with x-axis when slope is inf and y-axis otherwise.
Return axis and value of the intersection."""
if my_slope==inf:
return ('x-axis', point[0], my_slope)
return ('y-axis', point[1] - point[0] * my_slope, my_slope)
def checkio(cakes):
"""Look for alignments of 3 cakes,
here characterized by intersection with y-axis (or x-axis) and slope.
Return length of intersections set."""
return len({axis_intersection(A, slope(A, B))
for (A, B, C) in combinations(cakes, 3)
if aligned(A, B, C)})
Aug. 12, 2018
Comments: