#!/usr/bin/env python # coding: utf-8 # # Lecture 2, Part 2 Advanced Exercises # ## ***Before starting, please run the following cell*** # In[ ]: from __future__ import division, print_function # ## Question 12 # ### 12.1 # 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. # # For example: # # `invertBool(`
# `[[True, False, True], # [False, True, True], # [False, False, False]])` => # # `[[False, True, False], # [True, False, False], # [True, True, True]]` # In[11]: # write code here # Run the following cell to test your `invertBool(l)` function. # In[10]: def test(): lsts = [[[True, False, True, True], [False, False, False, True], [True, True, True, True], [False, True, False, True]], [[False, True, False], [True, True, True], [False, False, False]]] ans = [[[False, True, False, False], [True, True, True, False], [False, False, False, False], [True, False, True, False]], [[True, False, True], [False, False, False], [True, True, True]]] for i in range(2): if invertBool(lsts[i]) != ans[i]: return "Test Failed :'(" return "All Tests Passed!" test() # ### 12.2 # Write a function called `diagProd(l)` that takes in a list of integer or float lists where each nested list are the same length, and returns the product of a matrix's diagonal. You may assume the list is non-empty. # # For example: # # `diagProd(`
# `[[12, 5, 3], # [2, 1, 3], # [35, 23, 2]]` ) # # will return `24`. # In[20]: # Write your function here # Run the following cell to test your `diagProd(l)` function. # In[21]: def test(): lst = [ [[12, 5, 3], [2, 1, 3], [35, 23, 2]], [[54, 345, 23, 25], [135, 43, 3, 5], [75, 46, 63, 15], [16, 10, 9, 2]], [[1]], [[2, 4], [4, 2]] ] ans = [24,292572, 1, 4] for i in range(2): if diagProd(lst[i]) != ans[i]: return "Test Failed :'(" return "All Tests Passed!" test() # ### 12.3 # Write a function called `symmetric(l)` that takes in a list of integer lists called `l`, and returns a boolean on whether or not the matrix is symmetric. Recall that a matrix is symmetric if and only if when the ith columm becomes the ith row, it is still the same matrix. # # _Hint: You can do this without looking at the elements more than once._ # # For example: # # `symmetric(`
# `[[12, 5, 3], # [2, 1, 3], # [35, 23, 2]] )` will return `False`. # # `symmetric(`
# `[[1, 4, 5], # [4, 2, 6], # [5, 6, 3]] )` will return `True`. # In[34]: # Write your code here # Run the following cell to test your `symmetric(l)` function. # In[33]: def test(): lst = [[[12, 5, 3], [2, 1, 3], [35, 23, 2]], [[1, 4, 5], [4, 2, 6], [5, 6, 3]], [[2, 4], [4, 2]], [[54, 345, 23, 25], [135, 43, 3, 5], [75, 46, 63, 15], [16, 10, 9, 2]] ] ans = [False, True, True, False] for i in range(4): if symmetric(lst[i]) != ans[i]: return f'Test Case #{i +1} Failed' return "All Test Cases Passed!" test() # ## Question 13 # ### 13.1 # Write the function `advancedCheckered(x)` that takes in an integer `s` and prints a `s` by `s` checkerboard that has hashtags starting on even lines, and has percent signs starting on the odd lines, and they alternate during the line. # # For example: # # ```python # advancedCheckered(4) → # #%#% # %#%# # #%#% # %#%# # ``` # In[1]: # Write code here # ## Question 14 # ### 14.1 # 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? Here's a picture that describes how we can store an image as a 1D array. # # # # # # 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. # In[ ]: # ### 14.2 # Write a function called `1Dto2D` that takes in a list of integer pixels `lst`, height `h`, and width `w` and returns the 2D array representation of the image. # # For example: # # ```python # 1Dto2D([34, 234, 23, 255, 98, 23, 155, 87], 2, 4) → # [[34, 234, 23, 255], # [98, 23, 155, 87]] # ``` # In[ ]: # In[ ]: