Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple and clear solution in Clear category for The Square Chest by Vasily__Chibilyaev
def checkio(lines):
'''
So I implemented a rather straightforward strategy based on calculating number of squares
for which the current point represents top left angle. And I know there are ways to decrease
number of iterations by estimating max possible size of square for a point, but preconditions
state that grid is pretty small, so I decided not to tangle my solution.
'''
def check_square(i, size):
for k in range(size):
if [i+k, i+k+1] not in lines\
or [i+size*4+k, i+size*4+k+1] not in lines\
or [i+k*4, i+(k+1)*4] not in lines\
or [i+size+k*4, i+size+(k+1)*4] not in lines:
return 0
return 1
count=0
lines=[sorted(l) for l in lines]
for point in range(1, 17):
for size in range(1, 4):
count+=check_square(point, size)
return count
if __name__ == '__main__':
assert (checkio([[1, 2], [3, 4], [1, 5], [2, 6], [4, 8], [5, 6], [6, 7],
[7, 8], [6, 10], [7, 11], [8, 12], [10, 11],
[10, 14], [12, 16], [14, 15], [15, 16]]) == 3), "First, from description"
assert (checkio([[1, 2], [2, 3], [3, 4], [1, 5], [4, 8],
[6, 7], [5, 9], [6, 10], [7, 11], [8, 12],
[9, 13], [10, 11], [12, 16], [13, 14], [14, 15], [15, 16]]) == 2), "Second, from description"
assert (checkio([[1, 2], [1, 5], [2, 6], [5, 6]]) == 1), "Third, one small square"
assert (checkio([[1, 2], [1, 5], [2, 6], [5, 9], [6, 10], [9, 10]]) == 0), "Fourth, it's not square"
assert (checkio([[16, 15], [16, 12], [15, 11], [11, 10],
[10, 14], [14, 13], [13, 9]]) == 0), "Fifth, snake"
Sept. 22, 2017
Comments: