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 colinmcnicholl
import itertools
def vertical_lines(cakes):
groups = []
data = sorted(cakes, key=lambda x: x[0])
for k, g in itertools.groupby(data, lambda x: x[0]):
groups.append(list(g))
return len([elem for elem in groups if len(elem) >= 3])
def horizontal_lines(cakes):
groups = []
data = sorted(cakes, key=lambda x: x[1])
for k, g in itertools.groupby(data, lambda x: x[1]):
groups.append(list(g))
return len([elem for elem in groups if len(elem) >= 3])
def collinear(cakes):
triples = list(itertools.combinations(cakes, 3))
slopes_intercepts = set()
for triple in triples:
x1, x2, x3 = (triple[0] for triple in triple)
y1, y2, y3 = (triple[1] for triple in triple)
if ((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) == 0)
and (x1 != x2 != x3) and (y1 != y2 != y3)):
slope = (y2 - y1) / (x2 - x1)
intercept = y1 - slope * x1
slopes_intercepts.add((slope, intercept))
return len(slopes_intercepts)
def checkio(cakes):
num_vertical_lines = vertical_lines(cakes)
num_horizontal_lines = horizontal_lines(cakes)
num_diagonal_lines = collinear(cakes)
return num_vertical_lines + num_horizontal_lines + num_diagonal_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
Nov. 19, 2018
Comments: