{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework 04 Exercises\n", "\n", "The following exercises are based on the material from Lab 4. Feel free to open the Python notebook for [Lab 4](https://colab.research.google.com/github/AllenDowney/ElementsOfDataScience/blob/master/04_loops.ipynb) for reference. You might need to import the `NumPy` module for this homework exercise." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 1:** In the lab notebook you have seen how to access the elements of a list one-by-one using a `for` loop. In this excercise you will learn how to append elements to the end of a list. Let's say we want the list to contain the letters from your name, i.e., the first element of the list is the first letter of your name and so on. Translate the tasks below into Python code:\n", "1. In the cell below, start off by creating an empty list called `name_list`. You can do this by typing in the open and close square brakects without anything inside them and assigning it to `name_list`. \n", "2. Create another variable called `name` and assign it a string that contains your first name.\n", "3. Now write a `for` statement that will loop through the letters of your first name.\n", "4. Inside the loop, we want to take the current letter from the `name` string and append it to the list `name_list`. In order to do that we will use the function called `append`. The syntax for `append` is as follows. `list1.append(var1)`. In this example, `var1` will be appended, i.e., added as an element at the end, to the list `list1`. \n", "5. Finally, outside the `for` statement, print the list `name_list`. Remember, indentation is important in Python!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2:** In this exercise, we want to count the number times the word *Prince* is used in the book *War and Peace*. Translate the tasks below to Python code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a:** Following the instructions from Lab 4, download and open the book *War and Peace* from [Project Gutenberg](https://www.gutenberg.org). When you open the file, name your file pointer `fp` like you did in the lab for consistency. Also, create a variable called `count` and assign it a value of `0`. (We will use this variable as a counter for the number of times we find the word \"Prince\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b:** Now we going to write nested `for` loops, *i.e.*, a `for` loop within a `for` loop. The outer `for` statement is to loop through all the lines in the book. The inner `for` statement is to loop through all the words in each line. We also want to check what each word is, which means we would want an `if` statement inside the inner `for` loop. Remember to first run the code from the previous part before writing and running the code for this.\n", "1. Write the outer `for` loop to go through all the lines in the book.\n", "2. Inside this `for` loop, write a statement that will split each line and assign it to a variable called `words`.\n", "3. Now write the inner `for` loop to go through each word in the list `words`. \n", "4. Write an `if` statement in side the `for` loop to check whether the current word is equal to \"Prince\".\n", "5. Increment the `count` variable by 1 if the word \"Prince\" is found. Pay attention to indentation.\n", "5. Finally, print out the value of the variable `count` outside all the loops. You should get an answer of `1498`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 3:** In this exercise we will look at another function that is useful for opening and loading files with numeric data into arrays. We will first have to import the `Numpy` module and use the function `np.loadtxt`. We will use these to load a file containing data from a Yamaha scooter and then calculate the averageg miles per gallon for the scooter. Follow the instructions below and convert the tasks into Python code. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a:** Import the `NumPy` module." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b:** Download the file *Yamaha.txt* from this link. Use the same steps as you used in the Lab to download *War and Peace* from Project Gutenberg. *Suggestion:* Copy the code from the lab and simply change the url for `wget` to https://raw.githubusercontent.com/stat10/DS10-Python-HW/master/Data/Yamaha.txt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**c:** Load the contents of the file *Yamaha.txt* into an array named `yamaha_data` and print contents of the array. Use the function `np.loadtxt` which is documented [here](https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html) to load the file into an array. The syntax should be `var = np.loadtxt('filename')`, where `var` would be the array into which the data is loaded and `filename` is the string with the name of the file. Also, use the syntax `print(var)` to print the contents of the array `var`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In one of the previous labs and Python HW you have come across arrays. However, we haven't yet seen a very useful and powerful usecase of `NumPy` arrays, which is being able to access elements of the arrays using an index. By the way, elements of a list (or for that matter, characters in a string) can also be accessed using indices. In Python, the elements of a 2D array, for example the `yamaha_data` array, can be accesses by providing a row index and a column index. One important thing to keep in mind is that in Python indices start from 0 (not 1). The code below creates a 2D array `A`, prints out all of its elements, and shows how to obtain the element in the first row and first column, and second row and fifth column, respectively. Notice that the syntax is `array_name[row_index, column_index]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = np.array([[10, 20, 30, 40, 50, 60], [100, 200, 300, 400, 500, 600]])\n", "print(A)\n", "print('') # prints nothing, so it shows up as an empty line\n", "print(A[0, 0]) # element in first row and first column\n", "print('')\n", "print(A[1, 4]) # element in second row and fifth column" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**d:** The data in *Yamaha.txt* is arranged in 14 rows and 2 columns. The first column contains the *odometer reading in miles* while the second column contains the *amount of gas filled in gallons* when the odometer reading was taken. Write code to print out the odometer reading from row 5 and the gallons of gas in row 10. You should get `1010.9` and `1.231`, respectively." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**e:** Now let's go ahead and calculate the average miles per gallon for the Yamaha scooter. \n", "\n", "To get the miles driven between each gas filling, we have to take a difference of two odometer readings, *i.e.*, subtract the one at the time of a filling from the one at the next time of filling. Then we would want to divide the miles driven between two fillings by the amount of gas filled to obtain miles per gallon. Finally, we would want to take the mean of the miles per gallon between each of the fillings.\n", "\n", "In terms of array operations, we want to subtract the odometer reading at say index `i` from the one at index `i+1`. This difference gives the distance travelled between two gas fillings. When we divide this distance by the amount of gas filled at the index `i+1`, we get the miles per gallon for that distance. Finally, the average can be computed by using the function `np.mean`. \n", "\n", "Also, note that the *Yamaha.txt* file has 14 entries, however since we need to take differences between successive entries, we will end up with 13 numbers for miles per gallon. So, the `for` statement to calculate the miles per gallon would need to repeat 13 times. We would also need a way to define the index `i` that goes from `0` to `13`. Python has a function called `range` that can be used to generate these indices. We will not go into the details of the function `range` right now because we will come back to it again. But the syntax for generating the indicies in a `for` loop is as follows: `for i in range(max_num):`. This line of code will generate `max_num` number of integers that will be assinged to the variable `i` starting from `0` to a value of `max_num - 1`.\n", "\n", "Translate the tasks below to Python code for calculating the average miles per gallon.\n", "1. Create an empty list called `mpg` to hold the miles per gallon computed between each gas filling.\n", "2. Write a `for` loop that repeats as many times as the number of rows in the array `yamaha_data` less one. (*Hint:* use `len(yamaha_data)` to find the number of rows in `yamaha_data`, then `len(yamaha_data) - 1` is the maximum number of integers you want to obtain using `range`.)\n", "3. Inside the `for` calculate the current miles per gallon by taking the difference between the odometer reading at `i+1` and `i`, divided by the gas filled at index `i+1`. Assign this value to a variable called `curr_mpg`.\n", "4. Append the `curr_mpg` value to the list `mpg`. See exercise 1 above for the syntax of `append`.\n", "5. Compute the average miles per gallon using the function `np.mean` and print out the value. You should get a value of `96.27371246852911`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Solution\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }