easy +8 pts

Transpose Matrix In-Place

Transpose a square matrix in-place and return a fresh transposed copy.

Write a function `transpose_inplace(matrix)` that transposes a square matrix (list of lists) *in place* and returns the transposed matrix. The function must modify the original matrix so that `matrix[i][j]` becomes `matrix[j][i]` for all valid indices. The function should return the same matrix object (not a new list). The input matrix is guaranteed to be square (n x n) with n >= 1.

Constraints

- The matrix is a list of lists, always square (n x n). - n is at least 1. - Elements can be any type (int, float, string, etc.). - Do not use any extra 2D structure; modify the list in place. - The function must return the modified matrix.

Example

```python
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = transpose_inplace(matrix)
print(result is matrix) # True
print(matrix)           # [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
```
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Loop over indices i and j where i < j to avoid double-swapping.
Swap matrix[i][j] and matrix[j][i] using simultaneous assignment.
Return the same matrix object after the swaps.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.