## Session 22

### Exercise, Step 1: Time to Float

Let's start with an easy challenge. I have a bunch of strings of the form "45:00" and "9:40". They are minutes:seconds pairs. I need them to be in floating-point form, such as 45.0 and 9.66....

Write a Python function time_to_float() that takes a string of this form as input and returns the equivalent floating-point number.

For example:

```    >>> time_to_float('40:00')
40.0
>>> time_to_float('9:40')
9.666666666666666
```

I hope this sort of function is straightforward for you now.

### Exercise, Step 2: String to List

I have a bunch of comma-separated strings of this form:

```    2012.12.20,B08,H,10.70,390,40:00,2:23
2013.01.02,E--,W, 2.05,345,20:00,0:30
```

Each string is the record of a workout. The fields in the record are:

• a ten-character date string
• a three-character exercise code
• a one-character location code
• a mileage value
• a calories burned value
• a time string for the planned workout time
• a time string for extra time

Write a Python function string_to_list() that takes a string of this form as input and returns a list containing the several fields, with numbers and times converted to numbers.

For example:

```    >>> string_to_list('2012.12.20,B08,H,10.70,390,40:00,2:23')
['2012.12.20', 'B08', 'H', 10.7, 390, 40.0, 2.3833333333333333]
>>> string_to_list('2013.01.02,E--,W, 2.05,345,20:00,0:30')
['2013.01.02', 'E--', 'W', 2.05, 345, 20.0, 0.5]
```

This sort of function is relatively new, but still familar. I used our own time_to_float() function to convert some fields and the built-in string-to-number functions to convert some others. For now, the rest are strings. This does highlight that lists are different than strings. Lists are mutable, that is, we can change one of its members using an assignment.

### Exercise, Step 3: File of Strings to List of Lists

I have so many of these comma-separated strings that they are in a file. Some lines have only a # character, kind of like a comment in a Python file. These lines can be ignored.

Write a Python function load_data_file() that takes the name of a file of this form as input and returns a list of data records, each represented as a list of the kind produced by string_to_list().

For example:

```    >>> load_data_file('data.txt')
[['2012.12.17', 'B08', 'W', 9.54, 335, 40.0, 2.5],
['2012.12.18', 'E  ', 'W', 4.41, 756, 40.0, 5.0],
['2012.12.19', 'B08', 'W', 11.05, 359, 40.0, 3.0],
...
['2013.01.03', 'E--', 'W', 1.3, 273, 15.0, 1.0],
['2013.01.04', 'B--', 'H', 12.6, 465, 45.0, 3.0],
['2013.01.06', 'B--', 'H', 12.5, 462, 45.0, 1.0]]
```

Your function should not print anything! I put in the newlines, to make this easier to read.

For this function, I used the starts_with() function in our string utilities module to filter out comment lines. Then I used our own string_to_list() function to convert the line to a list.

Beautiful. Surely we are done? Actually, we are just beginning! The data is finally in a form that we can use to answer questions that might interest us.

### Exercise, Step 4: Analyze the Data

For each exercise session, the three-character exercise code indicates whether the workout was on an elliptical machine (E__) or an exercise bike (B__).

Some of this data is from a period of rehabilitation. I am curious... How does the average speed of the workout change over time?

Write a Python function speeds() that takes as input:
• a character indicating workout type
• a list of workout data
and returns a list of [date, speed] lists for the corresponding workouts.

For example:

```    >>> speeds('E', workouts)
[['2012.12.18', 5.880000000000001],
['2012.12.27', 6.6],
['2013.01.02', 5.999999999999999],
['2013.01.03', 4.875]]
```

Again, your function should not print anything. It returns a list.

This function processes a list in just the same way that we have been processing strings and files...

... data analysis.

... writing programs. ... decomposing problems.

### What Happens When a Function Is Called

Remember this?

Here is a quick summary of the process, which we saw in detail in last week:
1. Python evaluates the arguments.
2. The values are given to the function...
3. ... and assigned to the function's parameters.
4. The function uses its parameters to perform its operation.
5. Python inserts the value of the function in place of the function call.

Practice this process until it flows naturally. It is basic knowledge for reading and writing code.

### Lessons from the Lab

... look at a couple of tasks and solutions ...:

• ... the example for all_to_int(a_list). The argument looks complex, but Python evaluates it and sends a list of strings as the value.

• ... and add(list_a, list_b) -- Because we want to walk down two lists in parallel, we have to use the counted form of the for-list.

### Quick Notes on Homework 9

• You do not have to read homework09.py. You just use the get_files() function to get a list of filenames to process.
```    >>> get_files()
['dirda-excerpts.txt', 'hamlet.txt', 'macbeth.txt',
'midsummer-nights-dream.txt', 'test-input.txt']
```

• multi_find() takes two strings: a string to be searched, and a string to search for. You should not change the function; you call it to do part of the task.

• Your main program will call search() once for each file[name] produced by get_files(), and search() will call multi_find() once for each line the file.

• Your computer may try to open output.dat using some crazy application. You can change the attributes for the file to open all files with the .dat extension using NotePad (in Windows) or TextEdit (in OS X).

IMPORTANT. A student found a bug in my implementation of multi_find(). (Thanks, Taylor.) I have fixed it. You can download the new zip file from the homework page, or just grab the updated str_utils.py file here.

### Wrap Up

Eugene Wallingford ..... wallingf@cs.uni.edu ..... November 6, 2014