Skip to content

Commit b4a7198

Browse files
Edit C0-A22 (Chapter 0, Activity 22).
1 parent 8678fa2 commit b4a7198

File tree

1 file changed

+107
-11
lines changed

1 file changed

+107
-11
lines changed

chapter0-exercises/chapter_0_part2.ipynb

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,29 +1042,125 @@
10421042
"id": "thick-demonstration"
10431043
},
10441044
"source": [
1045-
"## Activity: Obtain the transpose of a given matrix"
1045+
"## Activity 22: Transpose of a Matrix\n",
1046+
"\n",
1047+
"Another popular matrix operation is to compute the *transpose*. We flip the elements in the matrix over the diagonal producing a new matrix.\n",
1048+
"\n",
1049+
"You are given a 2D list of numbers, `matrix`. How would you write Python code to compute the tranpose of `matrix`?"
10461050
],
10471051
"id": "thick-demonstration"
10481052
},
1053+
{
1054+
"cell_type": "markdown",
1055+
"metadata": {
1056+
"id": "eKYvopLpX1B7"
1057+
},
1058+
"source": [
1059+
"### Example Input\n",
1060+
"\n",
1061+
"Put another way, the transpose matrix is just what the original `matrix` would be, if you were to read the elements in said `matrix` going up and down vertically, rather than left to right horizontally.\n",
1062+
"\n",
1063+
"Therefore, if we had a matrix like the following:\n",
1064+
"```\n",
1065+
"matrix = [\n",
1066+
" [8, 2, 3],\n",
1067+
" [9, 1, 9], \n",
1068+
" [5, 4, 1]\n",
1069+
"]\n",
1070+
"```\n",
1071+
"Then we would expect its transpose to be the following output:\n",
1072+
"```\n",
1073+
"transposed_matrix = [\n",
1074+
" [8, 9, 5], # first column\n",
1075+
" [2, 1, 4], # second column\n",
1076+
" [3, 9, 1] # third column\n",
1077+
"]\n",
1078+
"```\n"
1079+
],
1080+
"id": "eKYvopLpX1B7"
1081+
},
1082+
{
1083+
"cell_type": "code",
1084+
"metadata": {
1085+
"id": "7ooH8oluXyzU"
1086+
},
1087+
"source": [
1088+
"matrix = [[8, 2, 3], [9, 1, 9], [5, 4, 1]]"
1089+
],
1090+
"id": "7ooH8oluXyzU",
1091+
"execution_count": 21,
1092+
"outputs": []
1093+
},
1094+
{
1095+
"cell_type": "markdown",
1096+
"metadata": {
1097+
"id": "9lQnNxm-VeaU"
1098+
},
1099+
"source": [
1100+
"### Solution "
1101+
],
1102+
"id": "9lQnNxm-VeaU"
1103+
},
10491104
{
10501105
"cell_type": "code",
10511106
"metadata": {
1052-
"id": "musical-industry",
1053-
"outputId": "f196159c-a326-4202-f16d-b5de539b0905"
1107+
"id": "musical-industry"
10541108
},
10551109
"source": [
1056-
"def transpose(matx):\n",
1057-
" t_m = [[] for _ in range(len(matx[0]))]\n",
1058-
" # Do not use this: t_m = [[]]*len(matx[0])\n",
1059-
" for i in range(len(matx[0])):\n",
1110+
"def transpose(matrix):\n",
1111+
" # A: init the transpose matrix using the dims of the original\n",
1112+
" transposed_matrix = [[] for _ in range(len(matrix[0]))] \n",
1113+
" # B: iterate over each column\n",
1114+
" for col_index in range(len(matrix[0])):\n",
1115+
" # C: collect the elements in this column\n",
10601116
" for row in matrix:\n",
1061-
" t_m[i].append(row[i])\n",
1062-
" return t_m\n",
1117+
" transposed_matrix[col_index].append(row[col_index])\n",
1118+
" # D: return the transpose of the matrix\n",
1119+
" return transposed_matrix"
1120+
],
1121+
"id": "musical-industry",
1122+
"execution_count": 22,
1123+
"outputs": []
1124+
},
1125+
{
1126+
"cell_type": "markdown",
1127+
"metadata": {
1128+
"id": "UUcNULKQVg1i"
1129+
},
1130+
"source": [
1131+
"**Note**: in the solution above, it is also possible to implement step A in the following way:\n",
10631132
"\n",
1133+
"```\n",
1134+
"transpose = [[]]*len(matrix[0])\n",
1135+
"```\n",
1136+
"However, using a list comprehension with the `range()` function is the more efficient way to initialize our matrix, and thus is preferred. "
1137+
],
1138+
"id": "UUcNULKQVg1i"
1139+
},
1140+
{
1141+
"cell_type": "markdown",
1142+
"metadata": {
1143+
"id": "hJGmQfzfXqnR"
1144+
},
1145+
"source": [
1146+
"#### Test Out The Solution"
1147+
],
1148+
"id": "hJGmQfzfXqnR"
1149+
},
1150+
{
1151+
"cell_type": "code",
1152+
"metadata": {
1153+
"colab": {
1154+
"base_uri": "https://localhost:8080/"
1155+
},
1156+
"id": "B93uZPY0XwrN",
1157+
"outputId": "0fe978d0-7fd5-49a6-8884-e7445957e73d"
1158+
},
1159+
"source": [
10641160
"print(transpose(matrix))"
10651161
],
1066-
"id": "musical-industry",
1067-
"execution_count": null,
1162+
"id": "B93uZPY0XwrN",
1163+
"execution_count": 20,
10681164
"outputs": [
10691165
{
10701166
"output_type": "stream",

0 commit comments

Comments
 (0)