Write a Python script named lab2.1.py. This script will decompose a text file na
ID: 3909039 • Letter: W
Question
Write a Python script named lab2.1.py. This script will decompose a text file named example.txt, available on your server at /users/name/example.txt, into an list of words, then perform list and set operations on the list of words.
Do not hard-code the example.txt text into your Python script — example.txt is readable by your Python script and your script, as part of the assignment, must read the file directly. If the /users/name/example.txt is modified for grading purposes to detect whether your code is actually reading the reference file, your script will be reading the correct data.
Use this line of code in your program to open and read the contents of the example.txt file into a string.
We will split this string of words into a list by using a simple regular expression. See the APPENDIX for more information about regular expressions.
1. Using Python slice notation, create two lists:
List One consists of the first 100 words in example.txt.
List Two consists of the remainder of the words in example.txt.
2. Write code that determines the length of the longest word in List One. Do not visually scan the list of works to find the length of the longest word — write Python code that finds it.
3. Use the max length + 2 to set the column width for printing the words in columns. You might use a solution that creates a list that contains the length of all the words in example.txt, then use the built-in max() function to return the longest length. You can do this in one line of code with a list comprehension.
4. Print the words in List One in 6 columns in formatted columns.
- Use the length of the longest word + 2 in the format specifier.
- Use the print() function format specifiers.
- The columns must be perfectly spaced, as in the example solution:?http://hills.ccsf.edu/~dputnam/cs131a/lab2.1.py.
5. Count the unique words in List One; print the count and the unique words.
6. Count the unique words in List Two; print the count and the unique words.
7. Count the unique words that are contained in both lists. This is a UNION operation. Print the count and the unique words.
8. Count the unique words that are in either List One or List Two, but not in both. This is a SYMMETRIC DIFFERENCE operation. Print the count and the words.
9. Create a sorted list of the unique words that are in List One but not in List Two (See The Quick Python Book, Ch 5.3) for examples of how to do this. Print the sorted list.
10. Create a sorted list of the unique words that are in List Two but not in List One. Print the sorted list.
Run your script and capture the output in a file.
Explanation / Answer
The code above should do the work you want. Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.