Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Counting Tiles by nickie
from math import ceil, hypot
def checkio(radius):
n = int(ceil(radius))
full = partial = 0
# let's stay in the upper-right quarter of the circle
for i in range(n):
for j in range(n):
if hypot(i+1, j+1) < radius:
full += 1 # full iff upper right corner is in the circle
elif hypot(i, j) < radius:
partial += 1 # partial iff lower right corner is in the circle
return [4*full, 4*partial]
#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"
Oct. 15, 2013
Comments: