Lab 5
Due: Sunday, Mar. 5 at 11:59pm on Moodle
For this lab, first find your randomly assigned partner. Introduce yourselves - the person with the first name that comes last alphabetically should begin as the "driver", with the other partner as the "navigator". The driver will have the code open, and the navigator will have these instructions open.
At the end of the lab, email the code (finished or not) and transcript to the person who started as the navigator. If you do not finish during lab you have two options:
(1) Arrange to meet before Sunday and finish the lab together.
(2) Continue the code separately and denote the part you did on your own with a comment.
Note: it is not an option for one person to complete the code on their own and then send the finished code to their partner to submit. Any code that you submit should be either written by you, or written by you and your partner while you were pair programming. Both partners should submit their code on Moodle.
First create a new file called lab5A.py. In this file create a main method. Now download the file letter_freq.txt (right click and select "Save Link As...") and place this file in the same folder as lab5A.py. In the next steps you will answer several questions about this file.
(Note that these values are percentages.)
First create two empty lists to store the letters and the frequencies. Then inside the for loop you created to print each line, split each line using the split() method for strings. This will return a list with two elements (the letter and the frequency). Using the append() method for list, add the letter to your list of letters and the frequency to your list of frequencies. Make sure to convert the frequency from a string to a float.
So far everything you've done has been in main. The next part is to create a sum function that will add up all the frequencies (what should they add up to?) and return the result. Write a function called my_sum(lst) that will add up the elements of any list of numbers using a for loop. (Note: sum is also a built-in function, but in this case we will write it from scratch.) When you are finished, call your function on the list of frequencies inside main.
Click here to check your result.
Next find the minimum and maximum frequencies in this list of frequencies. To do this part, copy over the minimum and maximum functions that we've been developing in class. Then inside main, invoke both these functions on your list of frequencies and print the results.
Click here to check your result.
On their own, the minimum and maximum frequency values do not tell us the whole story. It would be better to also know which letters they were associated with. To do this, we can use the index function, which returns the index of a given element in list. Try out the code below in the shell to see how index works:
Discuss these results and how to use them to obtain the letter that is associated with the maximum and minimum frequencies. You can do this part in main, right after obtaining the minimum and maximum.
Click here to check your result.
Finally, create a function mean(lst) that will return the average of the values of any numerical list. Think about how to make use of one of the functions you've already created.
Click here to check your result.
Create a new file called lab5B.py. In this file create a main method. Now download the file tempest.txt (right click and select "Save Link As...") and place this file in the same folder as lab5B.py. This file contains the entire text of Shakespeare's "The Tempest". In this second part you will compare the letter frequencies in "The Tempest" to the average letter frequencies in the English language.
that does not return anything, but modifies each value of a list so that the resulting sum is 100. For this function, feel free to use the built-in sum method. An example is shown below:
Also round each result to three decimal places. Call this function from main, passing in the list of counts that was returned from compute_counts. Print out this list to make sure it is working. The first value in the list should be 7.215.
a 7.215Make sure to close the file after you finish writing it. Open up the resulting file in a text editor to make sure it is working.
From the shell, highlight all the testing you have done and all the output (since the beginning of lab) and copy it into a plain text file (.txt extension). On Windows you can use the program Notepad (under Accessories) and on Mac you can use the program TextEdit (under Applications). For Mac, if it doesn't have an option to save as txt, go to "Format" -> "Make Plain Text", then save again. Save this file as lab5_transcript.txt.
Make sure that both you and your partner have a copy of all the code written during the lab period, and the transcript. Both partners should submit the files: