## Lab 6, CSC430, Spring 2022

### 1` `Developing Functions

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

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

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

Adding a docstring that indicates the purpose of the function,

Writing test cases for the function using assert, and finally

Writing the body of the function.

### 2` `For & 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:

As a function whose body is of the form

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

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.1` `Exercises

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

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

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

### 3` `Temperatures

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.1` `Temperature Exercises

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.

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

Give the name TCH_AVG to the average temperature in Chicago.

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

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.

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.

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

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