Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Loop colomns in a quarter solution in Speedy category for Counting Tiles by macfreek
#!/usr/bin/env python3.2
# -*- coding: utf-8 -*-
from math import floor, ceil, sqrt
def checkio(radius):
"""count tiles"""
full = 0
total = 0
# Count only one quadrant, and multiply by 4.
# Loop through rows "r"
leftheight = radius
for r in range(int(ceil(radius))):
if radius > r+1:
rightheight = sqrt(radius**2 - (r+1)**2)
else:
rightheight = 0
full += int(rightheight)
total += int(ceil(leftheight))
leftheight = rightheight
print(radius, "->", [4*full, 4*(total-full)])
return [4*full, 4*(total-full)]
if __name__ == '__main__':
assert checkio(2) == [4, 12], "First, N=2"
assert checkio(3) == [16, 20], "Second, N=3"
assert checkio(2.1) == [4, 20], "Third, N=2.1"
assert checkio(2.5) == [12, 20], "Fourth, N=2.5"
Dec. 18, 2012
Comments: