8.5

## Lab 5, CSC430, Spring 2022

### 1Acknowledgments

This lab uses exercises (with permission) from a lab written by Kurt Voelker. Many thanks!

### 2Developing 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.

### 3For loops

1. Develop the function sum_to_n that accepts a number n and returns the sum of the integers from 1 to n, inclusive.

2. Develop the function list_sum that accepts a list of numbers nums and returns the sum of the numbers in the list.

3. Develop the function min_in_list that accepts a list of numbers nums and returns the smallest number in the list.

4. Develop the function list_fifty_plus that accepts a list of numbers nums and returns True if and only if every element in the list is greater than or equal to 50. It should return False otherwise.

5. Develop the function many_shorts that accepts a list of strings strs and returns True if there are at least 3 strings with a length less than or equal to 3. It should return False otherwise.

### 4List Comprehensions or For loops?

For each of the following, choose a name for the function, write a single nontrivial test case, and specify whether it can be implemented by a list comprehension with a filter clause, or whether it requires a for loop with an accumulator. You do not need to develop a function or write any additional code for any of them.

1. return the sum of the numbers larger than zero

2. given a list of strings, return the first one that starts with the letter ’p’

3. given a list of strings, return the a list containing the ones starting with the letter ’p’.

4. given a list of lists of numbers, return a list containing the sum of every list with more than 4 elements

### 5Plotting

1. Using plt.plot() and np.linspace(), plot the following function for 100 values in the given range:

f(x) = \frac{(x+5)^2}{4 + 3x^2} \mathrm{for} -3 \leq x \leq 5

2. Using plt.plot() and np.linspace(), plot the following function for 100 values in the given range:

f(x) = \frac{5\sin(x)}{x + e^{-0.75x}} \mathrm{for} -5 \leq x \leq 10

3. Using plt.plot() and np.linspace(), plot the following function for 100 values in the given range:

f(x) = \sqrt{|\cos(3x)|} + \sin^2(4x) \textrm{in the domain} -2 \leq x \leq 2

4. The Gateway Arch in Saint Louis is shaped according to the equation

y = 693 - 68.8\cosh\left(\frac{x}{99.7}\right) \mathrm{ft}

Using plt.plot() and np.linspace(), plot its height for 100 values in the range -300 through 300 feet: