Skip to content

Instantly share code, notes, and snippets.

@F1ashhimself
Created April 2, 2014 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save F1ashhimself/9945131 to your computer and use it in GitHub Desktop.
Save F1ashhimself/9945131 to your computer and use it in GitHub Desktop.
Facebook numbers
def get_square(side_length):
"""
Calculates square.
Arguments:
- side_length: int, length of square side.
Returns:
- List with values for square.
"""
numbers = [[()] * side_length for _ in range(side_length)]
directions = ((0, 1), (1, 0), (0, -1), (-1, 0))
coord = (0, 0)
dir_num = 0
num = 1
while num <= side_length ** 2:
i, j = coord[0], coord[1]
numbers[i][j] = num
next_i, next_j = i + directions[dir_num][0], j + directions[dir_num][1]
if next_i > len(numbers) - 1 or next_j > len(numbers[i]) - 1 or \
numbers[next_i][next_j]:
dir_num += 1
dir_num -= (dir_num / 4) * 4
coord = coord[0] + directions[dir_num][0], coord[1] + \
directions[dir_num][1]
num += 1
return numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment