BONUS

This will test your ability to make creative use of loops.

Write a function that creates a matrix of 0s using the list-of-lists strategy.

Your function should:

  • be named make_matrix
  • take 2 arguments: a number of rows (integer) and a number of columns (integer)
  • return 1 list: the list-of-lists matrix with the specified rows and columns (all the values set to 0)

For example, make_matrix(2, 2) should return [ [0, 0], [0, 0] ]. You cannot use any built-in or imported functions besides len() and range().


In [ ]:


In [ ]:
import numpy as np

z1 = np.zeros((5, 10)).tolist()
np.testing.assert_allclose(z1, make_matrix(5, 10))

In [ ]:
z2 = np.zeros((7, 3)).tolist()
np.testing.assert_allclose(z2, make_matrix(7, 3))

Part B

Write a function that tests whether every element of a list is even or not. For each individual element of the list, you need to evaluate whether or not that element is even and add the outcome of that test to a list (yep, a list of booleans).

Your function should:

  • be named is_even_list
  • takes 1 argument: a list of numbers
  • returns 1 list: a list of booleans

For example, is_even_list([1, 2, 3]) should return [False, True, False].


In [ ]:


In [ ]:
import numpy as np

t1 = [1, 2, 3, 4, 5, 6, 7]
a1 = [False, True, False, True, False, True, False]
np.testing.assert_array_equal(a1, is_even_list(t1))

In [ ]:
t2 = [7, 13, 19, 29, 37, 42, 100, 101]
a2 = [False, False, False, False, False, True, True, False]
np.testing.assert_array_equal(a2, is_even_list(t2))