Skip to content

Commit

Permalink
matrices v2
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Oct 10, 2018
1 parent 2335d04 commit 5906c06
Showing 1 changed file with 90 additions and 3 deletions.
93 changes: 90 additions & 3 deletions exercises/matrices/matrices-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,76 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def extract_row(mat, i):\n",
" \"\"\" RETURN the ith row from mat. NOTE: the row MUST be a new list ! \"\"\"\n",
" #jupman-raise\n",
" return mat[i][:]\n",
" #/jupman-raise\n",
" \n",
"m = [\n",
" ['a','b'],\n",
" ['c','d'],\n",
" ['a','e'], \n",
"]\n",
"\n",
"\n",
"r = extract_row(m, 0)\n",
"assert r == ['a','b']\n",
"r[0] = 'z'\n",
"r = extract_row(m, 0)\n",
"assert r == ['a','b']\n",
"\n",
"assert extract_row(m, 1) == ['c','d']\n",
"assert extract_row(m, 2) == ['a','e']\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def extract_col(mat, j):\n",
" \"\"\" RETURN the jth column from mat. \"\"\"\n",
" \n",
" #jupman-raise\n",
" ret = []\n",
" \n",
" for row in mat:\n",
" ret.append(row[j])\n",
" return ret\n",
" #/jupman-raise\n",
"\n",
"m = [\n",
" ['a','b'],\n",
" ['c','d'],\n",
" ['a','e'], \n",
"]\n",
"\n",
"\n",
"c = extract_col(m, 0)\n",
"assert c == ['a','c','a']\n",
"c[0] = 'z'\n",
"c = extract_col(m, 0)\n",
"assert c == ['a','c','a']\n",
"\n",
"assert extract_col(m, 1) == ['b','d','e']\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def stitch_right(mat1,mat2):\n",
" \"\"\"Given matrices as list of lists mat1 of size n x l and mat2 of size n x r \n",
" \"\"\"Given matrices mat1 and mat2 as list of lists, with mat1 of size n x l and mat2 of size n x r, \n",
" RETURN a NEW matrix of size n x (l + r) as list of lists, by stitching second mat to the right end of mat1\n",
" \"\"\"\n",
" #jupman-raise\n",
Expand All @@ -108,7 +171,7 @@
" ['d','b','a']\n",
" ]\n",
"m2 = [\n",
" [ 'f','b'],\n",
" ['f','b'],\n",
" ['g','h']\n",
" ]\n",
"\n",
Expand All @@ -120,6 +183,30 @@
"assert stitch_right(m1, m2) == res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Others:\n",
"\n",
"```\n",
"stitch_left\n",
"\n",
"mat2 mat1\n",
"\n",
"stitch_up \n",
"\n",
"mat2 \n",
"mat1\n",
"\n",
"stitch down\n",
"\n",
"mat1\n",
"mat2\n",
"```\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 5906c06

Please sign in to comment.