Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Rows of Cakes by U.V
from collections import Counter
def checkio(cakes):
#print(*cakes)
ss = []
N = len(cakes)
x, y = (zip(*cakes))
xc = Counter(x)
yc = Counter(y)
L = len([c for v, c in xc.items() if c > 2]) + len([c for v, c in yc.items() if c > 2])
for i in range(N-2):
x1,y1 = cakes[i]
for j in range(i+1, N-1):
x2,y2 = cakes[j]
x0 = x2 - x1
y0 = y2 - y1
if x0 == 0 or y0 == 0:
continue
else:
S = set((i, j,) )
for k in range(j+1, N):
x3,y3 = cakes[k]
if x0 * (y3 - y1) == y0 * (x3 - x1):
S = S.union((k, ))
if any (S.issubset(s) for s in ss ) or len(S) < 3:
continue
ss.append(S)
L += 1
#print(L, ':', x1, y1, x2, y2, x3, y3, S)
return L
#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 3, 2022
Comments: