Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Set width and height solution in Uncategorized category for The Square Chest by bryukh
HEIGHT = 4
WIDTH = 4
def checkSquare(lines, topLeft, edgeSize):
#check top
linesSets = [set(line) for line in lines]
if not all([{topLeft + i, topLeft + i + 1} in linesSets
for i in range(edgeSize)]):
return False
#check bottom
bottomLeft = topLeft + (WIDTH * edgeSize)
if not all([{bottomLeft + i, bottomLeft + i + 1} in linesSets
for i in range(edgeSize)]):
return False
#check left
if not all([{topLeft + WIDTH * i, topLeft + WIDTH * (i + 1)} in linesSets
for i in range(edgeSize)]):
return False
#check right
topRight = topLeft + edgeSize
if not all([{topRight + WIDTH * i, topRight + WIDTH * (i + 1)} in linesSets
for i in range(edgeSize)]):
return False
return True
def checkio(linesList):
"""Return the qantity of squares"""
quantity = 0
topLeftList = [i for i in range(1, WIDTH * (HEIGHT - 1)) if i % WIDTH]
for topLeft in topLeftList:
maxEdgeSize = min((WIDTH - topLeft % WIDTH), HEIGHT - 1 -(topLeft // HEIGHT))
for edgeSize in range(1, maxEdgeSize+1):
if checkSquare(linesList, topLeft, edgeSize):
quantity += 1
return quantity
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"
March 16, 2013