Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Counting Tiles by J.W
from math import sqrt, ceil
# (x, y) is the coordinate of top-right corner of the squre
# r is the radius
# return the number of the 4 corners of the square which is/are in the circle
def number_of_points_in_circle(x, y, r):
return [sqrt(x*x + y*y) <= r, sqrt((x - 1)*(x -1) + y*y) <= r, sqrt(x*x + (y - 1)*(y - 1)) <= r, sqrt((x - 1)*(x - 1) + (y - 1)*(y - 1)) <= r].count(True)
def checkio(radius):
whole_tile_number = partial_tile_number = 0
for Y in range(ceil(radius), 0, -1):
for X in range(1, ceil(radius) + 1):
how_many_in_cirle = number_of_points_in_circle(X, Y, radius)
if how_many_in_cirle == 4:
whole_tile_number += 1
elif how_many_in_cirle > 0:
partial_tile_number += 1
# We only calculated the first quadrant. So when returning, need to times 4.
return [whole_tile_number * 4, partial_tile_number * 4]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(2) == [4, 12], "N=2"
assert checkio(3) == [16, 20], "N=3"
assert checkio(2.1) == [4, 20], "N=2.1"
assert checkio(2.5) == [12, 20], "N=2.5"
July 23, 2018
Comments: