Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Convolution with 2x2 array solution in 3rd party category for Counting Tiles by Rcp8jzd
import math
import numpy as np
from scipy import signal
def checkio(radius):
"""count tiles"""
size = math.ceil(radius) + 1
x, y = np.mgrid[0:size, 0:size]
# Boolean array: is the point at coordinates (x, y) inside the circle ?
is_inside = x ** 2 + y ** 2 < radius ** 2
convolution = signal.convolve2d(is_inside, [[1, 1], [1, 1]], mode='valid')
# All corners inside circle -> square inside circle
whole = np.sum(convolution == 4)
# Some corners inside and some outside -> square partially inside circle
partial = np.sum((0 < convolution) & (convolution < 4))
return [4 * whole, 4 * partial]
April 12, 2020
Comments: