8.5.0.2

## Lab 6, CSC430, Spring 2022

### 1Developing Functions

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.

### 2For & While loops

Each of the following summations is of the form

\sum^{n}_{i=1}{E}

For some expression E. For each one, develop a function that computes the value of the expression E, taking i as an input. Be sure to include a docstring and a test case.

Following this, write the function that computes the sum of the first n terms of the summation in three different ways:

1. As a function whose body is of the form

 return sum([... for i in range(1,n)])
2. As a function whose body is a for loop, using the range generator.

3. As a function whose body is a while loop, using a mutable counter to track i.

These functions should end with _lc, _fl, and _wl (standing for "list comprehension", "for loop", and "while loop" respectively.

#### 2.1Exercises

1. 1 = \sum_{i=1}^{n}{\frac{1}{i(i+1)}} = \frac{1}{2} + \frac{1}{6} + \frac{1}{12} + \ldots

2. \sum_{i=1}^{n}{\frac{(-1)^{i-1}}{i}}

3. \pi = \frac{4}{1} - \frac{4}{3} + \frac{4}{5} - \frac{4}{7} + \frac{4}{9} - \frac{4}{11} + \ldots

### 3Temperatures

Problems 2-6 can be done in the same cell. The maximum daily temperatures (F) for Chicago and San Francisco during the month of August (31 days) are as follows:

 TCH = [75, 70, 86, 86, 80, 81, 73, 89, 91, 86, 81, 82, 86, 88, 89, 90, 82, 84, 81, 79, 73, 69, 73, 79, 82, 71, 66, 71, 69, 66, 66] TSF = [69, 68, 70, 73, 72, 71, 69, 76, 85, 87, 74, 84, 76, 67, 79, 75, 68, 68, 73, 72, 79, 68, 67, 69, 71, 70, 89, 95, 90, 66, 69]

#### 3.1Temperature Exercises

1. Plot the values of TCH and TSF on the same plot. Use this website: https://www.geeksforgeeks.org/plot-multiple-lines-in-matplotlib/ to figure out how to do this. Be sure to label the lines properly and include a legend in the example. Be sure to also include axis labels.

2. Develop the mean function that computes the mean of a list of numbers. (NB: You do not need a list comprehension or a for loop or a while loop for this).

3. Give the name TCH_AVG to the average temperature in Chicago.

4. Give the name TSF_AVG to the average temperature in San Francisco.

5. Compute the number of days in Chicago in which the temperature exceeds its mean. You can use a list comprehension plus a call to len for this. Name this value TCH_COUNT.

6. Compute the number of days in San Francisco in which the temperature exceeds its mean. You can use a list comprehension plus a call to len for this. Name this value SFO_COUNT.

7. Use a while loop to count how many days the maximum temperatures in TCH and TSF match up. Name this value T_MATCH.

8. Use Polynomial.fit to fit a straight line approximation to each of these datasets, and plot them.