<jupyter_start><jupyter_text>Spiral Traverse[link](https://www.algoexpert.io/questions/Spiral%20Traverse)  My Solution<jupyter_code>def spiralTraverse(array):
    # Write your code here.
    i0, j0 = 0, 0
    i1, j1 = len(array) - 1, len(array[0]) - 1
    status = 0
    res = []
    while i0 <= i1 and j0 <= j1:
        if status == 0:
            for k in range(j0, j1 + 1):
                res.append(array[i0][k])
            i0 += 1
        elif status == 1:
            for k in range(i0, i1 + 1):
                res.append(array[k][j1])
            j1 -= 1
        elif status == 2:
            for k in range(j1, j0 - 1, -1):
                res.append(array[i1][k])
            i1 -= 1
        else:
            for k in range(i1, i0 - 1, -1):
                res.append(array[k][j0])
            j0 += 1
        status = (status + 1) % 4
    return res<jupyter_output><empty_output><jupyter_text>Expert Solution<jupyter_code># O(n) time | O(n) space
# where n is the total number of elements in the array
def spiralTraverse(array):
    result = []
    startRow, endRow = 0, len(array) - 1
    startCol, endCol = 0, len(array[0]) - 1

    while startRow <= endRow and startCol <= endCol:
        for col in range(startCol, endCol + 1):
            result.append(array[startRow][col])

        for row in range(startRow + 1, endRow + 1):
            result.append(array[row][endCol])

        for col in reversed(range(startCol, endCol)):
            result.append(array[endRow][col])

        for row in reversed(range(startRow + 1, endRow)):
            result.append(array[row][startCol])

        startRow += 1
        endRow -= 1
        startCol += 1
        endCol -= 1
    return result
# O(n) time | O(n) space
# where n is the total number of elements in the array
def spiralTraverse(array):
    result = []
    spiralFill(array, 0, len(array), 0, len(array[0]) - 1, results)
    return result

def spiralFill(array, startRow, endRow, startCol, endCol, results):
    if startRow > endRow or startCol > endCol:
        return
    
    for col in range(startCol, endCol + 1):
        result.append(array[startRow][col])

    for row in range(startRow + 1, endRow + 1):
        result.append(array[row][endCol])

    for col in reversed(range(startCol, endCol)):
        result.append(array[endRow][col])

    for row in reversed(range(startRow + 1, endRow)):
        result.append(array[row][startCol])
    
    spiralFill(array, startRow + 1, endRow - 1, startCol + 1, endCol - 1, result)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12854889589905363, 'fraction_numerical': 0.028391167192429023, 'mean_word_length': 2.2442455242966752, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '350497', 'license': 'MIT', 'n_tokens_mistral': 906, 'n_tokens_neox': 843, 'n_words': 234, 'path': 'algoExpert/spiral_traverse/solution.ipynb', 'repo_name': 'maple1eaf/learning_algorithm', 'size': '2536'}