From the course: Level Up: Advanced Python

Find a runner’s average race time - Python Tutorial

From the course: Level Up: Advanced Python

Find a runner’s average race time

- We're going to start off with a simple challenge to get you warmed up. In this challenge, given a log of race times we're going to determine the average race time for Jennifer Rhines, who's an American long distance runner. We're using a data set that has race times for women 10K runners from the Association of Road Racing Statisticians. So in this challenge we're going to update average race time dot py. So complete the get rhines times function which returns a list of Jennifer Rhines race times and then complete the get average function which returns Jennifer Rhines average race time and see the function docstrings for details of the format of the return values. You can use the test file in the test directory, so this is a sub directory of challenge to check your solution if you want or you can completely ignore it. We'll use the pytest framework to test your code. Now, if you're using code spaces you don't need to take any action. This package has been installed already. If you're working in a different I.D you'll need to have this package installed. Just do pip install pytest. Let me give you an example of this. I'm currently in the challenge directory, I go ahead and type pytest and then the part to the files. That's tests, test average race time. Now you can see that I get the message to field and that's because I haven't created any code yet in the average race time dot pyfile. So once you've created your solution re-run the pytest command, and if you get the message in green showing that all of the tests have past, then you're done. So go ahead to the average race time dot pyfile and use this as your starting point. Re-read the docstrings for each of the functions and look at the test files to check that you have the output in the expected format. So pause the video here and I'll show you the solution I came up with. Let's head over to the get rhines times function. We first create a list rhines times that we will populate with her race times. We then work our way through each line of the 10K race times. If the line includes the string, Jennifer Rhines, we then want to hand that off to the get time function. The get time function uses a regular expression to capture her times. And we then add each of the times to the list rhines times. And finally, we return this list at the end of the function. In the function get average, we use the results of the function that we created earlier. We call get rhines times. This returns a list of Jennifer Rhines race times and we store this in race times. We then want to sum all of her race times and store it in the variable total. All of her race times are in minutes and seconds but some of them include a millisecond component so we use a try and accept to deal with that. We then return the total divided by the number of entries in the list. And because of the formatting requirements from the docstring, we don't include the hour but we will also include the first millisecond digit. So let's check this, pass all the tests. So let me give myself a little bit of space. I'm just going to clear my screen. So pytest, test, and test. Average race time and this green bar confirms that we've passed all the tests. Now, my solution is just one way of solving this problem. Go ahead and share your solution in the Q&A section. Now, just so you know, if you just post your code in the Q&A section as plain text, it won't be formatted, and so it'll be difficult to read. You might want to post the link to your code snippet using something like github gist instead. I'd love to see your answer to this code challenge.

Contents