Lab 2, CSC430, Spring 2022
1 Acknowledgments
2 Exercises
3 Boolean Operators
4 List Comprehensions
5 Num  Py
8.4.0.8

Lab 2, CSC430, Spring 2022

1 Acknowledgments

Portions of this lab are used with permission from assignments written by Kurt Voelker. Many thanks!

2 Exercises

In the exercises below, any instruction to "develop a function" implies the following steps:

  1. Deciding what kind of data the function will accept (in future labs, you will have to make decisions about how the data will be structured),

  2. Writing the first line of the function including the function name and parameter names,

  3. Adding a docstring that indicates the purpose of the function,

  4. Writing test cases for the function using assert, and finally

  5. Writing the body of the function.

3 Boolean Operators

Develop the function range1020 that accepts a number x and returns true when it is greater than or equal to 10 and less than 20.

Develop the function range2030 that accepts a number x and returns true when it is greater than or equal to 20 and less than 30.

Develop the function range3040 that accepts a number x and returns true when it is greater than or equal to 30 and less than 40.

Develop the function rangeAB that accepts two numbers, A and B, and returns the function that accepts a number x and returns true when it is greater than or equal to A and less than B. Do this by placing one of your earlier definitions inside of the new function, and then returning it (without calling it).

4 List Comprehensions

Develop the function z that doubles a number and adds four to it.

Develop the function zmap that accepts a list of numbers and returns a new list where each number in the list is doubled and greater by four. That is, apply z to each element of the list.

Using the earlier rangeAB function, develop the rangeFilter function that accepts a lower bound, an upper bound, and a list of numbers and strings, and returns a sublist containing only those elements that are numbers and are in the range [A,B).

5 NumPy

Use numpy and math operations to create the 2-dimensional column vector containing these values:
  • \sqrt{5.2^{3}}

  • 6.71 \times 10^3

  • (3 + 5.1^2)\sin(51°)

  • 15.8

  • \sqrt[3]{90}

  • \frac{\sin(\pi / 3)}{\tan (20°)}

Use numpy functions including zeros, arange, and concatenate to create this array:

array([[0., 0., 0., 0., 0.],

       [0., 0., 1., 2., 3.],

       [0., 0., 4., 5., 6.],

       [0., 0., 7., 8., 9.]])

Your solution should not include the numbers 5 or 6. Give this array the name a1.

Use array indexing to extract the array

array([[0., 1., 2.],

       [0., 4., 5.]])

from the existing array.