Log In
NP Code Competition

Square dancing

Back to Problem Listing
Write a function called is_it_square that takes in a list of tuples that represent positive integer x,y coordinates on a grid. Return True if these coordinates outline a perfect square, False otherwise. The square can be filled in. Or not. Or it may be partially filled in. It doesn't matter. As long as the coordinates create an outline of a square without any holes in the perimeter and without any outliers outside of the square.

Sample

Here are visual examples of what I'm talking about:

Valid:
xx
xx

Valid:
x

Valid:
xxx
x x
xxx

Valid:
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx

Valid:
xxxxx
x   x
x x x
x   x
xxxxx

Valid:
xxxxx
x xxx
xxx x
x  xx
xxxxx

Invalid:
xxxxx
x   xx
x   x
x   x
xxxxx

Invalid:
xxxxx
x   x
    x
x   x
xxxxx

Invalid:
xxxxx
xxxxx
xxxxx
xxxx 
xxxxx


Here are code samples of the first three valid examples from above:
>>> is_it_square([(3, 3), (3, 4), (4, 3), (4, 4)])
True
>>> is_it_square([(72, 129)])
True
>>> is_it_square([(3, 2), (2, 1), (3, 1), (3, 3), (2, 3), (1, 1), (1, 2), (1, 3)])
True
This contest has ended. No more answers will be accepted.