8.5

## Lab 3, CSC430, Spring 2022

### 1Acknowledgments

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

### 2Exercises

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.

### 3Practice

1. Develop the function large_squares that accepts a list of numbers my_list and returns a new list that includes the square of each number that is greater than 50.

2. Develop the function which_in_range that accepts a list of numbers my_list and returns a new list that indicates if each number in the list is in the range [10, 20).

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

 array([[1., 0., 0.], [2., 0., 0.], [3., 0., 0.], [4., 0., 0.]])

### 4Data Cleaning

• Sometimes, data entry systems use unfortunate conventions. For instance, they might enter a person with an unknown name as "No Name Given", which could be interpreted as meaning that this is a person whose family name is "Given" and whose given name is "No". In Python, there is a special value, None, that is often used to represent missing data.

Develop the function clean_empty that accepts a list of strings and returns a new list where every instance of the string "No Name Given" is replaced with None.

• Another problem that occurs when (just say for instance hypothetically) you ask students to enter their Cal Poly login). This is a somewhat ambiguous prompt, and some people will enter, for instance, "clements@calpoly.edu" and other people will enter just "clements".

Develop the function clean_id that accepts a list of strings and returns a new list where each string in the list that ends with "@calpoly.edu" has the "@calpoly.edu" removed. Remember you can use + to concatenate two strings.

### 5Systems of Linear Equations

Use NumPy to solve the following sets of linear equations:

• Solve the following system of linear equations: 3x-2y+5z=7.5 -4.5x+2y+3z=5.5 5x+y-2.5z=4.5

• Solve the following system of linear equations: 3u+1.5v+w+0.5x+4y=-11.75 -2u+v+4w-3.5x+2y=19 6u-3v+2w+2.5x+y=-23 u+4v-3w+0.5x-2y=-1.5 3u+2v-w+1.5x-3y=-3.5

### 6Exceptions

1. Develop the function access that accepts a list of numbers nums and a number idx. Assume this number will be positive. If the number is a valid index in the list (hint: use len), return the item at that index. Otherwise, raise an IndexError with a detailed error message.

2. Develop the function check_start_abc that accepts a string. If the string starts with "a", "b", or "c" , return the string. Otherwise raise a ValueError with a detailed error message.

3. Develop the function divide that accepts two numbers. If the second number is zero, raise a ZeroDivisionError with a detailed error message. Otherwise, return the result of dividing the first by the second.