Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
set <= set solution in Clear category for The Square Chest by hogee
from typing import List
def checkio(lines_list: List[List[int]]) -> int:
"""Return the quantity of squares"""
lines_set = set(tuple(sorted([x, y])) for x, y in lines_list)
small = lambda x: {(x, x + 1), (x, x + 4), (x + 1, x + 5), (x + 4, x + 5)} #Top, Left, Right, Bottom
middle = lambda x: {(x, x + 1), (x + 1, x + 2), #Top
(x, x + 4), (x + 4, x + 8), #Left
(x + 2, x + 6), (x + 6, x + 10), #Right
(x + 8, x + 9), (x + 9, x + 10)} #Bottom
large = {(1, 2), (2, 3), (3, 4), #Top
(1, 5), (5, 9), (9, 13), #Left
(4, 8), (8, 12,), (12, 16), #Right
(13, 14), (14, 15), (15, 16)} #Bottom
count = 0
for i in range(0, 3):
for j in range(1, 4):
if small(i * 4 + j) <= lines_set:
count += 1
if i < 2 and j < 3 and middle(i * 4 + j) <= lines_set:
count += 1
if large <= lines_set:
count += 1
return count
if __name__ == '__main__':
print("Example:")
print(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]]))
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"
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 12, 2018
Comments: