def spiral(matrix, r, c): circular = [] start_row, end_row = 0, r start_col, end_col = 0, c while start_row < end_row and start_col < end_col: for i in range(start_col, end_col): circular.append(matrix[start_row][i]) start_row += 1 for i in range(start_row, end_row): circular.append(matrix[i][end_col-1]) end_col -= 1 for i in range(end_col, start_col, -1): circular.append(matrix[end_row-1][i-1]) end_row -= 1 for i in range(end_row, start_row, -1): circular.append(matrix[i-1][start_col]) start_col += 1 return circular
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter