{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "tW41g3jp8Uzc"
},
"source": [
"# Lecture 3, Part 3 Extra Exercises \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KcWlufPo8Uzf"
},
"source": [
"## Question 12"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "L0XHPZLJ8Uzi"
},
"source": [
"### 12.1"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b70FClm_8Uzl"
},
"source": [
"Write a function called `invertBool(l)` that takes in a list of lists called `l`, and returns a list of lists that represents all the booleans in the matrix, inverted. \n",
"\n",
"For example: \n",
"\n",
"`invertBool([[True, False, True],\n",
" [False, True, True], \n",
" [False, False, False]])`\n",
" \n",
" Should return:\n",
"\n",
"`[[False, True, False],\n",
" [True, False, False], \n",
" [True, True, True]]` "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aZHJFSp58Uzn"
},
"outputs": [],
"source": [
"# write code here\n",
"\n",
"\n",
"# test code here\n",
"invertBool([[True, False, True],\n",
" [False, True, True],\n",
" [False, False, False]])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0XhvD3LZ8Uzz"
},
"source": [
"#### Challenge: Can you do the above problem without using list compression?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zEKb66uV8Uz0"
},
"outputs": [],
"source": [
"# write code here"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "j-9fc-s48Uz5"
},
"source": [
"Run the following cell to test your `invertBool(l)` function. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NgdANei_8Uz7"
},
"outputs": [],
"source": [
"def test():\n",
" lsts = [[[True, False, True, True],\n",
" [False, False, False, True],\n",
" [True, True, True, True],\n",
" [False, True, False, True]],\n",
"\n",
" [[False, True, False],\n",
" [True, True, True],\n",
" [False, False, False]]]\n",
" ans = [[[False, True, False, False],\n",
" [True, True, True, False],\n",
" [False, False, False, False],\n",
" [True, False, True, False]],\n",
"\n",
" [[True, False, True],\n",
" [False, False, False],\n",
" [True, True, True]]]\n",
"\n",
" for i in range(2):\n",
" if invertBool(lsts[i]) != ans[i]:\n",
" return \"Test Failed :'(\"\n",
" return \"All Tests Passed!\"\n",
"\n",
"\n",
"test()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HEI8vFRq8Uz_"
},
"source": [
"### 12.2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rx3LGp088U0A"
},
"source": [
"Write a function called `diagProd(l)` that takes in a list of integer or float lists where each nested list is the same length. The function should return the product of a matrix's diagonal. You may assume the list is non-empty. \n",
"\n",
"For example: \n",
"\n",
"`diagProd(` <br/>\n",
"`[[12, 5, 3],\n",
"[2, 1, 3], \n",
"[35, 23, 2]]` )\n",
" \n",
" will return `24`. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rcu6wMW38U0B"
},
"outputs": [],
"source": [
"# Write your function here"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oB1cbIBG8U0E"
},
"source": [
"Run the following cell to test your `diagProd(l)` function. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ga7Scn8L8U0F"
},
"outputs": [],
"source": [
"def test():\n",
" lst = [\n",
" [[12, 5, 3],\n",
" [2, 1, 3],\n",
" [35, 23, 2]],\n",
" [[54, 345, 23, 25],\n",
" [135, 43, 3, 5],\n",
" [75, 46, 63, 15],\n",
" [16, 10, 9, 2]],\n",
" [[1]],\n",
" [[2, 4],\n",
" [4, 2]]\n",
" ]\n",
" ans = [24, 292572, 1, 4]\n",
" for i in range(2):\n",
" if diagProd(lst[i]) != ans[i]:\n",
" return \"Test Failed :'(\"\n",
" return \"All Tests Passed!\"\n",
"\n",
"\n",
"test()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QoLPbYAp8U0J"
},
"source": [
"### 12.3"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0qKNs0pw8U0K"
},
"source": [
"Write a function called `symmetric(l)` that takes in a list of integer lists called `l`, and returns `True` if the boolean is symmetrical and `False` if the matrix is not symmetrical. Recall: A matrix is symmetrical if and only if it is still the same matrix when the ith columm becomes the ith row.\n",
"\n",
"_Hint: You can do this without looking at the elements more than once._\n",
"\n",
"For example: \n",
"\n",
"`symmetric(` <br/>\n",
"`[[12, 5, 3],\n",
"[2, 1, 3], \n",
"[35, 23, 2]] )` will return `False`. \n",
"\n",
"`symmetric(` <br/>\n",
"`[[1, 4, 5],\n",
"[4, 2, 6], \n",
"[5, 6, 3]] )` will return `True`. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WsiRpY138U0L"
},
"outputs": [],
"source": [
"# Write your code here"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w4-CUyWT8U0O"
},
"source": [
"Run the following cell to test your `symmetric(l)` function. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wx-YIWCa8U0Q"
},
"outputs": [],
"source": [
"def test():\n",
" lst = [[[12, 5, 3],\n",
" [2, 1, 3],\n",
" [35, 23, 2]],\n",
"\n",
" [[1, 4, 5],\n",
" [4, 2, 6],\n",
" [5, 6, 3]],\n",
"\n",
" [[2, 4],\n",
" [4, 2]],\n",
"\n",
" [[54, 345, 23, 25],\n",
" [135, 43, 3, 5],\n",
" [75, 46, 63, 15],\n",
" [16, 10, 9, 2]]\n",
" ]\n",
" ans = [False, True, True, False]\n",
"\n",
" for i in range(4):\n",
" if symmetric(lst[i]) != ans[i]:\n",
" return f'Test Case #{i +1} Failed'\n",
" return \"All Test Cases Passed!\"\n",
"\n",
"\n",
"test()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DZjcdLzl8U0U"
},
"source": [
"## Question 13"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vO-acdQ48U0V"
},
"source": [
"### 13.1"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cpuGfM_U8U0V"
},
"source": [
"Write the function `advancedCheckered(x)` that takes in an integer `s` and prints a `s` by `s` checkerboard that alternates between hashtags and percent signs on odd lines and percent signs and hashtags on even lines. \n",
"\n",
"For example: \n",
"\n",
"```python\n",
"advancedCheckered(4) → \n",
"#%#%\n",
"%#%#\n",
"#%#%\n",
"%#%#\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZR-ijaN_8U0W"
},
"outputs": [],
"source": [
"# Write code here.\n",
"\n",
"# Test your code here."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rVOxI3s58U0Z"
},
"source": [
"#### Challenge: Solve the above problem 13 in a different way. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "egy9tuXG8U0a"
},
"outputs": [],
"source": [
"# Write code here.\n",
"\n",
"\n",
"# Test your code here.\n",
"advancedcheckered(4)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9Mma1xgG8U0e"
},
"source": [
"## Question 14"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ynn6DP-l8U0e"
},
"source": [
"### 14.1"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yTiddujI8U0f"
},
"source": [
"An image is usually represented as a 2D array, but let's say we only have access to a 1D array. Is there a way that we can represent a 2D array using a 1D array? \n",
"\n",
"Write a function called `getPixel(lst, h, w, i, j)` where `lst` is a 1D array, `h` is the height of the image, `w` is the width of image, `i` is the row that the pixel is on, and `j` is the column that the pixel is on. Then, this function will return the value that the pixel holds. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Xxpkre5v8U0g"
},
"outputs": [],
"source": [
"# Write code here.\n",
"\n",
"# Test your code here.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RHbfppvR8U0k"
},
"source": [
"### 14.2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ce74h6M98U0k"
},
"source": [
"Write a function called `to_two_d` that takes in a list of integer pixels `lst`, height `h`, and width `w` and returns the 2D array representation of the image.\n",
"\n",
"For example: \n",
"\n",
"```python \n",
"to_two_d([34, 234, 23, 255, 98, 23, 155, 87], 2, 4) → \n",
"[[34, 234, 23, 255],\n",
" [98, 23, 155, 87]]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zg0Ceq2H8U0l"
},
"outputs": [],
"source": [
"# Write code here.\n",
"\n",
"# Test your code here.\n",
"to_two_d([34, 234, 23, 255, 98, 23, 155, 87], 2, 4)"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "jamcoders_probs3c.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}