Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
set of frozenset solution in Clear category for The Rows of Cakes by yoichi
def on_the_same_line(a, b, c):
if a[0] == b[0]:
return a[0] == c[0]
elif a[1] == b[1]:
return a[1] == c[1]
else:
return (c[0] - a[0]) * (b[1] - a[1]) == (b[0] - a[0]) * (c[1] - a[1])
def checkio(cakes):
from itertools import combinations
rows = set()
for a,b in combinations(cakes, 2):
if any(tuple(a) in r and tuple(b) in r for r in rows):
continue
row = frozenset(tuple(c) for c in cakes if c == a or c == b or on_the_same_line(a,b,c))
if len(row) >= 3:
rows.add(row)
return len(rows)
#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
Jan. 7, 2015