Created
April 2, 2014 23:06
-
-
Save F1ashhimself/9945131 to your computer and use it in GitHub Desktop.
Facebook numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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