Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Rows of Cakes by cimarronm
from itertools import combinations
def inline(p0, p1, p2):
# cross product will be zero if the two lines from p0-p1 and p0-p2 are
# inline with each other
v0 = p1[0] - p0[0], p1[1] - p0[1]
v1 = p2[0] - p0[0], p2[1] - p0[1]
return v0[0]*v1[1] - v0[1]*v1[0] == 0
def checkio(cakes):
# compute all unique lines
lines = set()
for c0, c1 in combinations(cakes, 2):
line = frozenset(tuple(c) for c in cakes if inline(c, c0, c1))
lines.add(line)
# return the number of lines that have at least three cakes in them
return sum(len(line)>=3 for line in lines)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([[3, 3], [5, 5], [8, 8], [2, 8], [8, 2]]) == 2
assert checkio(
[[2, 2], [2, 5], [2, 8], [5, 2], [7, 2], [8, 2],
[9, 2], [4, 5], [4, 8], [7, 5], [5, 8], [9, 8]]) == 6
May 1, 2015