BONUS

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

A

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

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

In [ ]:


In [ ]:
import numpy as np

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

z2 = np.zeros((7, 3)).tolist()
np.testing.assert_allclose(z2, make_matrix(7, 3))

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

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

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

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