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.
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:
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.
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.
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:and returns a list of [date, speed] lists for the corresponding workouts.
- a character indicating workout type
- a list of workout data
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.
Remember this?
Here is a quick summary of the process, which we saw in detail in last week:
- Python evaluates the arguments.
- The values are given to the function...
- ... and assigned to the function's parameters.
- The function uses its parameters to perform its operation.
- 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.
... look at a couple of tasks and solutions ...:
>>> get_files() ['dirda-excerpts.txt', 'hamlet.txt', 'macbeth.txt', 'midsummer-nights-dream.txt', 'test-input.txt']
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.