Created
January 27, 2020 20:07
-
-
Save Taohid0/014dcd10defefa83da9a65aa5b5fae76 to your computer and use it in GitHub Desktop.
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 print_spiral_matrix(starting_row, starting_col, ending_row, ending_col, matrix): | |
up, down, right, left = [], [], [], [] | |
for c in range(starting_col, ending_col + 1): | |
up.append(matrix[starting_row][c]) | |
right.append(matrix[c][ending_row]) | |
down.append(matrix[ending_row][-(1 + c)]) | |
left.append(matrix[c][starting_row]) | |
result = up + right[1:] + down[1:] + left[1:-1] | |
if starting_row == ending_row and starting_row and ending_col: | |
return result | |
result += print_spiral_matrix(starting_row + 1, starting_col + 1, ending_row-1, ending_col-1, matrix) | |
return result | |
matrix = [ | |
[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9] | |
] | |
result = print_spiral_matrix(0, 0, len(matrix) - 1, len(matrix[0]) - 1, matrix) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment