JamCoders

💾 Download

Lecture 3, Part 3 Extra Exercises

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]])

Should return:

[[False, True, False], [True, False, False], [True, True, True]]

In [ ]:
# write code here


# test code here
invertBool([[True, False, True],
            [False, True, True],
            [False, False, False]])

Challenge: Can you do the above problem without using list compression?

In [ ]:
# write code here

Run the following cell to test your invertBool(l) function.

In [ ]:
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 is the same length. The function should return 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 [ ]:
# Write your function here

Run the following cell to test your diagProd(l) function.

In [ ]:
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 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.

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 [ ]:
# Write your code here

Run the following cell to test your symmetric(l) function.

In [ ]:
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 alternates between hashtags and percent signs on odd lines and percent signs and hashtags on even lines.

For example:

advancedCheckered(4) → 
#%#%
%#%#
#%#%
%#%#
In [ ]:
# Write code here.

# Test your code here.

Challenge: Solve the above problem 13 in a different way.

In [ ]:
# Write code here.


# Test your code here.
advancedcheckered(4)

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?

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 [ ]:
# Write code here.

# Test your code here.

14.2

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.

For example:

to_two_d([34, 234, 23, 255, 98, 23, 155, 87], 2, 4) → 
[[34, 234, 23, 255],
  [98, 23, 155, 87]]
In [ ]:
# Write code here.

# Test your code here.
to_two_d([34, 234, 23, 255, 98, 23, 155, 87], 2, 4)