Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Square Chest by Avaris
# migrated from python 2.7
import itertools
def checkio(lines_list):
"""Return the qantity of squares"""
lines_list = [list(sorted(line)) for line in lines_list]
def squares():
for size in range(1,4):
for x, y in itertools.product(list(range(4-size)), repeat=2):
square = []
for i in range(size):
square.append([4*x+y+i, 4*x+y+i+1])
square.append([4*(x+i)+y, 4*(x+i+1)+y])
square.append([4*(x+size)+y+i, 4*(x+size)+y+i+1])
square.append([4*(x+i)+y+size, 4*(x+i+1)+y+size])
yield [[a+1, b+1] for a,b in square]
return sum(all(c in lines_list for c in square) for square in squares())
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"
Nov. 4, 2012
Comments: