To start this lab, download this zip file.

# Lab 10 - Lists

## What does this print?

```
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
```

```
def add_something(numbers, something):
result = []
for number in numbers:
result.append(number + something)
return result
def print_list(items):
for item in items:
print(item)
numbers = [2, 3, 4, 5]
numbers = add_something(numbers, 3)
print_list(numbers)
numbers = add_something(numbers, 1)
print_list(numbers)
```

```
def remove(numbers, target):
result = []
for number in numbers:
if number != target:
result.append(number)
return result
def print_list(items):
for item in items:
print(item)
numbers = [1, 2, 3, 1, 2, 3]
other = remove(numbers, 2)
print_list(other)
numbers = remove(numbers, 3)
print_list(numbers)
```

```
def geometric_mean(numbers):
total = 1
for number in numbers:
total = total * number
return total ** (1/len(numbers))
gm = geometric_mean([1, 2, 2, 4, 2])
print(gm)
gm = geometric_mean([3, 9, 1])
print(gm)
```

**HINT**:

$32^{\frac{1}{5}} = 2$

```
def as_ratios(numbers, factor):
result = []
for number in numbers:
result.append(number / factor)
return result
def floor(numbers, minimum):
result = []
for number in numbers:
if number < minimum:
number = minimum
result.append(number)
return result
def floor_ratios(numbers):
numbers = as_ratios(numbers, 100)
numbers = floor(numbers, 0.3)
for number in numbers:
print(number)
floor_ratios([5, 23, 55, 79])
```

```
def add(numbers, offset):
result = []
for number in numbers:
result.append(number + offset)
return result
def mod(numbers, factor):
result = []
for number in numbers:
result.append(number % factor)
return result
def find_min(numbers):
minval = None
for number in numbers:
if minval is None or number < minval:
minval = number
return minval
def mod_min(numbers, offset, factor):
numbers = add(numbers, offset)
numbers = mod(numbers, factor)
return find_min(numbers)
print(mod_min([1, 2, 3, 4], 3, 5))
print(mod_min([5, 7, 10, 22], 4, 6))
```

## Activities

For each of these activities, **draw out** your strategy. What are the series of
steps you will take to solve each problem? How do those steps fit together?

`fun_with_lists.py`

Implement the functions in `fun_with_lists.py`

.

The tests are there to help you get started. What other tests could you write to make sure your implementations are correct?

`ceiling.py`

Write a function `ceiling(numbers, limit)`

that returns a copy of the list of numbers with each number greater than `limit`

replaced with `limit`

.

For example:

`ceiling([1, 3, 5, 8], 4)`

Would return:

`[1, 3, 4, 4]`

`sum_abs.py`

Write a function `sum_abs(numbers)`

that returns the sum of the absolute values of the numbers.

For example:

`sum_abs([-2, 5, -8, 3])`

Would return:

`18`

`average_positive.py`

Write a function `average_positive(numbers)`

that returns the average of the positive numbers in `numbers`

.

For example:

`average_positive([-2, 5, 7, -9, 3, -1])`

Would return:

`5.0`

Break the problem into smaller pieces. What combination of simple patterns can you use to solve this problem? Write a function for each step.

`filter_infrequent.py`

Write a function `filter_infrequent(numbers, threshold)`

that returns a list of frequencies greater than the `threshold`

.

The `numbers`

are first converted to frequencies by dividing by the total (the sum of the numbers). Then frequencies less than `threshold`

are removed.

Break the problem into smaller pieces. What combination of simple patterns can you use to solve this problem? Write a function for each step.

For example:

`filter_infrequent([1, 2, 3, 4, 10], 0.1)`

Would return;

`[0.15, 0.2, 0.5]`

What if you wanted `filter_infrequent`

to return the original numbers instead of
frequencies (but still filter out numbers that had a low corresponding
frequency)?

`scale_to_unit.py`

Write a function `scale_to_unit(numbers)`

that returns the `numbers`

translated
and scaled to the unit scale (0 to 1).

This means that you must find the minimum value and subtract that from all numbers. Then find the maximum value of this new list and divide all of the numbers by this maximum.

For example:

`scale_to_unit([1, 2, 3, 4, 5])`

Would return:

`[0, 0.25, 0.5, 0.75, 1]`

## Grading

Activity | Points |
---|---|

`fun_with_lists.py` | 5 points |

`ceiling.py` | 5 points |

`sum_abs.py` | 5 points |

`average_positive.py` | 5 points |

`filter_infrequent.py` | 5 points |

`scale_to_unit.py` | 5 points |