From 3f66dc7e13d58f9669be4e9d1a422ad7fd3fd8e4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 17 Feb 2024 15:58:04 -0800 Subject: [PATCH] Update transposition snippet (python) zip is a nice trick but it doesn't output a matrix. The output of zip is an iterator of tuples. You can't use it for accessing elements of matrix by index. `transposed_matrix[0][0]` will raise an error `TypeError: 'zip' object is not subscriptable` --- apps/website/contents/algorithms/matrix.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/website/contents/algorithms/matrix.md b/apps/website/contents/algorithms/matrix.md index 321464e3..8866fb66 100644 --- a/apps/website/contents/algorithms/matrix.md +++ b/apps/website/contents/algorithms/matrix.md @@ -59,7 +59,7 @@ Many grid-based games can be modeled as a matrix, such as Tic-Tac-Toe, Sudoku, C Transposing a matrix in Python is simply: ```py -transposed_matrix = zip(*matrix) +transposed_matrix = [[matrix[i][j] for i in range(len(matrix))] for j in range(len(matrix[0]))] ``` ## Essential questions