@@ -0,0 +1,1612 @@
+#!/Applications/anaconda/envs/Python3/bin%0A%0A%0Adef main():%0A    # Test suite%0A    matrix = %5B%0A        %5B1, 2, 3%5D,%0A        %5B4, 5, 6%5D,%0A        %5B7, 8, 9%5D%0A    %5D%0A    spiral = spiral_order(matrix)%0A    print(spiral)%0A%0A%0Adef spiral_order(matrix):%0A    '''%0A    Given an mxn matrix, returns the elements in spiral order%0A    Input: list of m lists, with n elements per list%0A    Output: list of elements in spiral order%0A    matrix = %5B%0A        %5B1, 2, 3%5D,%0A        %5B4, 5, 6%5D,%0A        %5B7, 8, 9%5D%0A    %5D%0A    returns: %5B1, 2, 3, 6, 9, 8, 7, 4, 5%5D%0A    '''%0A    rows = len(matrix)%0A    cols = len(matrix%5B0%5D)%0A    direction = 0 # 0: right, 1: down, 2: left, 3: up%0A    # Index for each edge of matrix%0A    top = 0%0A    bottom = rows - 1%0A    left = 0%0A    right = cols - 1%0A%0A    spiral = %5B%5D%0A%0A    while (left %3C= right and top %3C= bottom):%0A        if direction == 0:%0A            # Heading left to right%0A            for x in range(left, right+1):%0A                spiral.append(matrix%5Btop%5D%5Bx%5D)%0A            top += 1%0A        elif direction == 1:%0A            # Heading top to bottom%0A            for x in range(top, bottom+1):%0A                spiral.append(matrix%5Bx%5D%5Bright%5D)%0A            right -= 1%0A        elif direction == 2:%0A            # Heading right to left%0A            for x in range(right, left-1, -1):%0A                spiral.append(matrix%5Bbottom%5D%5Bx%5D)%0A            bottom -= 1%0A        elif direction == 3:%0A            # Heading bottom to top%0A            for x in range(bottom, top-1, -1):%0A                spiral.append(matrix%5Bx%5D%5Bleft%5D)%0A            left += 1%0A%0A        direction = (direction + 1) %25 4%0A%0A    return spiral%0A%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add spiral order of matrix problem