M
M
Michael2019-03-17 20:51:35
Images
Michael, 2019-03-17 20:51:35

How to count the squares on a lined page?

Actually, the squares must be calculated here:
5c8e88c49b38f543813585.jpeg
Maybe there is an algorithm or an approach or a pattern.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2019-03-17
@longclaps

from collections import Counter
from itertools import combinations


def HowManySquares(xx, yy):
    """
    :param xx: отсортированые x-координаты вертикалей
    :param yy: отсортированые y-координаты горизонталей
    """
    ww = Counter(b - a for a, b in combinations(xx, 2))
    hh = Counter(b - a for a, b in combinations(yy, 2))
    return sum(c * hh[w] for w, c in ww.items())


print(HowManySquares([0, 2, 3, 4, 5, 6], [0, 2, 3, 4, 6]))  # 31

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question