Skip to content

Commit

Permalink
upper, pyramid
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Oct 12, 2018
1 parent 8946414 commit 5710823
Showing 1 changed file with 150 additions and 27 deletions.
177 changes: 150 additions & 27 deletions exercises/matrices/matrices-solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,14 @@
" <iframe\n",
" width=\"900\"\n",
" height=\"600\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#code=def+extract_row_pointer%28mat%2C+i%29%3A%0A++++%22%22%22+RETURN+the+ith+row+from+mat.+%0A++++++++NOTE%3A+the+row+MUST+be+a+new+list%21+To+create+a+new+list+use+a+for+cycle.%0A++++%22%22%22%0A++++return+mat%5Bi%5D%0A%0A++++%0Am+%3D+%5B%0A++++++%5B%27a%27%2C%27b%27%5D%2C%0A++++++%5B%27c%27%2C%27d%27%5D%2C%0A++++++%5B%27a%27%2C%27e%27%5D%2C++++%0A%5D%0A%0Arow+%3D+extract_row_pointer%28m%2C+0%29%0A%0A%0A&py=3&cumulative=false&curInstr=0\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#py=3&code=def+extract_row_pointer%28mat%2C+i%29%3A%0A++++%22%22%22+RETURN+the+ith+row+from+mat.+%0A++++++++NOTE%3A+the+row+MUST+be+a+new+list%21+To+create+a+new+list+use+a+for+cycle.%0A++++%22%22%22%0A++++return+mat%5Bi%5D%0A%0A++++%0Am+%3D+%5B%0A++++++%5B%27a%27%2C%27b%27%5D%2C%0A++++++%5B%27c%27%2C%27d%27%5D%2C%0A++++++%5B%27a%27%2C%27e%27%5D%2C++++%0A%5D%0A%0Arow+%3D+extract_row_pointer%28m%2C+0%29%0A%0A%0A&cumulative=false&curInstr=0\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f1e49783ef0>"
"<IPython.lib.display.IFrame at 0x7f93e0026e48>"
]
},
"execution_count": 9,
Expand Down Expand Up @@ -425,14 +425,14 @@
" <iframe\n",
" width=\"900\"\n",
" height=\"775\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#code=%23+WARNING%3A+WRONG+CODE%21%21%21%21%0A%23+It+is+adding+a+LIST+as+element+to+another+empty+list.+%0A%23+In+other+words%2C+it+is+wrapping+the+row+%28which+is+already+a+list%29+into+another+list.+%0A%0Adef+extract_row%28mat%2C+i%29%3A%0A++++%22%22%22+RETURN+the+ith+row+from+mat.+NOTE%3A+the+row+MUST+be+a+new+list+%21+%22%22%22%0A++++%0A++++riga+%3D+%5B%5D%0A++++riga.append%28mat%5Bi%5D%29++%0A++++return+riga%0A%0A%0A%23+Let%27s+check+the+problem+in+Python+tutor%21+You+will+see+an+arrow+going+from+row+to+a+list+of+one+element+%0A%23+which+will+contain+exactly+one+arrow+to+the+original+row.%0A++++%0Am+%3D+%5B%0A++++++%5B%27a%27%2C%27b%27%5D%2C%0A++++++%5B%27c%27%2C%27d%27%5D%2C%0A++++++%5B%27a%27%2C%27e%27%5D%2C++++%0A%5D%0A%0Arow+%3D+extract_row%28m%2C0%29%0A%0A&py=3&cumulative=false&curInstr=0\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#py=3&code=%23+WARNING%3A+WRONG+CODE%21%21%21%21%0A%23+It+is+adding+a+LIST+as+element+to+another+empty+list.+%0A%23+In+other+words%2C+it+is+wrapping+the+row+%28which+is+already+a+list%29+into+another+list.+%0A%0Adef+extract_row%28mat%2C+i%29%3A%0A++++%22%22%22+RETURN+the+ith+row+from+mat.+NOTE%3A+the+row+MUST+be+a+new+list+%21+%22%22%22%0A++++%0A++++riga+%3D+%5B%5D%0A++++riga.append%28mat%5Bi%5D%29++%0A++++return+riga%0A%0A%0A%23+Let%27s+check+the+problem+in+Python+tutor%21+You+will+see+an+arrow+going+from+row+to+a+list+of+one+element+%0A%23+which+will+contain+exactly+one+arrow+to+the+original+row.%0A++++%0Am+%3D+%5B%0A++++++%5B%27a%27%2C%27b%27%5D%2C%0A++++++%5B%27c%27%2C%27d%27%5D%2C%0A++++++%5B%27a%27%2C%27e%27%5D%2C++++%0A%5D%0A%0Arow+%3D+extract_row%28m%2C0%29%0A%0A&cumulative=false&curInstr=0\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f1e501a7be0>"
"<IPython.lib.display.IFrame at 0x7f93e008cba8>"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -817,14 +817,14 @@
" <iframe\n",
" width=\"900\"\n",
" height=\"650\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#code=%0A%23+WARNING%3A+WRONG+CODE%0Adef+deep_clone_wrong%28mat%29%3A%0A++++%22%22%22+RETURN+a+NEW+list+of+lists+which+is+a+COMPLETE+DEEP+clone%0A++++++++of+mat+%28which+is+a+list+of+lists%29%0A++++%22%22%22%0A++++return+mat%5B%3A%5D+%23+NOT+SUFFICIENT+%21%0A++++++++++++++++++%23+This+is+a+SHALLOW+clone%2C+it%27s+only+copying+the+_external_+list%0A++++++++++++++++++%23+and+not+also+the+internal+ones+%21%0A%0Am+%3D+%5B%0A++++++++%5B%27a%27%2C%27b%27%5D%2C%0A++++++++%5B%27b%27%2C%27d%27%5D%0A++++%5D+++++++%0A++++++++%0Ares+%3D+deep_clone_wrong%28m%29%0A%0A%23+Notice+you+will+have+arrows+in+res+list+going+to+the+_original_+mat.+We+don%27t+want+this+%21%0A&py=3&cumulative=false&curInstr=0\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#py=3&code=%0A%23+WARNING%3A+WRONG+CODE%0Adef+deep_clone_wrong%28mat%29%3A%0A++++%22%22%22+RETURN+a+NEW+list+of+lists+which+is+a+COMPLETE+DEEP+clone%0A++++++++of+mat+%28which+is+a+list+of+lists%29%0A++++%22%22%22%0A++++return+mat%5B%3A%5D+%23+NOT+SUFFICIENT+%21%0A++++++++++++++++++%23+This+is+a+SHALLOW+clone%2C+it%27s+only+copying+the+_external_+list%0A++++++++++++++++++%23+and+not+also+the+internal+ones+%21%0A%0Am+%3D+%5B%0A++++++++%5B%27a%27%2C%27b%27%5D%2C%0A++++++++%5B%27b%27%2C%27d%27%5D%0A++++%5D+++++++%0A++++++++%0Ares+%3D+deep_clone_wrong%28m%29%0A%0A%23+Notice+you+will+have+arrows+in+res+list+going+to+the+_original_+mat.+We+don%27t+want+this+%21%0A&cumulative=false&curInstr=0\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f1e50127518>"
"<IPython.lib.display.IFrame at 0x7f93e0213438>"
]
},
"execution_count": 19,
Expand Down Expand Up @@ -1100,6 +1100,55 @@
"assert m1 == res\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def is_utriang(mat):\n",
" \"\"\" Takes a RETURN True if the provided nxn matrix is upper triangular, that is, it has all the entries \n",
" below the diagonal set to zero\n",
" \"\"\"\n",
" #jupman-raise\n",
" n = len(mat)\n",
" m = len(mat[0])\n",
" \n",
" for i in range(1,n):\n",
" for j in range(0, i // 2):\n",
" if mat[i][j] != 0:\n",
" return False\n",
" return True\n",
" #/jupman-raise\n",
" \n",
"assert is_utriang([\n",
" [1]\n",
" ]) == True\n",
"assert is_utriang([\n",
" [3,2,5],\n",
" [0,6,2],\n",
" [0,0,4]\n",
"]) == True\n",
"\n",
"assert is_utriang([\n",
" [3,2,5],\n",
" [0,6,2],\n",
" [1,0,4]\n",
"]) == False\n",
"\n",
"assert is_utriang([\n",
" [3,2,5],\n",
" [0,6,2],\n",
" [1,1,4]\n",
"]) == False\n",
"\n",
"assert is_utriang([\n",
" [3,2,5],\n",
" [1,6,2],\n",
" [1,0,4]\n",
"]) == False\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -1111,7 +1160,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1147,7 +1196,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1184,7 +1233,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1216,7 +1265,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### transpose\n",
"### transpose_1\n",
"\n",
"Transpose a matrix _in-place_. The transpose $M^T$ of a matrix $M$ is defined as "
]
Expand All @@ -1237,7 +1286,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 29,
"metadata": {},
"outputs": [
{
Expand All @@ -1246,7 +1295,7 @@
"[3, 4, 5, 6]"
]
},
"execution_count": 28,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1264,7 +1313,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1332,7 +1381,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"outputs": [
{
Expand All @@ -1341,7 +1390,7 @@
"[0, 0, 0]"
]
},
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1366,7 +1415,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 32,
"metadata": {},
"outputs": [
{
Expand All @@ -1375,7 +1424,7 @@
"[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]"
]
},
"execution_count": 31,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1394,7 +1443,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 33,
"metadata": {},
"outputs": [
{
Expand All @@ -1404,17 +1453,17 @@
" <iframe\n",
" width=\"900\"\n",
" height=\"225\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#code=bad+%3D+%5B%5B0%5D%2A3%5D%2A5%0A&py=3&cumulative=false&curInstr=0\"\n",
" src=\"https://pythontutor.com/iframe-embed.html#py=3&code=bad+%3D+%5B%5B0%5D%2A3%5D%2A5%0A&cumulative=false&curInstr=0\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f1e50135d30>"
"<IPython.lib.display.IFrame at 0x7f93e021bba8>"
]
},
"execution_count": 32,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1435,7 +1484,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 34,
"metadata": {},
"outputs": [
{
Expand All @@ -1444,7 +1493,7 @@
"[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]"
]
},
"execution_count": 33,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1465,7 +1514,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1516,7 +1565,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1584,7 +1633,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1628,7 +1677,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1730,6 +1779,80 @@
" ]\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### pyramid"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"def pyramid(n):\n",
" \"\"\"\n",
" Takes a number n >= 1 and RETURN a matrix as list of lists containing numbers displaced like this\n",
" example for a pyramid of square 7:\n",
"\n",
" 1111111\n",
" 1222221 \n",
" 1233321 \n",
" 1234321\n",
" 1233321\n",
" 1222221\n",
" 1111111\n",
" \"\"\" \n",
" #jupman-raise\n",
" ret = [[0]*n for i in range(n)]\n",
" for i in range(n//2 + 1):\n",
" for j in range(n//2 +1):\n",
" ret[i][j] = min(i, j) + 1\n",
" ret[i][n-j-1] = min(i, j) + 1\n",
" ret[n-i-1][j] = min(i, j) + 1\n",
" ret[n-i-1][n-j-1] = min(i, j) + 1\n",
" \n",
" ret[n//2][n//2] = n // 2 + 1\n",
" return ret\n",
" #/jupman-raise\n",
"\n",
"\n",
"assert pyramid(1) == [\n",
" [1]\n",
" ]\n",
"\n",
"assert pyramid(3) == [\n",
" [1,1,1],\n",
" [1,2,1],\n",
" [1,1,1]\n",
" ]\n",
"\n",
"assert pyramid(4) == [\n",
" [1, 1, 1, 1], \n",
" [1, 3, 3, 1], \n",
" [1, 3, 3, 1], \n",
" [1, 1, 1, 1]\n",
"]\n",
"\n",
"assert pyramid(5) == [\n",
" [1, 1, 1, 1, 1],\n",
" [1, 2, 2, 2, 1], \n",
" [1, 2, 3, 2, 1], \n",
" [1, 2, 2, 2, 1], \n",
" [1, 1, 1, 1, 1]\n",
" ]\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 5710823

Please sign in to comment.